【问题标题】:Change product prices via a hook in WooCommerce 3+通过 WooCommerce 3+ 中的挂钩更改产品价格
【发布时间】:2018-01-30 01:37:58
【问题描述】:

在 WooCommerce 中,我需要将所有产品价格乘以一个数字。所以我使用了以下(通过插件)

add_filter('woocommerce_get_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_price', array( $this, 'my_custom_price'), 99);

function my_custom_price( $original_price ) {
  global $post, $woocommerce;

  //Logic for calculating the new price here
  $new_price = $original_price * 2;

  //Return the new price (this is the price that will be used everywhere in the store)
  return $new_price;
 }

但是,这不适用于变体产品。我尝试了以下钩子,但没有成功:

add_filter('woocommerce_get_variation_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_variation_price', array( $this, 'my_custom_price'), 99);

唯一能半途而废的就是这个:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);

但这只是改变了整体价格,而不是所选的变体价格。见下图,价格为 BsF。 200,整体价格是对的,200 x 2 = 400,但是选择时的变化价格仍然显示200:

注意:我需要它来实际更改,因此显示 html 挂钩不起作用。

我有什么遗漏或有什么问题吗?

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    更新 (2020 年 12 月)

    • 主题和插件的 2 个代码版本(也适用于 Woocommerce 3.3.x)
    • Woocommerce 3 中的缓存变化价格(更新和添加)
      现在使用 woocommerce_get_variation_prices_hash 过滤器挂钩效率更高,而不是 wc_delete_product_transients()... 请参阅 this related thread
    • 添加了产品价格过滤器小部件挂钩(见最后)

    1) 带有构造函数的插件版本

    您使用的钩子在 WooCommerce 3+ 中已弃用

    要使其适用于所有产品价格,包括变体价格,您应该使用这个:

    ## The following goes inside the constructor ##
    
    // Simple, grouped and external products
    add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
    add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
    // Variations 
    add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
    add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );
    
    // Variable (price range)
    add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
    add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );
    
    // Handling price caching (see explanations at the end)
    add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 3 );
    
    
    ## This goes outside the constructor ##
    
    // Utility function to change the prices with a multiplier (number)
    public function get_price_multiplier() {
        return 2; // x2 for testing
    }
    
    public function custom_price( $price, $product ) {
        return (float) $price * get_price_multiplier();
    }
    
    public function custom_variable_price( $price, $variation, $product ) {
        return (float) $price * get_price_multiplier();
    }
    
    public function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
        $price_hash[] = get_price_multiplier();
        return $price_hash;
    }
    

    经过测试的代码(仅)在 WooCommerce 3+ 中完美运行。


    2) 对于主题版本: functions.php 活动子主题(或活动主题)上的文件:

    // Utility function to change the prices with a multiplier (number)
    function get_price_multiplier() {
        return 2; // x2 for testing
    }
    
    // Simple, grouped and external products
    add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
    add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
    // Variations
    add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
    add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
    function custom_price( $price, $product ) {
        return (float) $price * get_price_multiplier();
    }
    
    // Variable (price range)
    add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
    add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
    function custom_variable_price( $price, $variation, $product ) {
        // Delete product cached price  (if needed)
        // wc_delete_product_transients($variation->get_id());
    
        return (float) $price * get_price_multiplier();
    }
    
    // Handling price caching (see explanations at the end)
    add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
    function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
        $price_hash[] = get_price_multiplier();
        return $price_hash;
    }
    

    在 woocommerce 3+ 上测试并运行


    对于销售中的产品,您有这些钩子:

    • woocommerce_product_get_sale_price (简单、分组和外部产品)
    • woocommerce_variation_prices_sale_price (可变产品(最小-最大))
    • woocommerce_product_variation_get_sale_price (产品变体)

    缓存价格和 woocommerce 3:

    变量缓存价格中涉及的 3 个过滤器挂钩是:

    • woocommerce_variation_prices_price
    • woocommerce_variation_prices_regular_price
    • woocommerce_variation_prices_sale_price

    在 Woocommerce 3 中引入,woocommerce_get_variation_prices_hash 过滤钩子将允许以更有效的方式刷新变化缓存的价格,而不会在执行此钩子时删除相关的瞬态。

    所以性能将保持提升(感谢Matthew Clark 指出了更好的方法)

    见:Caching and dynamic pricing – upcoming changes to the get_variation_prices method


    要使用小部件过滤产品价格 (最低和最高价格),请使用以下挂钩:

    • woocommerce_price_filter_widget_min_amount 只有一个参数 $price
    • woocommerce_price_filter_widget_max_amount 只有一个参数 $price

    【讨论】:

    • 谢谢!现在它只有在我使用插件删除数据库瞬变以查看更改时才有效。有没有办法用函数自动删除那些瞬态??
    • 您能指导我如何将其正确添加到 my_custom_price 函数中吗?在此先感谢,很好的回答!
    • @KronosL 用wc_delete_product_transients($post->ID); 更新了我的答案……因为这在从插件中使用时似乎是特定的。当您通过主题使用该挂钩时,此问题不存在。之后这还不够……所以你必须搜索一下。
    • 这似乎不起作用所有价格都变成了 0
    • @kos 我已经测试、更正和更新了代码...现在有 2 个版本,所以根据它是用于插件还是活动主题 function.php 文件选择好的版本。跨度>
    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多