2017-07-06 73 views
1

我想將購物車中商品的自定義價格更改爲特定類別('t-shirts-d','socks-d','joggers-d ','boxers-d'),因爲每種產品共享2種不同的類別。將特定類別的WooCommerce購物車商品價格更改爲

我試過這樣做,但它的工作,但自定義價格也影響其他類別,我只想顯示其他類別的原始價格('T恤','襪子','慢跑者',拳擊手「)。

我需要幫助。

這裏是我到目前爲止的代碼:

function changeprice($html, $cart_item, $cart_item_key){ 
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 
     //$thepro = $woocommerce->cart->get_cart(); 
$product = $cart_item['data']; 
$heading_nicename = array('t-shirts-d','socks-d','joggers-d','boxers-d'); 
//$heading_nicename1 = array('t-shirts','socks','joggers','boxers'); 
    $termsother = $heading_nicename; 
foreach($termsother as $termsnew) { 
    if (is_cart()) { 
      $price_adjusted = 666.666666667; // your adjustments here 
      $price_base = $cart_item['data']->sale_price; 
      if (!empty($price_adjusted)) { 
       if (intval($price_adjusted) > 0) { 
        $cart_item['data']->set_price($price_adjusted); 
       } /*else { 
        $html = '<span class="amount">' . wc_price($price_base) 
    . '</span>'; 
       }*/ 
      } 
     } 
    } 
    } 
    } 
    add_filter('woocommerce_cart_item_price', 'changeprice', 100, 3); 

回答

1

權問題的工作HOOK (更新)

add_filter('woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1); 
function change_cart_items_prices($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    foreach ($cart_object->get_cart() as $cart_item) { 
     if(has_term(array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'])){ 
      $price_adjusted = 666.666666667; // your adjustments here 
      if (! empty($price_adjusted) && intval($price_adjusted) > 0) { 
       $cart_item['data']->set_price($price_adjusted); 
      } 
     } 
    } 
} 

代碼放在您的活動子主題的function.php文件(或主題)或任何插件文件。

這次,所有的東西都在購物車和結帳頁面上運行。 總計也更新

+0

謝謝這麼多,但代碼只適用於6種產品中的一種產品。它顯示購物車中的第二個產品的自定義價格,但不是其他的。 – SandeepTete

+0

看起來價格只會在簡單產品上發生變化,而不會在變量產品上發生變化。我嘗試了if is_type =變量但不工作 – SandeepTete

+0

@SandeepTete所以這個更新版本爲你工作... – LoicTheAztec

相關問題