【问题标题】:Disable shipping for specific products based on country in Woocommerce在 Woocommerce 中根据国家/地区禁用特定产品的运输
【发布时间】:2018-07-25 12:29:14
【问题描述】:

如果客户发货国家/地区不是意大利,我正在尝试禁用特定产品的发货

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

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 );

如果所选的发货国家/地区不是意大利,我如何禁用产品的发货?

【问题讨论】:

    标签: php woocommerce cart product shipping


    【解决方案1】:

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

    你的问题不是很清楚......所以你主要有 2 个选项 (并在最后检查添加到购物车这两个选项,当客户注册时,当检测到送货国家或已在购物车或结帐时设置):

    选项 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 - 删除不适用于意大利以外的所有其他国家/地区的产品的运输方式并显示自定义错误通知...您必须在函数中定义仅适用于可在意大利发货:

    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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2023-04-01
      相关资源
      最近更新 更多