Last active
August 30, 2019 18:37
-
-
Save kartikparmar/5f8a969d9261e010b0645a8ace9f500e to your computer and use it in GitHub Desktop.
Multiplying the rate if the any product in the cart falls under the specified categories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 ); | |
function overwrite_shipping_cost($rates, $package ) { | |
global $woocommerce; | |
if ( get_field( 'multiplier', 'option' ) ) : | |
$multiplier = get_field( 'multiplier', 'option' ); | |
else : | |
$multiplier = 1; | |
endif; | |
// My changes are starting here | |
$product_category_id = array( 86, 21, 47 ); // Ids of Product Category | |
$product_category_id_check = false; // Assuming that any of the product doesn't have above category | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { // looping through the cart. | |
$_product = $values['data']; | |
$product_cats_ids = wc_get_product_term_ids( $_product->get_id(), 'product_cat' ); // Getting category ids of the individual product | |
foreach ( $product_category_id as $key => $value ) { | |
if ( in_array( $value, $product_cats_ids ) ) { | |
$product_category_id_check = true; // If any of the product in cart having category defined in $product_category_id variable then this will set to true. | |
} | |
} | |
} | |
// My changes are ending here | |
if ( ! is_admin() && $product_category_id_check ) { | |
// If Shiptor 1 | |
if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) { | |
$rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier; | |
} | |
// If Shiptor 2 | |
if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) { | |
$rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier; | |
} | |
//If anothee shipping method | |
if ( isset( $rates['rpaefw_post_calc:16'] )) { | |
$rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier; | |
} | |
} | |
return $rates; | |
} |
It works, thanks!
How can I contact you? Are you open for paid tasks?
I am glad to know that it works for you. :)
Yep, I am open for paid tasks and you can contact me on below email address.
Email Address: parmarkartik19 at yahoo dot com
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works, thanks!
How can I contact you? Are you open for paid tasks?