Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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 价格修改适用于购物车,但不适用于订单_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 价格修改适用于购物车,但不适用于订单

Php 价格修改适用于购物车,但不适用于订单,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在编写一个函数来修改价格。 根据某些类别内产品的总价格,将应用返利(-1至-4%) 该功能适用于购物车和迷你购物车,直至“订单”页面。但折扣不适用于实际订单 // hook : add_action( 'woocommerce_before_calculate_totals', array($this,'action_woocommerce_before_calculate_totals'), 10 ); // function function action_woocommerce_b

我正在编写一个函数来修改价格。
根据某些类别内产品的总价格,将应用返利(-1至-4%)
该功能适用于购物车和迷你购物车,直至“订单”页面。但折扣不适用于实际订单

// hook :
add_action( 'woocommerce_before_calculate_totals', array($this,'action_woocommerce_before_calculate_totals'), 10 ); 

// function
function action_woocommerce_before_calculate_totals( $WC_Cart ) {
// 1. verifications... if eligible then proceed to :

// 2. sum up eligible products amounts
$eligible_items = array();
$total_eligible_price = 0;
foreach ( $WC_Cart->get_cart_contents() as $cart_item_key => $cart_item ) {
   if(!isProductEligible($cart_item)) continue;
   $eligible_items[$cart_item_key] = array(
      'full_price' => $cart_item['data']->get_price(),
   );
$total_eligible_price += $cart_item['data']->get_price() *     $cart_item['quantity'];
}

// 3. find the correct ratio
$price_ratio_to_apply = getPriceRatioForTotal($total_eligible_price);

// 4. and calculate the discounted price for eligible items
foreach($eligible_items as $item_key => $item) {
   $eligible_items[$item_key]['discounted_price'] = $item['full_price'] * $price_ratio_to_apply;
}

// 5 then apply discounted prices to cart items
foreach($WC_Cart->get_cart() as $cart_item_key => $cart_item ) { 
   if(!isset($eligible_items[$cart_item_key])) continue;
   $discounted_price = $eligible_items[$cart_item_key]['discounted_price'];
   $cart_item['data']->set_price( $discounted_price );
}
该功能在购物车页面和订单页面上运行良好(其他功能用于在购物车项目行上显示全额/折扣价格)
但当客户实际验证订单时,不会对订单执行任何操作


当订单生效时,我是否应该添加另一个钩子来应用折扣价格?

我认为您应该尝试钩住以下操作之一:

 do_action( 'woocommerce_checkout_create_order_line_item', $item, $cart_item_key, $values, $order );

这将在创建和保存价格之前更新价格


但我必须说,我认为动态应用优惠券会更好。通过这种方式,您将看到订单上的折扣,这对您和您的客户都非常方便。

您的代码不完整,例如
isProductQualified()
getPriceRatioForTotal()
没有定义…现在所有检查和计算都应该在开始之前完成…记住这一点寻求调试帮助的问题必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码
do_action( 'woocommerce_checkout_create_order', $order, $data );