在WooCommerce 3中以编程方式设置产品销售价格 [英] Set product sale price programmatically in WooCommerce 3

查看:140
本文介绍了在WooCommerce 3中以编程方式设置产品销售价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Woocommerce商店中开设了各种产品类别.

I have a Woocommerce store set up with various product categories.

我想对属于产品类别杜鹃

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

For now all I'm trying to achieve is set a sale price in my 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 ); 

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

If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.

是否可以使用钩子/过滤器来实现这一目标?

Is there a hook/filter I can use to achieve this?

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

I've also tried setting the sale price by doing:

$product->set_sale_price($sale_price); 

无济于事.

推荐答案

自WooCommerce 3起,钩子woocommerce_get_sale_price被弃用,并由woocommerce_product_get_sale_price代替.

The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.

也将缓存产品显示的价格.当销售价格处于活动状态时,常规价格也处于活动状态.

Also Product displayed prices are cached. When sale price is active, regular price is also active.

尝试以下方法:

// 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文件中.

Code goes in function.php file of your active child theme (active theme).

经过测试,可以在单个产品,商店,产品类别和标签归档页面上使用.

续篇:
通过编程设置产品销售后错误的Woocommerce购物车项目价格价格

The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price

这篇关于在WooCommerce 3中以编程方式设置产品销售价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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