Last active
January 28, 2019 06:14
-
-
Save varun-pluginhive/0d86c88cbe37b240d313588dbcced366 to your computer and use it in GitHub Desktop.
Snippet to reduce the shipping Cost for Particular Country and Shipping Method if Shipping class exists in cart. If all the Snippet Classes are available in cart then only snippet will affect the price. (AND Logic)
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
/** | |
* Snippet to reduce the shipping Cost for Particular Country and Shipping Method if Shipping class exists in cart. | |
* Created at : 15 Jan 2019 | |
* Updated at : 15 Jan 2019 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
* Gist Link : https://gist.github.com/varun-pluginhive/0d86c88cbe37b240d313588dbcced366 | |
*/ | |
if( ! function_exists('ph_reduce_shipping_method_cost') ) { | |
function ph_reduce_shipping_method_cost( $shipping_costs, $package ){ | |
$shipping_classes = array( 'shipping_class_a', 'shipping_class_b', 'shipping_class_c' ); // Shipping Classes that should be in cart to reduce the price | |
$price_to_reduce = 5; // Price to reduce | |
$destination_country = "US"; // Destination Country for which rate need to be reduced | |
$canada_post_id = "wf_shipping_canada_post"; // Shipping Method For which rate need to be reduced | |
if( $package['destination']['country'] == $destination_country ) { | |
// Get Shipping Class from Cart | |
$cart_shipping_classes = array(); | |
foreach( $package['contents'] as $line_item ) { | |
$product_shipping_class = $line_item['data']->get_shipping_class(); | |
if( ! empty($product_shipping_class) ) $cart_shipping_classes[] = $product_shipping_class; | |
} | |
if( count(array_intersect( $shipping_classes, $cart_shipping_classes)) == count($shipping_classes) ) { | |
foreach( $shipping_costs as &$shipping_cost ) { | |
if( $shipping_cost->get_method_id() == $canada_post_id ) { | |
$shipping_cost->set_cost( (float) $shipping_cost->get_cost() - $price_to_reduce ); | |
} | |
} | |
} | |
} | |
return $shipping_costs; | |
} | |
add_filter( 'woocommerce_package_rates', 'ph_reduce_shipping_method_cost', 10, 2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment