首页 文章

Woocommerce预订的定制购物车商品价格计算

提问于
浏览
1

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

从技术上讲,当产品添加到购物车时,价格应该改变 .

我想它必须像这样计算:

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()函数 . 调用此选项时,购物车仍为空并且未输入循环 .

我的问题似乎是Woocommerce预订,因为以下行$ this-> price未设置然后调用get_base_cost导致仅显示基本成本而不是“重新计算”价格 . 由于它是可预订的产品,这似乎使它更加棘手 .

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是必须支付的重新计算价格 .

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

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
)

2 回答

  • 2

    您是否尝试在发送到购物车之前更改价格自定义方式如果是,那么您需要按会话发送以下两个钩子将有很大帮助

    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;
    }
    
  • 3

    这需要在 woocommerce_before_calculate_totals 动作钩子上完成,才能工作 . 在一个工作示例下面,它为购物车项目中的每个产品价格添加30:

    add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 10, 1 );
    function custom_cart_item_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        foreach ( $cart->get_cart() as $cart_item ){
            $price = $cart_item['data']->get_price(); // Get the product price
            $new_price = $price + 30; // the calculation
            $cart_item['data']->set_price( $new_price ); // Set the new price
        }
    }
    

    此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中 .


    注意:全球$ woocommerce;与$ woocommerce->购物车已过时,已被WC() - >购物车取代 .

相关问题