首页 文章

在WooCommerce购物车中修改产品价格

提问于
浏览
2

我有一个商店,如果批量购买产品,我们会给予折扣(产品的批量报价不同) . 那部分是分类的 .

目前,我使用这样的负面费用来申请折扣:

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种产品A的平均产品价格为11 * 100/12 = 91.67 . 我希望在我的购物车中使用它 .

但是我无法让它在购物车中显示修改后的价格以及正确的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 钩子计算并显示更新的价格,但这只是显示,它不在业务逻辑中,因此所有总计都被修改 .

我究竟做错了什么?

2 回答

  • 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
    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中尝试此代码段

相关问题