dpv21589 2015-10-27 21:51
浏览 112

Woocommerce根据发货地址检查设置和取消设置运输

UPDATED QUESTION AFTER Scriptonomy ANSWER BELOW

I have a woocommerce site in which I have a few different shipping options. I need to hide and show these shipping options based on the "Ship to different address button being checked"

//remove local pickup
function ddc_hide_shipping( $rates, $package ) {

    unset( $rates['local_pickup'] );

    return $rates;
}

//check if needs shipping address so we can unset local pickup
function ddc_update_shipping_options($instance) {

    parse_str(html_entity_decode($instance), $postData);
    if ( @$postData['ship_to_different_address']  ) {
        add_filter('woocommerce_package_rates', 'ddc_hide_shipping');
    } 
}
add_action('woocommerce_checkout_update_order_review', 'ddc_update_shipping_options');

Using the above code that got from help from Scriptonomy I am getting closer. The problem I am having as this doesn't seem to work when the user checks and un checks the ship to different billing address. This only works for me on first page load, not on every ajax call to update the order review area.

Thank you.

  • 写回答

1条回答 默认 最新

  • doupa2871 2015-10-28 00:38
    关注

    WC()->cart->needs_shipping_address() is based on store settings and item settings...so not ideal in this case, it will trigger every time in most cases.

    Use the $instance data instead which is a url encoded string.

    1. Decode and parse the string then test for the ship_to_different_address option
    2. Suppress invalid array index errors when the option is not selected

    Here's your solution:

    parse_str(html_entity_decode($instance), $postData);
    if ( @$postData['ship_to_different_address']  ) {
            add_filter( 'woocommerce_package_rates', 'ddc_hide_shipping', 10, 2 );
    }
    

    Put this in your action hook.

    评论

报告相同问题?