add custom woocommerce shipping rule with PHP
-
I want to add custom woocommerce shipping rule with PHP like that :
If the product_id is 2077 and if the number of product is 1 Then the cost is 5€
else : let the other rules.
Edit : This works :
add_filter( 'woocommerce_package_rates', 'custom_package_rates' ); function custom_package_rates( $rates, $package ) { $total = WC()->cart->cart_contents_total; $nb_product=0; $id_product=""; $qt_product=0; foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; // if( $_product->id == 2077 ) { // $rr=1; // } $id_product.=$_product->id; $qt_product=$values['quantity']; $nb_product=$nb_product+1; } if ( ($id_product=="2077" OR $id_product=="2189") and $qt_product==1 and $nb_product==1 ) { // Si seul Carnet de notes foreach ( $rates as $rate ) { // Store the previous cost in $cost $cost = $rate->cost; // Adjust the cost as needed $rate->cost = 5; } } // etc add the remaining condition return $rates; }
- The topic ‘add custom woocommerce shipping rule with PHP’ is closed to new replies.