首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Woocommerce中根据国家禁用特定产品的运输

在Woocommerce中根据国家禁用特定产品的运输
EN

Stack Overflow用户
提问于 2018-02-14 20:21:28
回答 1查看 2.8K关注 0票数 2

如果客户发货国不是意大利,我正试图禁用某一特定产品的运输。

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

代码语言:javascript
运行
复制
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 );

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

EN

回答 1

Stack Overflow用户

发布于 2018-02-15 04:35:28

备注:此代码用于Woocommerce 3+ (但不是很旧的版本2.3)。

你的问题不太清楚,…因此,您主要有两个选项(并在两个选项的末尾检查add to cart,当客户注册时,当检测到运输国家或已在cart或结帐中设置时)

选项1 --与其删除意大利以外所有国家都无法运输的产品的运输方法,不如删除显示自定义通知…的相关购物车项目。您必须在该函数中定义仅在意大利可移植的产品Ids:

代码语言:javascript
运行
复制
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 -删除除意大利以外所有其他国家不可运输的产品的运输方法,并显示自定义错误通知…。您必须在该函数中定义仅在意大利可移植的产品Ids:

代码语言:javascript
运行
复制
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文件中。

添加到购物车验证功能与自定义通知(两个选项)。

您必须在函数中定义仅在意大利可移植的产品Ids。

代码语言:javascript
运行
复制
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 )

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48795558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档