动态打折Woocommerce产品

动态打折Woocommerce产品

本文介绍了动态打折Woocommerce产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是为我的每个Woocommerce产品收取正常价格,并根据多种条件动态创建销售价格.这将包括以下内容:

What I would like to do is take the regular price for each of my Woocommerce products and dynamically create a sale price based on several conditions. This would include the following:

1)在商店页面产品上显示动态创建的销售价格.2)购物车中显示的销售价格(购物车中的项目价格和总计反映了动态销售价格)3)结帐页面和总计反映了动态的销售价格.

1) Showing the dynamically created sale price on the shop page products.2) Sale price displayed in the cart (item price and total in cart are reflective of the dynamic sale price )3) Checkout page and totals reflect dynamic sale price.

研究了几种解决方案,我提出了以下解决方案:

Researching several solutions I came up with the following solution:

// 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.7;
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;
}

add_action( 'woocommerce_before_calculate_totals',
'set_cart_item_sale_price', 10, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
    $price = $cart_item['data']->get_sale_price(); // get sale price
    $cart_item['data']->set_price( $price ); // Set the sale price
}
}


add_filter( 'woocommerce_cart_item_price',
'bbloomer_change_cart_table_price_display', 30, 3 );

function bbloomer_change_cart_table_price_display( $price, $values,
$cart_item_key ) {
$slashed_price = $values['data']->get_price_html();
$is_on_sale = $values['data']->is_on_sale();
if ( $is_on_sale ) {
$price = $slashed_price;
}
return $price;
}

除了购物车表总计显示原始价格总计而不是动态销售价格总计之外,此方法均有效.必须有一个更简单的方法来执行此操作.任何帮助,将不胜感激.谢谢,朱尔斯

This works except for the cart table total displays the original price totals, not the dynamic sale price totals. There must be an easier way of doing this. Any help would be appreciated. Thanks, Jules

推荐答案

不知何故,折扣被套用了两次.我不确定这可能与主题或框架有关.在两个函数中添加一行以限制应用折扣的时间,这对我来说很有用.

Somehow, the discount was being applied twice. Perhaps it was theme or framework related, I am not sure. Adding one line in both functions restricting when the discount is applied made it work for me.

function custom_product_sale_price( $price, $product ) {

if( is_admin() ) return $price;{

}

if( ! get_post_meta( $product->get_id(), '_sale_price', true ) > 0 ){
 if( is_shop() || is_product_category() || is_product_tag() || is_product()
|| is_cart() || is_checkout() )
  $price *= 0.7;
 }

 return $price;
 }

这篇关于动态打折Woocommerce产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:43