Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce配送计算超时_Php_Wordpress_Foreach_Woocommerce_Hook - Fatal编程技术网

Php Woocommerce配送计算超时

Php Woocommerce配送计算超时,php,wordpress,foreach,woocommerce,hook,Php,Wordpress,Foreach,Woocommerce,Hook,我已经创建了一个函数折扣\u foil\u seal\u items(),它在计算总计之前,使用钩子woocommerce\u在购物车中应用价格变化。它会很好地更新购物车总数,但当我结帐时,运费计算超时(即永远旋转图标)。我还没有找到任何关于这个具体问题的帖子 我试着注释掉函数的各个部分,看看是什么导致了它,似乎如果我在这个函数中运行任何一个foreach,它都会超时。这与在foreach中嵌套if语句或switch语句有关吗 //apply discounts to foil and seal

我已经创建了一个函数
折扣\u foil\u seal\u items()
,它在计算总计之前,使用钩子
woocommerce\u在购物车中应用价格变化。它会很好地更新购物车总数,但当我结帐时,运费计算超时(即永远旋转图标)。我还没有找到任何关于这个具体问题的帖子

我试着注释掉函数的各个部分,看看是什么导致了它,似乎如果我在这个函数中运行任何一个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;
        }            
    } 
    
 }
此站点上的其他产品无超时错误

任何关于导致超时的原因的帮助都将不胜感激

以下是超时的屏幕截图:

更新 我尝试重写代码,用if-else-if块替换switch语句,但没有成功。然后,我尝试将函数拆分为两个单独的函数,只是想看看这是否能解决超时问题。以下代码分为两个不同的函数:

//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;
        }            
    }      
 }

}
//对箔材和密封产品类别应用折扣
添加行动(“计算总计前的商业”、“购物车计数”、“铝箔封口”项目),10,1);
功能车\计数\铝箔\封条\物品($cart\对象){
echo“折扣箔封项目()已运行
”; $seal_prod_tally=0; $foil_prod_tally=0; //遍历购物车中的每个项目 foreach($cart\u object->get\u cart()作为$item\u值){ //获取购物车项目数据 $item_id=$item_值['data']->get_id();//产品id $item_数量=$item_值['quantity'];//项目数量 //获取对象 $product=新的WC\U产品($item\U id); $prod_cat=wp_get_post_terms($product->get_id(),'product_cat',数组('fields'=>'slugs'); //理货总额 if(在_阵列中(‘密封标签’,$prod_cat)){ $seal\U prod\U tally+=$item\U数量; }else if(在_数组中($foil-徽章,$prod_cat)){ $foil\U prod\U tally+=$item\U数量; } } 返回数组($seal\U prod\U tally,$foil\U prod\U tally); } //对箔材和密封产品类别应用折扣 添加行动(“计算总额前的商业”、“折扣、包装、封存项目”,12); 功能折扣\铝箔\封条\物品($cart\对象){ echo“折扣箔封项目()已运行
”; 列表($seal\u prod\u taly,$foil\u prod\u taly)=购物车计数\u foil\u seal\u物品($cart\u对象); foreach($cart\u object->get\u cart()作为$item\u值){ //获取购物车项目数据 $item_id=$item_值['data']->get_id();//产品id $item_数量=$item_值['quantity'];//项目数量 //获取对象 $product=新的WC\U产品($item\U id); $prod_cat2=wp_get_post_terms($product->get_id(),'product_cat',数组('fields'=>'slugs'); //对类别中的每个项目应用折扣 if(在_阵列中(‘密封标签’,$prod_cat2)){ 开关($seal\U prod\U TALL){ 案例25000: $item_值['data']->设定价格(1327.01/20000); 打破 案例30000: $item_值['data']->设定价格(1578.65/30000); 打破 案例50000: $item_值['data']->设定价格(2126.76/50000); 打破 案例60000: $item_值['data']->设定价格(2405.98/60000); 打破 违约: 打破 } }else if(在_数组中('foil徽章',$prod_cat2)){ 开关($foil\U prod\U tally){ 案例25000: $item_值['data']->设定价格(5872.63/25000); 打破 案例50000: $item_值['data']->设定价格(10815.47/50000); 打破 违约: 打破 } } } }
我发现了问题,这是一个用于调试的echo语句。我去掉了它,它就起作用了。我不知道echo语句会导致这样的超时,但很高兴知道:

我删除了这一行:
echo“折扣锡箔封口项目()已运行”


在这两个函数中,它都能正常工作。

我发现问题在于,它是一个用于调试目的的echo语句。我去掉了它,它就起作用了。我不知道echo语句会导致这样的超时,但很高兴知道:

我删除了这一行:
echo“折扣锡箔封口项目()已运行”

在这两个功能和它的工作

//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;
        }            
    }      
 }

}