首页 文章

购物车总价未按推车价格更新

提问于
浏览
0

我根据具体标准动态地压低了产品的价格,但是当产品被添加到购物车中时,ajax和mini-cart似乎没有看到价格变化 . 它显示原始价格总计 . 我可以覆盖购物车本身的价格没问题,但你必须在购物车或结帐页面上才能看到它 . 不知道采取什么方法 . 我觉得好像我已经尝试了一切 .

好像_2427211被调用来显示当前的购物车总数,但是在调用时似乎没有运行 add_action( 'woocommerce_before_calculate_totals', 'woo_add_discount'); hook . 如果你去实际的购物车页面,这是正确的价格 .

添加的此代码将显示正确的单个价格,但不会显示小计 . 您必须通过单击购物车URL来刷新购物车页面,以更新$ woocommerce-> cart-> get_cart_total()对象 .

我也尝试过 add_action( 'woocommerce_before_mini_cart', 'woo_add_discount'); 做同样的事情..你必须在加载后刷新页面 . 我不是唯一一个抛弃价格并且不能让所有的价格落到实处的人 .

我已经尝试了这个,看到答案的第二个评论,有人有同样的问题,但没有答案 . WooCommerce: Add product to cart with price override?

2 回答

  • 0

    尝试使用 apply_filters 而不是 add_action

  • 1

    我自己最终解决了这个问题 . 我尝试了Paul de Koning的回答 . 更改为apply_filters导致价格变为$ 0 . 我认为这是因为他们需要进入的顺序 .

    这是我的解决方案 . 根本原因是我使用change price_html函数来提供购物车的更改价格 . 在该函数内部有add_action调用等 . 不知何故,必定存在一个排序问题 . 这是一个多步骤过程,以确保所有区域都正确完成 .

    如果您想更改woocommerce价格而不使用会话来存储更改显示价格和隐藏价格的购物车更改价格,请执行以下操作:

    functions.php

    add_action('woocommerce_get_price_html','special_price');
    
        function special_price($price) {
    
           //put any if statements for hiding the cart button and discounting pricing below and return true or false
           $displayprice = true;
           $alterprice   = true;
    
           if($displayprice === true) {
    
             add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2);
             add_action( 'init', 'woocommerce_add_to_cart_action', 10);
             add_action( 'init', 'woocommerce_checkout_action', 10 );
    
           } else {
    
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2);
            remove_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_template_single_product_add_to_cart', 10, 2);        //   
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
            //return blank or something like 'Signup to view price'
            return '';
    
           }
    
           //if you are displaying the price (altered)
           if($displayprice === true && $alterprice === true){
    
             $price = price_manipulator($theid);
             return $price;
    
             //display add to cart and price
    
           } 
    
           //if you are displaying the price (unaltered)
           return $price       
    
         }
    
    
        function price_manipulator($theid = '') {
    
          if(empty($theid)){
            $theid = get_the_ID();
          }
    
          $product = wc_get_product($theid);
    
          //30% off example
          $newprice = floatval($product->price * (1-0.3));
    
          return $newprice;
        }
    
       /*version of pricing if you are adding something to cart*/
        function special_price_cart($theid){
    
                  $price = price_manipulator($theid);             
    
                  return $price;
    
        }
    
    
        add_action( 'woocommerce_before_calculate_totals', 'woo_add_discount');
        add_action( 'woocommerce_before_mini_cart', 'woo_add_discount'); //this is if you are using the mini-cart woocommerce widget
    
        function woo_add_discount() {
    
           global $woocommerce;    
    
           //create if statements same as if you were displaying the price 
           $displayprice = true;
    
    
           if($displayprice === true){  
    
             foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { 
    
               $price = special_price_cart($cart_item['data']->id);
               $price = str_replace('$','',$price);
               $price = str_replace(',','',$price);               
    
    
               if($price > 0){
    
                 $cart_item['data']->price = floatval($price);
    
               }    
    
           }
    
         } 
    
      }
    

相关问题