2017-06-01 84 views
1

我創建了一個函數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; 
     }    
    }  
} 

} 

回答

0

我發現它是我用於調試目的的echo聲明的問題。我刪除了它,它的工作。我沒有意識到回聲語句會導致這樣的超時,但它是很好的瞭解:

我刪除了這條線: echo 'discount_foil_seal_items() has run <br>';

在這兩種功能,它的工作原理。