0

I have this weird situation with Woocommerce and WAS. I have a shipping method created using WAS, which my clients wants to be removed if "Ship to different address." So, I created a condition that did exactly that. Here is the source code:

function was_conditions_add_ship_to_different_address( $conditions ) {
    $conditions['General']['ship_to_different_address'] = __( 'Ship to different address', 'durhone-two' );
    return $conditions;
}

function was_values_add_ship_to_different_address( $values, $condition ) {
    if ( 'ship_to_different_address' === $condition ) {
        $values['field'] = 'select';
        $values['options'][0] = __( 'No' );
        $values['options'][1] = __( 'Yes' );
    }
    return $values;
}

function was_match_condition_ship_to_different_address( $match, $operator, $value, $package ) {
    $value_int = (int) $value;
    $ship_to_different_address = 0;

    if ( $_POST['post_data'] ) {
        parse_str( $_POST['post_data'], $post_data );
        if ( isset( $post_data['ship_to_different_address'] ) ) {
            $ship_to_different_address = (int) $post_data['ship_to_different_address'];
        }
    }

    $match = $value_int === $ship_to_different_address;

    return $match;
}

add_filter( 'was_conditions', 'was_conditions_add_ship_to_different_address', 10, 1 );
add_filter( 'was_values', 'was_values_add_ship_to_different_address', 10, 2 );
add_filter( 'was_match_condition_ship_to_different_address', 'was_match_condition_ship_to_different_address', 10, 4 );

The problem is that when I check the "ship to different address" checkbox, a request to /checkout?wc-ajax=update_order_review just like when I change the country field. However, was_match_condition_ship_to_different_address filter does not get called. I have added print statements inside the function and it gets printed when country changes but does not get printed when checkbox is selected. What can be the reason for that? Can this problem be solved?

closed as off-topic by Max Yudin, Dave Romsey, fuxia May 16 '17 at 12:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

Your code is good and shouldn't be the issue (as long as the shipping rate is active/published and has the condition).

I think its related to the shipping cache; WC doesn't re-calculate the shipping cost if nothing has changed for the destination (so the billing would be the same as the shipping address).

You can test this by enabling the Shipping debug mode under the 'Shipping' settings since WC 3.0, before that under System status > Tools.

If that works and it is calling the function now; you can resolve it completely by adding the value of the checkbox to the shipping package.

function custom_add_package_var( $package ) {
    $package['destination']['ship_to_diff_address'] = $my_var;
    return $package;
}
add_filter( 'woocommerce_cart_shipping_packages', 'custom_add_package_var' );

(non-functional code above; going from where you got so far I believe this would be enough for you to work with ;-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.