我创建了一个函数discount_foil_seal_items()
,它使用钩子woocommerce_before_calculate_totals
在购物车中应用价格变化。它会更新购物车总数,但是当我结账时运费计算超时(即永久旋转图标)。我无法找到与此特定问题相关的任何帖子。
我尝试评论函数的各个部分以查看导致它的原因,并且似乎如果我在此函数中运行foreach中的任何一个,它就会超时。是否与在foreach中嵌套if语句或switch语句有关?
//apply discounts to foil and seal product categories
add_action( 'woocommerce_before_calculate_totals', 'discount_foil_seal_items', 10, 1 );
function discount_foil_seal_items( $cart_object ) {
// Iterating through each item in cart
foreach ( $cart_object->get_cart() as $item_values ) {
// Get cart item data
$item_id = $item_values['data']->id; // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Get the object
$product = new WC_Product( $item_id );
$prod_cat = wp_get_post_terms($product->id,'product_cat',array('fields'=>'slugs'));
//tally totals
if (in_array('seal-stickers', $prod_cat)){
$seal_prod_tally += $item_qty;
}else if(in_array('foil-badges', $prod_cat)){
$foil_prod_tally += $item_qty;
}
}
foreach ( $cart_object->get_cart() as $item_values ) {
//Get cart item data
$item_id = $item_values['data']->id; // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Get the object
$product = new WC_Product( $item_id );
$prod_cat2 = wp_get_post_terms($product->id,'product_cat',array('fields'=>'slugs'));
//apply discount to each item within category
if (in_array('seal-stickers',$prod_cat2)){
switch ($seal_prod_tally){
case 25000:
$item_values['data']->set_price(1327.01/20000);
echo 'case 25000 seal has run <br>';
break;
case 30000:
$item_values['data']->set_price(1578.65/30000);
break;
case 50000:
$item_values['data']->set_price(2126.76/50000);
break;
case 60000:
$item_values['data']->set_price(2405.98/60000);
break;
}
}else if (in_array( 'foil-badges',$prod_cat2)){
switch ($foil_prod_tally){
case 25000:
$item_values['data']->set_price(5872.63/25000);
break;
case 50000:
$item_values['data']->set_price(10815.47/50000);
break;
}
}
}
}
此外,我已经注释掉了foreach并插入了一个简化的foreach,如下所示,运行良好:
//this foreach doesn't timeout
foreach ( $cart_object->get_cart() as $item_values ){
$item_values['data']->set_price(1327.01/20000);
}
此网站上的其他产品结帐时没有超时错误。
非常感谢任何导致超时的帮助!
以下是超时的屏幕截图:
https://i.gyazo.com/f5869d0b7b01e4e73ff27885e5b18208.png
我尝试重写代码替换switch语句,if else if if if无效。然后我尝试将这些函数拆分成两个独立的函数,看看是否做了什么也没有修复超时。这里的代码分为两个不同的函数:
//apply discounts to foil and seal product categories
add_action( 'woocommerce_before_calculate_totals', 'cart_count_foil_seal_items',10,1);
function cart_count_foil_seal_items( $cart_object ) {
echo 'discount_foil_seal_items() has run <br>';
$seal_prod_tally = 0;
$foil_prod_tally = 0;
// Iterating through each item in cart
foreach ( $cart_object->get_cart() as $item_values ) {
// Get cart item data
$item_id = $item_values['data']->get_id(); // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Getting the object
$product = new WC_Product( $item_id );
$prod_cat = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));
//tally total
if (in_array('seal-stickers', $prod_cat)){
$seal_prod_tally += $item_qty;
}else if(in_array('foil-badges', $prod_cat)){
$foil_prod_tally += $item_qty;
}
}
return array($seal_prod_tally,$foil_prod_tally);
}
//apply discounts to foil and seal product categories
add_action( 'woocommerce_before_calculate_totals', 'discount_foil_seal_items',12);
function discount_foil_seal_items( $cart_object ) {
echo 'discount_foil_seal_items() has run <br>';
list($seal_prod_tally, $foil_prod_tally) = cart_count_foil_seal_items($cart_object);
foreach ( $cart_object->get_cart() as $item_values ) {
//Get cart item data
$item_id = $item_values['data']->get_id(); // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Getting the object
$product = new WC_Product( $item_id );
$prod_cat2 = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));
//apply discount to each item within category
if (in_array('seal-stickers',$prod_cat2)){
switch ($seal_prod_tally){
case 25000:
$item_values['data']->set_price(1327.01/20000);
break;
case 30000:
$item_values['data']->set_price(1578.65/30000);
break;
case 50000:
$item_values['data']->set_price(2126.76/50000);
break;
case 60000:
$item_values['data']->set_price(2405.98/60000);
break;
default:
break;
}
}else if (in_array( 'foil-badges',$prod_cat2)){
switch ($foil_prod_tally){
case 25000:
$item_values['data']->set_price(5872.63/25000);
break;
case 50000:
$item_values['data']->set_price(10815.47/50000);
break;
default:
break;
}
}
}
}
答案 0 :(得分:0)
我发现问题是我用于调试目的的echo语句。我删除了它,它工作。我没有意识到回声声明会导致这样的超时但是很有必要知道:
我删除了这一行:
echo 'discount_foil_seal_items() has run <br>';
在两个功能中都有效。