2017-03-16 100 views
1

如果產品是批量購買的(散裝產品在產品之間有所不同),我有一家商店可以打折。該部分已排序。修改WooCommerce購物車中的產品價格

目前,我使用負費這樣的應用的折扣:

function sale_custom_price($cart_object) { 
    foreach ($cart_object->cart_contents as $p) { 
    $prod_id = $p['product_id']; 
    $prod_n = $p['quantity']; 
    $prod_price = $p['data']->price; 
    $prod_name = $p['data']->post->post_title; 
    $prod_total = $p['line_total']; 

    /* 
    calc_discount: NULL for no discount for this product 
        else 
        array(
         n => how many are required, e.g. 12 
         free_pcs_deal => how many of these should be free, e.g. 1 
        ) 
    */ 
    $discount = calc_discount($prod_id, $prod_n); 

    if (is_null($discount)) { 
     continue; 
    } 

    $discount_n = $discount['n']; 
    $free_pcs_deal = $discount['free_pcs_deal']; 

    if ($discount_n <= 0) { 
     continue; 
    } 

    $discount_txt = $prod_name . ' (' . $discount_n . ' x ' . $free_pcs_deal . ' stk.)'; 
    $discount = -1 * $discount_n * $free_pcs_deal * $prod_price; 

    $cart_object->add_fee('Rabat: ' . $discount_txt, $discount, true, ''); 
    } 
} 

add_action('woocommerce_cart_calculate_fees', 'sale_custom_price', 2, 1); 

但不支持並且會在未來成爲泡影。相反,我想自動調整產品價格。

說一個顧客買了12件產品A.正常價格是100,但是產品A有大量的購買12,只支付11(買11買一免費)。所以目前的產品價格是12 * 100 = 1200,而負的費用是100.因此,所有12種產品的平均產品價格是11 * 100/12 = 91.67 A.我希望這可以用在我的購物車中。

但我不能讓它在購物車中顯示修改後的價格以及正確的line_total和line_subtotal和訂單總數。所以好像我還沒有找到合適的action/filter

我已經試過這樣的事情:

$cart = WC()->cart->cart_contents; 

foreach ($cart as $key => $p) { 
    $custom_price = 111; // of course replaced by logic calculating the new, modified price 
    WC()->cart->cart_contents[$key]['data']->price = $custom_price; 
} 

我已經試過這種邏輯在woocommerce_cart_updatedwoocommerce_before_cart_contentswoocommerce_add_to_cart和其他人,但它不會顯示出與更新的價格,line_total正確的購物車,訂單總額等

我可以通過woocommerce_cart_item_price鉤子計算並顯示更新後的價格,但這只是顯示器,它不在業務邏輯中,因此所有總數都會被修改。

我在做什麼錯?

回答

0

在計算總計之前嘗試執行價格自定義。您可以使用此鉤子「woocommerce_before_calculate_totals」並降低鉤子的優先級,以便任何其他插件/定製都不會覆蓋它。

下面是一段代碼,您可以根據需要定製:

function calculate_custom_price($cart_object) { 

    // Loop for all products in cart 
    foreach ($cart_object->cart_contents as $key => $value) { 

     if(isset($value['_custom_customized']) && !empty($value['_custom_customized']) && (!isset($isProcessed) || empty($isProcessed))) { 
      $fltCustomPrice = 0.00; 
      $quantity = intval($value['quantity']); 
      $orgPrice = floatval($value['data']->price); 

      // For all sub products of build your own basket 
      foreach($value['_custom_customized'] AS $keyInner => $arrValInner) { 
       $fltCustomPrice += floatval($arrValInner['price']) * floatval($arrValInner['quantity']); 
      } 
      $cart_object->cart_contents[$key]['data']->price = ($orgPrice + $fltCustomPrice); 
     } 
    } 
} 
add_action('woocommerce_before_calculate_totals', 'calculate_custom_price', 2, 1); 
+0

它做在車對象更新的價格,但不顯示購物車中的line_total等。 我已經使用這個: 功能calculate_custom_price($ cart_object){ 的foreach($ cart_object-> cart_contents爲$密鑰=> $值){$ cart_object-> cart_contents [$鍵] [ '數據'] - >價= 5; } echo'<! - '; print_r(WC() - > cart); echo' - >'; } add_action('woocommerce_before_calculate_totals','calculate_custom_price',2,1); 打印價格= 5,但不顯示購物車html頁面時。 所以看起來更新完成得太晚了。 –

0
 function on_display_cart_item_price_html($html, $cart_item, $cart_item_key) { 

      if (is_cart()) { 
       $price_adjusted = 10; // your adjustments here 
       $price_base = $cart_item['data']->sale_price; 
       if (!empty($price_adjusted)) { 
        if (intval($price_adjusted) > 0) { 
         $_qnty = $cart_item['quantity']; 
         $cart_item['data']->set_price($price_base - ($price_adjusted/$_qnty)); 
         $html = '<del>' . wc_price($price_base) . '</del><ins> ' . wc_price($price_base - ($price_adjusted/$_qnty)) . '</ins>'; 
        } else { 
         $html = '<span class="amount">' . wc_price($price_base) . '</span>'; 
        } 
       } 
      } 

      return $html; 
     } 


add_filter('woocommerce_cart_item_price', 'on_display_cart_item_price_html', 100, 3); 


    function change_woocommerce_cart_subtotal($cart_subtotal, $compound, $cart) { 
     $cart_subtotal = 5; 
     return $cart_subtotal; 
    }; 
    add_filter('woocommerce_cart_subtotal', 'change_woocommerce_cart_subtotal', 10, 3); 


    // define the woocommerce_order_amount_total callback 
    function change_woocommerce_order_amount_total($order_total) { 

     $order_total = 5; 
     return $order_total; 
    }; 
    add_filter('woocommerce_cart_totals_order_total_html', 'change_woocommerce_order_amount_total'); 

在活動主題的functions.php中嘗試這個代碼片斷

+0

這隻會改變打印的html,而不是購物車業務邏輯中的價格。 –

+0

@PaulJenkins:請檢查更新的答案 –

+0

謝謝,但是我覆蓋了所有的邏輯(費用,運費,稅收,...)。因此,我需要重新實現它,或者在會話中保存摺扣,並減去從提供的總數作爲參數的功能。這必然會帶來錯誤。因此,最好的(最容錯的)方法是在工作流程的早期改變產品價格,並讓WooC引擎像往常一樣進行。 –

相關問題