我在检查购物车总数、购物车数量和运输类别时遇到一些问题。
我想要实现的目标
我试图根据其他特定运输类别的数量和购物车总数来隐藏特定的运输方式
free_shipping:7
。
我遇到的问题是,使用
||
(或)运算符后,我的条件逻辑将不起作用,如果我还检查数量,它不会检查购物车总数。
代码
/* Hide free shipping unless minimum of 6 bottle or 1 box is selected, or if 2 gins and under 600 cart total */
add_filter( 'woocommerce_package_rates', 'hide_free_shipping_method', 10, 2 );
function hide_free_shipping_method( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping class to check for
$class1 = 132; // shipping class for bottles
$class2 = 131; // shipping class for boxes
$class3 = 142; // shipping class for gin bottles
// Shipping method to hide
$ShipMethod_key_ids = array('free_shipping:7'); // free shipping
// Get cart total of items in the cart, but after discounts.
$cart_totals = WC()->cart->cart_contents_total;
// Keep track of the quantity of items with the specified shipping class
$quantity_bottle = 0;
$quantity_gin = 0;
$quantity_box = 0;
// Loop through cart content and add to specified shipping classes
if( $item['data']->get_shipping_class_id() == $class1 || $item['data']->get_shipping_class_id() == $class3){
$quantity_bottle += $item['quantity'];
$quantity_gin += $item['quantity'];
}
elseif ( $item['data']->get_shipping_class_id() == $class2 ) {
$quantity_box += $item['quantity'];
}
}
// If there are less than 6 bottles, 6 gins, and less than 1 box
// or
// if equal to 2 gins and less than 600 in Cart Total, hide the shipping method
if( $quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1 || $quantity_gin == 2 && $cart_totals < 600 ) {
foreach( $ShipMethod_key_ids as $method_key_id ){
unset($rates[$method_key_id]);
}
}
return $rates;
}
问题
我遇到的问题是,检查购物车总数的条件与
||
(或)运算符结合使用时不起作用。
if( $quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1 || $quantity_gin == 2 && $cart_totals < 600 ) {
如果我使用上面的组合示例,代码将忽略最后一个条件 (2)。
该条件仅适用于两者之一:
$quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1
$quantity_gin == 2 && $cart_totals < 600
我尝试过的
我尝试隔离这两个条件来检查它们是否有效。它们确实单独工作,但不能组合使用。
我也尝试过将它们写在单独的
if
语句中,但没有任何结果。
问题
有人可以向我解释一下我在这个例子中做错了什么吗?我该怎么做才能让它检查这两个语句?
我是否可以实现上述目标,而无需为每个逻辑实现两个不同的代码?
参考文献列表
所以主要问题是我的逻辑表达。当我们使用
||
(OR) 时,如果 $a or $b
为真,它将返回 true。而如果我们使用 &&
(AND),如果 $a and $b
都为 true,它将返回 true。
我还确保循环检查购物车商品的运输类别并单独添加到变量中。这使得我的第一个表达式无效
$quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1
,但我的第二个表达式$quantity_gin == 2 && $cart_totals < 600
需要它。为了使我的第一个表达式起作用,我添加了一个新变量来计算瓶子数量和杜松子酒瓶子数量总计。
// Loop through cart content and add to specified shipping classes
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class_id() == $class1 ){
$quantity_bottle += $item['quantity'];
}
if( $item['data']->get_shipping_class_id() == $class3){
$quantity_gin += $item['quantity'];
}
elseif ( $item['data']->get_shipping_class_id() == $class2 ) {
$quantity_box += $item['quantity'];
}
}
// Variable counting gin and other bottles
$total_bottle_quantity_cart = $quantity_gin + $quantity_bottle;
所以最终的条件是:
if (($quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1 && $total_bottle_quantity_cart < 6 ) && !($quantity_gin >= 2 && $subtotaltax > 600 ) )
完整代码如下:
/* Hide free shipping unless minimum of 6 bottle or 1 box is selected, or if 2 gins and under 600 cart total */
add_filter( 'woocommerce_package_rates', 'hide_free_shipping_method', 10, 2 );
function hide_free_shipping_method( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping class to check for
$class1 = 132; // shipping class for bottles
$class2 = 131; // shipping class for boxes
$class3 = 142; // shipping class for gin bottles
// Shipping method to hide
$ShipMethod_key_ids = array('free_shipping:7'); // free shipping
// Get cart subtotal of items in the cart incl. tax, as a number
$subtotal = WC()->cart->get_subtotal(); // Get cart subtotal value
$subtax = WC()->cart->get_subtotal_tax(); // Get cart subtotal value tax
$subtotaltax = $subtotal+$subtax; // calculate cart subtotal value with the tax added
// Keep track of the quantity of items with the specified shipping class
$quantity_bottle = 0;
$quantity_gin = 0;
$quantity_box = 0;
// Loop through cart content and add to specified shipping classes
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class_id() == $class1 ){
$quantity_bottle += $item['quantity'];
}
if( $item['data']->get_shipping_class_id() == $class3){
$quantity_gin += $item['quantity'];
}
elseif ( $item['data']->get_shipping_class_id() == $class2 ) {
$quantity_box += $item['quantity'];
}
}
// Variable counting gin and other bottles
$total_bottle_quantity_cart = $quantity_gin + $quantity_bottle;
// If there are less than 6 bottles, 6 gins, 1 box and less than 6 total bottles
// or
// if gin quantity is not bigger than or equal to 2 and subtotal less than 600
// hide the shipping method
if (($quantity_bottle < 6 && $quantity_gin < 6 && $quantity_box < 1 && $total_bottle_quantity_cart < 6 ) && !($quantity_gin >= 2 && $subtotaltax > 600 ) ) {
foreach( $ShipMethod_key_ids as $method_key_id ){
unset($rates[$method_key_id]);
}
}
return $rates;
}