【问题标题】:Set product sale price programmatically in WooCommerce 3在 WooCommerce 3 中以编程方式设置产品销售价格
【发布时间】:2018-07-23 15:45:52
【问题描述】:

我有一个 Woocommerce 商店,里面有各种产品类别。

我想对属于产品类别 Cuckoo

的所有产品应用 20% 的折扣

目前我想要实现的只是在我的 functions.php

中设置销售价格

尝试如下:

    /* 
     * For a specific date, 20% off all products with product category as cuckoo clock.
     */
    function cuckoo_minus_twenty($sale_price, $product) { 
        $sale_price = $product->get_price() * 0.8;
        return $sale_price;
    }; 

    // add the action 
    add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 ); 

如果我在计算后var_dump $sale_price的结果我得到了正确的答案,但是前端的价格显示删除了正常价格并将销售价格显示为正常价格。

有没有我可以使用的钩子/过滤器来实现这一点?

我也尝试通过以下方式设置销售价格:

$product->set_sale_price($sale_price); 

无济于事。

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    我意识到,您或多或少需要以下所有过滤器才能使 HTML 开箱即用。

    add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    

    如果您想为可变产品正确显示它,您需要确保已更改 woocommerce_get_variation_prices_hash 因为存储的瞬态。

    您可能会发现我为客户创建的要点很有用

    https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020

    【讨论】:

      【解决方案2】:

      钩子 woocommerce_get_sale_price 自 WooCommerce 3 起已弃用,取而代之的是 woocommerce_product_get_sale_price

      产品显示的价格也会被缓存。当促销价有效时,正常价格也有效。

      试试这个:

      // Generating dynamically the product "regular price"
      add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
      add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
      function custom_dynamic_regular_price( $regular_price, $product ) {
          if( empty($regular_price) || $regular_price == 0 )
              return $product->get_price();
          else
              return $regular_price;
      }
      
      
      // Generating dynamically the product "sale price"
      add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
      add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
      function custom_dynamic_sale_price( $sale_price, $product ) {
          $rate = 0.8;
          if( empty($sale_price) || $sale_price == 0 )
              return $product->get_regular_price() * $rate;
          else
              return $sale_price;
      };
      
      // Displayed formatted regular price + sale price
      add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
      function custom_dynamic_sale_price_html( $price_html, $product ) {
          if( $product->is_type('variable') ) return $price_html;
      
          $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
      
          return $price_html;
      }
      

      代码进入您的活动子主题(活动主题)的 function.php 文件中。

      在单个产品、商店、产品类别和标签存档页面上测试和工作。

      继续在:
      Wrong Woocommerce cart item price after setting programmatically product sale price

      【讨论】:

      • 出于兴趣。此代码是否也适用于添加到购物车?旧价格似乎显示在购物车中。
      • @MarcusChristiansen 如果你问一个新问题,我可以用一个经过测试的答案来回答......这将适用于购物车/结帐和其他任何地方($orders 和电子邮件通知)......
      【解决方案3】:

      使用 woocomerce_get_sale_price 过滤器。

      add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
      add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
      
      function my_custom_price( $price, $product )
      {
          //your logic for calculating the new price here
           $price = $product->get_regular_price() * 0.8;
      
          //Return the new price (this is the price that will be used everywhere in the store)
          return $price;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 2011-12-11
        • 1970-01-01
        • 1970-01-01
        • 2019-05-04
        相关资源
        最近更新 更多