我想做些简单的事情:不要将特定产品运往加拿大。
在我看来,这是最简单的方法:如果购物车中有该特定产品,请从结帐页面中删除Canada。
特殊产品:
1- https://constructioncovers.com/product/insulated-cement-curing-blankets/(ID:12616)
2- https://constructioncovers.com/product/insulated-construction-tarps/(ID:15631)
我的研究
本文为我提供了一种在条件为真的情况下在购物车中查找产品并执行任何操作的方法:https://businessbloomer.com/woocommerce-easily-check-product-id-cart/本文为我提供了一种从结帐页面How to remove specific country in WooCommerce删除特定国家/地区的方法,我将两者合并并修改代码来尝试完成我的任务。这是我的代码:
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 );
但是上述功能不起作用
。它会使国家/地区下拉列表为空,从而导致在整个站点范围内发出警告通知。
有人可以指出我做错了什么吗?
答案 0 :(得分:1)
由this answer编辑
add_filter( 'woocommerce_countries', 'unset_country_on_condition', 10, 1 );
function unset_country_on_condition( $countries ) {
// Set here your to add product IDS (in the array)
$product_ids = array( 15631, 12616 );
$is_in_cart = false;
// Iterating through cart items and check
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
if( in_array( $cart_item['data']->get_id(), $product_ids ) ){
$is_in_cart = true; // We set it to "true"
break; // At least one product, we stop the loop
}
if( $is_in_cart ){
unset($countries["CA"]);
}
return $countries;
}
答案 1 :(得分:1)
当特定产品在购物车中时,以下代码将从允许的国家/地区删除“加拿大”:
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;
}
代码进入您的活动子主题(活动主题)的function.php文件中。经过测试和工作。
答案 2 :(得分:0)
您可以尝试以下代码吗?
global $product_check; //declare a global variable
function remove_canada_insulated_blankets_traps(){
$product_ids = array(12616,15631);
foreach($product_ids as $product_id){
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
$product_check[$in_cart]; //global variable is converted into an array storing ids
}
}
add_action( 'woocommerce_before_cart', 'remove_canada_insulated_blankets_traps' );
//pass the global variable into following function as another argument
function woo_remove_canada_country( $countries, $product_check ){
if( count(product_check) > 0 ){ //check count of the array
unset($countries['CA']); //unset country
}
return $countries;
}
add_filter( 'woocommerce_countries', 'woo_remove_canada_country', 10, 2 );
请注意“ woocommerce_countries”过滤器的最后一个参数的更改。由于将附加参数传递给函数,因此最后一个参数更改为2而不是1。
答案 3 :(得分:0)
不幸的是,从WooCommerce 3.6.2
开始,他们已经取消了在任何后端调用according to the ticket I opened中检查购物车的功能。我在woocommerce_countries
上运行了类似的过滤器,以限制购物车中的某些物品只能在某些位置购买,而几天前更新后出现了致命错误。
function vnm_disable_country_for_products($countries) {
// get_cart() will fail here in WC > 3.6.2, but we can still access a basic cart via the session:
$cartArray = WC()->session->cart;
if (is_array($cartArray) && !empty($cartArray)) {
$noSaleCountry = 'CA';
$limitingProducts = array(12616, 15631);
foreach ($cartArray as $itemArray) {
if (in_array($itemArray['product_id'], $limitingProducts)) {
unset($countries[$noSaleCountry]);
}
}
return $countries;
}
add_filter('woocommerce_countries', 'vnm_disable_country_for_products', 10, 1);
表面上与Loic's answer相同;但是,由于WC()->cart->get_cart()
不再可用,我们改为通过WC()->session->cart
访问购物车。
答案 4 :(得分:0)
由于woocommerce API的更改,因此需要进行一些更改。 https://github.com/woocommerce/woocommerce/issues/23758
该购物车无法像以前一样提供。
add_filter('woocommerce_countries', 'products_disable_country', 10, 1);
function products_disable_country( $countries ) {
global $woocommerce;
// is_cart & is_checkout does not to be working anymore.
// if( is_cart() || is_checkout() ) {
$products = array(15631, 12616);
// but you can get the cart via the session
$cart = $woocommerce->session->cart;
foreach( $cart as $item ){
if( in_array( $item['product_id'], $products ) ){
unset($countries["CA"]);
return $countries;
}
}
// }
return $countries;
}