Reputation: 14283
I'm having an issue when try to apply a filter to woocommerce_package_rates
.
Basically what I'm trying to do, is hide some shipping methods if a coupon code is applied.
This is my test code:
add_action('woocommerce_before_cart_totals', 'display_coupon');
function display_coupon($order)
{
global $woocommerce;
global $wpdb;
if (isset($_POST['coupon_code'])) {
add_filter('woocommerce_package_rates', 'hide_shipping_when_coupon_code_is_available', 10, 2);
echo "Ok"
} else {
echo "no"
}
}
function hide_shipping_when_coupon_code_is_available($rates, $package){
$test= $rates['table_rate_shipping_test'];
$rates = array();
$rates['test'] = $test;
return $rates;
}
Now, When I apply the coupon code, the variable $coupon_code
is populated and it goes inside the if statement, but it doesn't seam to call the woocommerce_package_rates
filter. Is it wrong try to call a filter from inside another function like that? if no, what else am I doing wrong here?
Thanks in advance
PS: As a reference, I modified this code here -> http://docs.woothemes.com/document/hide-other-shipping-methods-when-free-shipping-is-available/
Upvotes: 1
Views: 227
Reputation: 11808
Try the following code
add_action('wp', 'wdm_my_custom_function');
function wdm_my_custom_function() {
global $woocommerce;
global $wpdb;
if ( isset($_POST['coupon_code']) ) {
add_filter('woocommerce_package_rates', 'hide_shipping_when_coupon_code_is_available', 10, 2);
echo "Ok";
} else {
echo "no";
}
}
Let me know if your problem is solved by this.
Upvotes: 1