WooCommerce:自定义功能添加免费产品 - 订单高达 3000 的购物车页面被破坏
WooCommerce: Custom function adding free product - Cart page getting broken for orders up to 3000
只要我将产品添加到购物车,WooCommerce 购物车页面就会中断如果购物车的价值超过 3000。
下面是我在我的主题 functions.php
中使用的代码:
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
// Cart's Total Excluding Taxes
function qualifies_for_incentive() {
// Incentive product we are giving away
$incentive_product_id = 6971;
if( qualifies_basedon_cart_total( 250 ) ) {
add_incentive_to_cart( $incentive_product_id );
} else {
remove_incentive_from_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
}
/**
* Adds a specific product to the cart
* @param $product_id Product to be added to the cart
*/
function add_incentive_to_cart( $product_id ) {
// Check the cart for this product
$cart_id = WC()->cart->generate_cart_id( $product_id );
$prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
// Add the product only if it's not in the cart already
if( qualifies_basedon_cart_total( 250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,1 );
} else {
remove_incentive_from_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,2 );
}
if( qualifies_basedon_cart_total( 750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,3 );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,4 );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,5 );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,6 );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,7 );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,8 );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,9 );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,10 );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,11 );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id, 12);
}
if( qualifies_basedon_cart_total( 3250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,13 );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,14 );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,15 );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,16 );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,17 );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,18 );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,19 );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,20 );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,21 );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,22 );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,23 );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,24 );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,25 );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,26 );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,27 );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,28 );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,29 );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,30 );
}
if( qualifies_basedon_cart_total( 7750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,31 );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,32 );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,33 );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,34 );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,35 );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,36 );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,37 );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,38 );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,39 );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,40 );
}
}
/**
* Removes a specific product from the cart
* @param $product_id Product ID to be removed from the cart
*/
function remove_incentive_from_cart( $product_id ) {
$prod_unique_id = WC()->cart->generate_cart_id( $product_id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
}
/**
* Checks the cart for the Total excluding taxes
* @param $total_required
* @return bool
*/
function qualifies_basedon_cart_total( $total_required ) {
/*
* We only want to run this on the cart or checkout page
*/
if( is_cart() || is_checkout () ) {
if( WC()->cart->subtotal >= $total_required ) {
return true;
}
}
// Return false in case anything fails
//return false;
}
我需要有关此问题的帮助。我做错了什么?
谢谢。
缩短重复代码 带有 PHP FOR 循环的代码:
这并没有直接回答您的问题,但是您的代码很长而且多余。使用 简单的 php for loop,您可以 大大缩短 前两个功能...
您的代码似乎部分来自此线程:
Add a product to woocommerce cart if other cart items are in a specific category
更新:
然后我 MERGED 你的第一个钩子函数 qualifies_for_incentive()
中的所有代码。
因此下面的代码与您的所有代码(20 行无注释)完全相同:
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() { // Cart's Total Excluding Taxes
if( ( is_cart() || is_checkout () ) {
$free_product = 6971; // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
$cart_amount = WC()->cart->subtotal;
if( $cart_amount < 250 ) {
// removing free product by unsetting it
unset( WC()->cart->cart_contents[$cart_id] );
}
// The shortening "FOR LOOP":
// $amount goes by step of 250 until 10000 while
// $number is increased by 1 each step until 40.
for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
if( ( $cart_amount >= $amount ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart a $number of free products
WC()->cart->add_to_cart( $free_product, $number );
}
}
}
}
这样可以避免在复习时迷失方向。
See it visually in action here.
这是通过回显 循环内的内容 与 php 文件生成的代码:
<html><head><title>The FOR loop in action (PHP)</title></head>
<body>
<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
<div>
<code><?php
for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
echo '
if( $cart_amount >= $amount ) {
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, '.$number.' ); <br>
} <br>';
}
?></code>
</div>
<p><br> - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - <br></p>
</body>
</html>
之后我将检查您的代码以了解您遇到此问题的原因...
Update2: 发现问题了,我觉得
说明:用你的代码,最高是购物车总金额,更多的购物车是通过多次重复删除和免费添加项目。所以当他赚到3000的时候,就破了……
为避免此问题,您需要在每个 if
语句中定义 "in between"
金额而不是仅 最小 数量。
这是我的新压缩代码(只有24行没有注释):
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() { // Cart's Total Excluding Taxes
if( ( is_cart() || is_checkout () ) {
$free_product = 6971; // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
$cart_amount = WC()->cart->subtotal; // cart subtotal
// Cart subtotal is less than 250
if( $cart_amount < 250 ) {
// removing free product by unsetting it
unset( WC()->cart->cart_contents[$cart_id] );
}
// Cart amount is up to 249.
// The min amount $i begins at 250 and goes by step of 250 until 9750 while
// the max amount $j begins at 500 and goes by step of 250 until 10000 while
// the quantity of free products $k increases by 1 each step until it reaches 39.
for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){
// NOW THERE WILL BE NO MULTIPLE ADDINGS AND REMOVINGS <=========== @@@
// Dedfined "in between" values to avoid this issue.
// For each step of 250 amount…
if( $cart_amount >= $i && $cart_amount < $j ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart a number $k of free products
WC()->cart->add_to_cart( $free_product, $k );
}
}
// For cart amount up to 10000 The quantity is fixed: 40 free products
if( $cart_amount >= 10000 ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart 40 free products
WC()->cart->add_to_cart( $free_product, 40 );
}
}
}
这是我的代码所做的 visually in action here from the for loop。
这是通过回显 循环内的内容 与 php 文件生成的代码:
<html><head><title>The FOR loop in action (PHP)</title></head>
<body>
<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
<div>
<code><?php
for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){
echo '
if( $cart_amount >= '.$i.' && $cart_amout < ( '.$j.' ) { <br>
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, '.$k.' ); <br>
} <br>';
}
echo '
if( $cart_amount >= 10000 ) { <br>
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, 40 ); <br>
} <br>';
?></code>
</div>
<p><br> - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - <br></p>
</body>
</html>
只要我将产品添加到购物车,WooCommerce 购物车页面就会中断如果购物车的价值超过 3000。
下面是我在我的主题 functions.php
中使用的代码:
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
// Cart's Total Excluding Taxes
function qualifies_for_incentive() {
// Incentive product we are giving away
$incentive_product_id = 6971;
if( qualifies_basedon_cart_total( 250 ) ) {
add_incentive_to_cart( $incentive_product_id );
} else {
remove_incentive_from_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
}
/**
* Adds a specific product to the cart
* @param $product_id Product to be added to the cart
*/
function add_incentive_to_cart( $product_id ) {
// Check the cart for this product
$cart_id = WC()->cart->generate_cart_id( $product_id );
$prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
// Add the product only if it's not in the cart already
if( qualifies_basedon_cart_total( 250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,1 );
} else {
remove_incentive_from_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,2 );
}
if( qualifies_basedon_cart_total( 750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,3 );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,4 );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,5 );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,6 );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,7 );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,8 );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,9 );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,10 );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,11 );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id, 12);
}
if( qualifies_basedon_cart_total( 3250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,13 );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,14 );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,15 );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,16 );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,17 );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,18 );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,19 );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,20 );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,21 );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,22 );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,23 );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,24 );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,25 );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,26 );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,27 );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,28 );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,29 );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,30 );
}
if( qualifies_basedon_cart_total( 7750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,31 );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,32 );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,33 );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,34 );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,35 );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,36 );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,37 );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,38 );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,39 );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,40 );
}
}
/**
* Removes a specific product from the cart
* @param $product_id Product ID to be removed from the cart
*/
function remove_incentive_from_cart( $product_id ) {
$prod_unique_id = WC()->cart->generate_cart_id( $product_id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
}
/**
* Checks the cart for the Total excluding taxes
* @param $total_required
* @return bool
*/
function qualifies_basedon_cart_total( $total_required ) {
/*
* We only want to run this on the cart or checkout page
*/
if( is_cart() || is_checkout () ) {
if( WC()->cart->subtotal >= $total_required ) {
return true;
}
}
// Return false in case anything fails
//return false;
}
我需要有关此问题的帮助。我做错了什么?
谢谢。
缩短重复代码 带有 PHP FOR 循环的代码:
这并没有直接回答您的问题,但是您的代码很长而且多余。使用 简单的 php for loop,您可以 大大缩短 前两个功能...
您的代码似乎部分来自此线程:
Add a product to woocommerce cart if other cart items are in a specific category
更新:
然后我 MERGED 你的第一个钩子函数 qualifies_for_incentive()
中的所有代码。
因此下面的代码与您的所有代码(20 行无注释)完全相同:
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() { // Cart's Total Excluding Taxes
if( ( is_cart() || is_checkout () ) {
$free_product = 6971; // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
$cart_amount = WC()->cart->subtotal;
if( $cart_amount < 250 ) {
// removing free product by unsetting it
unset( WC()->cart->cart_contents[$cart_id] );
}
// The shortening "FOR LOOP":
// $amount goes by step of 250 until 10000 while
// $number is increased by 1 each step until 40.
for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
if( ( $cart_amount >= $amount ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart a $number of free products
WC()->cart->add_to_cart( $free_product, $number );
}
}
}
}
这样可以避免在复习时迷失方向。
See it visually in action here.
这是通过回显 循环内的内容 与 php 文件生成的代码:
<html><head><title>The FOR loop in action (PHP)</title></head>
<body>
<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
<div>
<code><?php
for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
echo '
if( $cart_amount >= $amount ) {
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, '.$number.' ); <br>
} <br>';
}
?></code>
</div>
<p><br> - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - <br></p>
</body>
</html>
之后我将检查您的代码以了解您遇到此问题的原因...
Update2: 发现问题了,我觉得
说明:用你的代码,最高是购物车总金额,更多的购物车是通过多次重复删除和免费添加项目。所以当他赚到3000的时候,就破了……
为避免此问题,您需要在每个 if
语句中定义 "in between"
金额而不是仅 最小 数量。
这是我的新压缩代码(只有24行没有注释):
add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() { // Cart's Total Excluding Taxes
if( ( is_cart() || is_checkout () ) {
$free_product = 6971; // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
$cart_amount = WC()->cart->subtotal; // cart subtotal
// Cart subtotal is less than 250
if( $cart_amount < 250 ) {
// removing free product by unsetting it
unset( WC()->cart->cart_contents[$cart_id] );
}
// Cart amount is up to 249.
// The min amount $i begins at 250 and goes by step of 250 until 9750 while
// the max amount $j begins at 500 and goes by step of 250 until 10000 while
// the quantity of free products $k increases by 1 each step until it reaches 39.
for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){
// NOW THERE WILL BE NO MULTIPLE ADDINGS AND REMOVINGS <=========== @@@
// Dedfined "in between" values to avoid this issue.
// For each step of 250 amount…
if( $cart_amount >= $i && $cart_amount < $j ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart a number $k of free products
WC()->cart->add_to_cart( $free_product, $k );
}
}
// For cart amount up to 10000 The quantity is fixed: 40 free products
if( $cart_amount >= 10000 ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart 40 free products
WC()->cart->add_to_cart( $free_product, 40 );
}
}
}
这是我的代码所做的 visually in action here from the for loop。
这是通过回显 循环内的内容 与 php 文件生成的代码:
<html><head><title>The FOR loop in action (PHP)</title></head>
<body>
<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
<div>
<code><?php
for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){
echo '
if( $cart_amount >= '.$i.' && $cart_amout < ( '.$j.' ) { <br>
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, '.$k.' ); <br>
} <br>';
}
echo '
if( $cart_amount >= 10000 ) { <br>
WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
WC()->cart->add_to_cart( $free_product, 40 ); <br>
} <br>';
?></code>
</div>
<p><br> - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - <br></p>
</body>
</html>