Woocommerce预订的定制购物车价格计算 [英] Custom cart items price calculation for Woocommerce Bookings

查看:90
本文介绍了Woocommerce预订的定制购物车价格计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据一些内部编码来更新价格计算。这是进入购物车项目所需的价格,到目前为止,购物车项目仅显示产品的基本成本。

I want to know how to update the price calculation depending on some internal coding. This is the price that needs to go into the cart item and thus far, the cart item shows only the base cost of the product.

从技术上讲,应该更改价格,当我将产品添加到购物车中时。

Technically the price should be altered, when a product is added to the cart.

我认为必须这样计算:

global $woocommerce;
$items = $woocommerce->cart->get_cart();

foreach($items as $item => $values) { 
    if ($value['product_id'] == $product_id) {
        $value['data']->set_price($price);
    }  
} 

因为这不能解决问题,所以在何时何地我是否需要放置set_price()函数。

Since this does not do the trick, where/when do I need to put the set_price() function. When this is called, the cart is still empty and the loop is not entered.

我的问题似乎与Woocommerce Bookings一样,如下行$ this-> price是未设置,然后调用get_base_cost导致仅显示基本成本,而不显示重新计算的价格。既然它是可预订的产品,这似乎使它变得更加棘手。

My problem seems to be with Woocommerce Bookings as with following line $this->price is not set and then get_base_cost is called resulting in only showing the base cost and not the "recalculated" price. Since it is a bookable product, this seems to make it more tricky.

public function get_price($context = ‘view’) {
    return apply_filters( 'woocommerce_get_price', $this->price ? $this->price : $this->get_base_cost(), $this );
}

仔细观察$ this,我至少可以看到class_wc_product_booking中的两个价格,其中300是基本成本,而250是必须支付的重新计算的价格。

Looking closely at $this I can at least see both prices within the class_wc_product_booking, where 300 is the "base cost" and 250 the recalculated price that has to be paid.

我确实删除了很多与该问题无关的数组。

I did delete a lot of the array that should not be relevant for this issue.

WC_Product_Booking Object ( 
[availability_rules:WC_Product_Booking:private] =>  Array ( ) 
[object_type:protected] => product 
[post_type:protected] => product 
[cache_group:protected] => products 
[data:protected] => 
    Array ( 
        [name] => Booking Test 
        [slug] => Booking Test
        [sku] => 
        [price] => 300
        [regular_price] => 
        [sale_price] => 
        [rating_counts] => Array ( ) 
        [average_rating] => 0 
        [review_count] => 0
    ) 
[supports:protected] => Array ( ) 
[id:protected] => 1708 
[changes:protected] => Array ( [price] => 250 ) 
[meta_data:protected] => 
[product_type] => booking
) 


推荐答案

您是否要更改价格自定义方式,然后再发送到购物车(如果是),那么您需要按会话发送以下两个钩子会很有帮助

Are you trying to change the price custom way before sending to the cart if yes then you need to send by session the below two hooks will help a lot

add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');
add_filter( 'woocommerce_get_cart_item_from_session',  'set_session_prices', 20 , 3 );

function set_woo_prices( $woo_data ) {
  session_start();    
  $tac_dd_discounted_price = $_SESSION['']; // get the updated new price field 

  if ( ! isset($tac_dd_discounted_price ) || empty ($tac_dd_discounted_price ) ) { return $woo_data; }
  $woo_data['data']->set_price( $tac_dd_discounted_price );
  $woo_data['my_price'] = $tac_dd_discounted_price;
  return $woo_data;
}

function  set_session_prices ( $woo_data , $values , $key ) {
    if ( ! isset( $woo_data['my_price'] ) || empty ( $woo_data['my_price'] ) ) { return $woo_data; }
    $woo_data['data']->set_price( $woo_data['my_price'] );
    return $woo_data;
}

这篇关于Woocommerce预订的定制购物车价格计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆