Woocommerce上的购物车中有特定产品时,从国家列表中删除加拿大 [英] Remove Canada from country list when specific products are in cart on Woocommerce

查看:63
本文介绍了Woocommerce上的购物车中有特定产品时,从国家列表中删除加拿大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做些简单的事情:不要将特定产品运往加拿大。



在我看来,这是最简单的方法:如果购物车中有该特定产品,请从结帐页面中删除加拿大。



特殊产品:

1-

解决方案

当特定产品在购物车中时,以下代码将从允许的国家/地区删除加拿大:

  add_filter('woocommerce_countries','products_disable_country',10,1); 
函数products_disable_country($ countries){
if(is_cart()|| is_checkout()){

$ products = array(15631,12616);

foreach(WC()-> cart-> get_cart()as $ item){
if(in_array($ item ['product_id'],$ products)){
未设置($ countries [ CA]);
个返回$个国家;
}
}
}

返回$国家;
}

代码进入活动子主题(活动主题)的function.php文件。经过测试和工作。


I want to do something simple: do not ship a particular product to Canada.

Here's the easiest way to do it in my opinion: if that particular product is present in the cart, remove Canada from checkout page.

Particular products:
1- https://constructioncovers.com/product/insulated-cement-curing-blankets/ (ID: 12616)
2- https://constructioncovers.com/product/insulated-construction-tarps/ (ID: 15631)

My research:
This article gave me a way to find products in cart and perform any action if condition is true: https://businessbloomer.com/woocommerce-easily-check-product-id-cart/ This article gave me a way to remove specific country from checkout page How to remove specific country in WooCommerce I have combined and modified the two codes to try and accomplish my task. Here's my code:

function unset_country_on_condition( $country ) {
    $product_id = 15631;
    $product_id_2 = 12616; 
    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $product_cart_id_2 = WC()->cart->generate_cart_id( $product_id_2 );
    $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
    $in_cart_2 = WC()->cart->find_product_in_cart( $product_cart_id_2 );
    if ( $in_cart || $in_cart_2 ) {
        unset($country["CA"]);
        return $country;
    }
}
add_filter( 'woocommerce_countries', 'unset_country_on_condition', 10, 1 );

But the above function doesn't work
. It makes the country dropdown empty resulting in a site-wide warning notice.

Can someone point out what I am doing wrong?

Screenshots:

解决方案

The following code will remove "Canada" from allowed countries when specific products are in cart:

add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {
    if( is_cart() || is_checkout() ) {

        $products = array(15631, 12616);

        foreach( WC()->cart->get_cart() as $item ){
            if( in_array( $item['product_id'], $products ) ){
                unset($countries["CA"]);
                return $countries;
            }
        }
    }

    return $countries;
}

Code goes in function.php file of your active child theme (active theme). tested and work.

这篇关于Woocommerce上的购物车中有特定产品时,从国家列表中删除加拿大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆