woocommerce exclude state free shipping (australian states)

我只是一个虾纸丫 提交于 2019-12-11 10:03:46

问题


I'm trying to implement woocommerces exclude states from free shipping (Australian Site) snippet but am running into this error:

'Fatal error: Call to a member function get_shipping_state() on a non-object in /home/bpcsport/public_html/wp-content/plugins/wa-exclude/wa-exclude.php on line 30'

My client needs to exclude Western Australia from the free-shipping deal they offer through woocommerce. I get the error either way if I put it in my themes function.php or via plug-in format.

I've also tried the following method to no avail.

How can I disable free shipping for particular states?

This is the snippet from woocommerce that is in the plug-in

/**
 * Hide ALL shipping options when free shipping is available and customer is NOT in certain states
 * Hide Free Shipping if customer IS in those states
 *
 * Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping
 */

add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );

/**
* Hide ALL Shipping option when free shipping is available
*
* @param array $available_methods
*/
function hide_all_shipping_when_free_is_available( $available_methods ) {

  $excluded_states = array( 'WA' );

    if( isset( $available_methods['free_shipping'] ) AND !in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) :

        // Get Free Shipping array into a new array
        $freeshipping = array();
        $freeshipping = $available_methods['free_shipping'];

        // Empty the $available_methods array
        unset( $available_methods );

        // Add Free Shipping back into $avaialble_methods
        $available_methods = array();
        $available_methods[] = $freeshipping;

    endif;

    if( isset( $available_methods['free_shipping'] ) AND in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) {

        // remove free shipping option
        unset( $available_methods['free_shipping'] );

    }

    return $available_methods;
}


?>

回答1:


I've made an plugin which allow advanced user configurations. Altough it includes only US states, you can configure country and postal code conditions.

I don't know if this meets you requirements, but might worth checking out: http://wordpress.org/plugins/woocommerce-advanced-free-shipping/



来源:https://stackoverflow.com/questions/19379757/woocommerce-exclude-state-free-shipping-australian-states

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!