根据Woocommerce中的国家/地区禁用特定产品的送货

时间:2018-02-14 20:21:29

标签: php woocommerce cart product shipping

如果客户运送国家/地区不是意大利,我正尝试停用特定产品的运费

这是我的代码,但我不知道如何设置国家/地区条件:

function hide_shipping_when_class_is_in_cart( $rates, $package ) {
    // shipping class IDs that need the method removed
    $shipping_classes = array('bulky-items');
    $if_exists = false;

    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }

    if( $if_exists ) unset( $rates['free_shipping:7'] );

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );

如果选定的运输国家/地区不是意大利,如何禁用产品运费?

1 个答案:

答案 0 :(得分:2)

  

注意:此代码适用于Woocommerce 3+ (但不适用于非常旧的版本2.3)

您的问题不是那么明确......所以您主要有两个选项(并且检查两个选项的最后添加到购物车,当客户注册时,检测到运输国家/已经设置在购物车或结账时)

选项1 - 您应该更好地移除显示自定义通知的相关购物车项目,而不是删除不能在意大利以外的所有其他国家/地区发货的产品的送货方式......您必须定义功能中的产品ID只能在意大利发货:

add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );
function checking_and_removing_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Iterate through each cart item
    $found = false;
    foreach( $cart->get_cart() as $cart_item_key => $cart_item )
        if( in_array( $cart_item['data']->get_id(), $products_ids ) ){
            $found = true;
            $cart->remove_cart_item( $cart_item_key ); // remove item
        }

    if( $found ){
         // Custom notice
         wc_clear_notices();
         wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');
    }
}

代码放在活动子主题(活动主题)的function.php文件中。

  

添加到购物车验证功能结束...

选项2 - 删除对于除意大利以外的所有其他国家/地区无法发货并且显示自定义错误通知的产品的送货方式...您必须在功能中定义产品ID仅在意大利可以发货:

add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
    if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
        return $rates;

    // Only for NON Italians customers
    if( $package['destination']['country'] == 'IT' ) return $rates;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Loop through cart items and checking
    $found = false;
    foreach( $package['contents'] as $item )
        if( in_array( $item['data']->get_id(), $products_ids ) ){
            $found = true;
            break;
        }

    if( ! $found ) return $rates; // If nothing is found: We EXIT

    foreach( $rates as $rate_id => $rate )
        unset($rates[$rate_id]); // Removing all shipping methods

    // Custom notice
    wc_clear_notices();
    wc_add_notice('Some products are only shippable for Italy', 'error');

    return $rates;
}

代码放在活动子主题(活动主题)的function.php文件中。

使用自定义通知添加到购物车验证功能(对于这两个选项)。

您必须在函数中定义只能在意大利发货的产品ID。

add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );
function avoid_products_for_non_italian( $passed, $product_id, $quantity ) {

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return $passed;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return $passed;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // The condition
    if( in_array( $product_id, $products_ids ) ){
        $passed = false;
        wc_add_notice( 'This product is only shippable for Italy.', 'error' );
    }
    return $passed;
}

代码放在活动子主题(活动主题)的function.php文件中。

所有代码都经过测试,适用于Woocommerce版本3+ (也可能是2.6.x)