Woocommerce运费计算超时
问题描述:
我创建了一个函数discount_foil_seal_items()
,它使用挂钩woocommerce_before_calculate_totals
应用购物车中的价格变化。它会更新购物车总计罚款,但当我得到结算运费计算超时(即永久旋转图标)。我一直无法找到与此特定问题有关的任何帖子。Woocommerce运费计算超时
我试着评论函数的各个部分,看看是什么导致它,似乎如果我运行这个函数中的任何一个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
更新
我试图重写代码的if else 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;
}
}
}
}
答
我发现它是我用于调试目的的echo声明的问题。我删除了它,它的工作。我没有意识到回声语句会导致这样的超时,但它是很好的了解:
我删除了这条线: echo 'discount_foil_seal_items() has run <br>';
在这两种功能,它的工作原理。