问题描述
我正在尝试对购物车中的所有商品应用 TOTAL CART 折扣,具体取决于添加的商品数量.我已经从
您可以按购物车商品数量或根据购物车商品数量全局进行:
1) 按购物车商品数量:
//根据数量参数给出折扣百分比的实用函数函数 get_discount_percent( $quantity ){如果( $数量 0 ?$variation_id : $product_id;## ----- 您的设置 ----- ##$percent_1 = 5;//折扣 (5%)$percent_2 = 10;//折扣 (10%)//WC_Product 对象$product = wc_get_product($product_id);$price = (float) $product->get_price();//将产品默认基本价格设置为自定义购物车项目数据$cart_item_data['discount'][0] = $price;//将产品折扣价设置为 5% 作为自定义购物车项目数据$cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1)/100;//将产品折扣价设置为 10% 作为自定义购物车项目数据$cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2)/100;返回 $cart_item_data;}//显示产品原价add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){if( isset($cart_item['discount'][0]) ) {$product = $cart_item['data'];$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) );}返回 $product_price;}//显示带有折扣百分比的产品名称add_filter('woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3);函数 append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){//获取基于数量的百分比$percent = get_discount_percent($cart_item['quantity']);if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {if( $cart_item['data']->get_price() != $cart_item['discount'][0] )$product_name .= ' (' . $percent . '% 折扣)';}返回 $product_name;}add_action('woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1);函数 set_custom_discount_cart_item_price( $cart ) {if ( is_admin() && !defined( 'DOING_AJAX' ) )返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )返回;foreach( $cart->get_cart() as $cart_item ){//获取基于数量的百分比$percentage = get_discount_percent($cart_item['quantity']);//对于非打折商品,根据定义的数量设置折扣if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {$cart_item['data']->set_price($cart_item['discount'][$percentage]);}}}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
2) 全球范围内的购物车商品数量 (对于非特价商品):
//根据数量参数给出折扣百分比的实用函数函数 get_discount_percent( $quantity ){如果( $数量 cart->get_cart() 作为 $cart_item ){if(!$cart_item['data']->is_on_sale()){$items_count += $cart_item['数量'];}}返回 $items_count;}add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3);函数 add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){$product_id = $variation_id >0 ?$variation_id : $product_id;## ----- 您的设置 ----- ##$percent_1 = 5;//折扣 (5%)$percent_2 = 10;//折扣 (10%)//WC_Product 对象$product = wc_get_product($product_id);$price = (float) $product->get_price();//将产品默认基本价格设置为自定义购物车项目数据$cart_item_data['discount'][0] = $price;//将产品折扣价设置为 5% 作为自定义购物车项目数据$cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1)/100;//将产品折扣价设置为 10% 作为自定义购物车项目数据$cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2)/100;返回 $cart_item_data;}//显示产品原价add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){if( isset($cart_item['discount'][0]) ) {$product = $cart_item['data'];$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) );}返回 $product_price;}//显示带有折扣百分比的产品名称add_filter('woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3);函数 append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){//获取购物车商品数量$items_count = get_non_on_sale_cart_items_count();//获取基于数量的百分比$percent = get_discount_percent($items_count);if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {if( $cart_item['data']->get_price() != $cart_item['discount'][0] )$product_name .= ' (' . $percent . '% 折扣)';elseif ( $cart_item['data']->is_on_sale() && $percent != 0 ){$product_name .= ' <em>(特价商品)</em>';}}返回 $product_name;}//更改购物车商品价格add_action('woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1);函数 set_custom_discount_cart_item_price( $cart ) {if ( is_admin() && !defined( 'DOING_AJAX' ) )返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )返回;//获取购物车商品数量$items_count = get_non_on_sale_cart_items_count();//获取基于数量的百分比$percentage = get_discount_percent($items_count);foreach( $cart->get_cart() as $cart_item ){//对于非打折商品,根据定义的数量设置折扣if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {$cart_item['data']->set_price($cart_item['discount'][$percentage]);}}}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
在下面这个例子中,第三件商品在打折,所以实际数量是10 + 1 = 11
件商品,然后是11
件的折扣> 项是 5%
:
I am trying to apply a TOTAL CART discount to all the items in the cart, depending on the quantity of items added. I have taken and modified the code example from answer here and got most of the logic working.
I cannot however get it working for two scenarios described below. It also only applies the rule to a single item in the cart. Not the entire cart.
Scenario:
- If no of items in the cart are between 9-12, Apply 5% Discount to all items.
- If no of items in the cart are between 13-16, Apply 10% Discount to all items.
- The discount's should not stack. e.g. when 12 items in cart, apply 5% discount...when 13 items added, remove 5% discount and apply 10% discount.
Code:
add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
$product_id = $variation_id > 0 ? $variation_id : $product_id;
## ----- YOUR SETTING ----- ##
$discount_percentage = 5; // Discount (5%)
// The WC_Product Object
$product = wc_get_product($product_id);
// Only for non on sale products
if( ! $product->is_on_sale() ){
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['base_price'] = $price;
// Set the Product discounted price as custom cart item data
$cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;
// Set the percentage as custom cart item data
$cart_item_data['percentage'] = $discount_percentage;
}
return $cart_item_data;
}
// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
if( isset($cart_item['base_price']) ) {
$product = $cart_item['data'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
}
return $product_price;
}
// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
if( $cart_item['data']->get_price() != $cart_item['base_price'] )
$product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
}
return $product_name;
}
add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
## ----- YOUR SETTING ----- ##
$balanced_qty = 9; // Targeted quantity
$upper_balanced_qty = 12; // Max quantity limit
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// For item quantity between 9 and 12
if( ($cart_item['quantity'] >= $balanced_qty && isset($cart_item['new_price']) && ($cart_item['quantity'] <= $upper_balanced_qty) ) ){
// Set cart item discounted price
$cart_item['data']->set_price($cart_item['new_price']);
}
}
}
You can do it by cart item quantity or globally based on cart items count:
1) By cart item quantity:
// Utility function that give the discount percentage based on quantity argument
function get_discount_percent( $quantity ){
if( $quantity < 9 )
$percent = 0; // 0 % ( quantity from 1 to 8 )
elseif( $quantity >= 9 && $quantity < 13 )
$percent = 5; // 5 % ( quantity from 9 to 12 )
else
$percent = 10; // 10 % ( quantity up to 13 )
return $percent;
}
add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
$product_id = $variation_id > 0 ? $variation_id : $product_id;
## ----- YOUR SETTING ----- ##
$percent_1 = 5; // Discount (5%)
$percent_2 = 10; // Discount (10%)
// The WC_Product Object
$product = wc_get_product($product_id);
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['discount'][0] = $price;
// Set the Product discounted price of 5% as custom cart item data
$cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1) / 100;
// Set the Product discounted price of 10% as custom cart item data
$cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2) / 100;
return $cart_item_data;
}
// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
if( isset($cart_item['discount'][0]) ) {
$product = $cart_item['data'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) ) );
}
return $product_price;
}
// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
// get the percent based on quantity
$percent = get_discount_percent($cart_item['quantity']);
if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {
if( $cart_item['data']->get_price() != $cart_item['discount'][0] )
$product_name .= ' <em>(' . $percent . '% discounted)</em>';
}
return $product_name;
}
add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
function set_custom_discount_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach( $cart->get_cart() as $cart_item ){
// get the percent based on quantity
$percentage = get_discount_percent($cart_item['quantity']);
// For items non on sale set a discount based on quantity as defined in
if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {
$cart_item['data']->set_price($cart_item['discount'][$percentage]);
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) Globally on cart item count (for non on sale products):
// Utility function that give the discount percentage based on quantity argument
function get_discount_percent( $quantity ){
if( $quantity < 9 )
$percent = 0; // 0 % ( quantity from 1 to 8 )
elseif( $quantity >= 9 && $quantity < 13 )
$percent = 5; // 5 % ( quantity from 9 to 12 )
else
$percent = 10; // 10 % ( quantity up to 13 )
return $percent;
}
// Utility function that count cart items that are not on sale
function get_non_on_sale_cart_items_count(){
$items_count = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
if( ! $cart_item['data']->is_on_sale() ){
$items_count += $cart_item['quantity'];
}
}
return $items_count;
}
add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
$product_id = $variation_id > 0 ? $variation_id : $product_id;
## ----- YOUR SETTING ----- ##
$percent_1 = 5; // Discount (5%)
$percent_2 = 10; // Discount (10%)
// The WC_Product Object
$product = wc_get_product($product_id);
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['discount'][0] = $price;
// Set the Product discounted price of 5% as custom cart item data
$cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1) / 100;
// Set the Product discounted price of 10% as custom cart item data
$cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2) / 100;
return $cart_item_data;
}
// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
if( isset($cart_item['discount'][0]) ) {
$product = $cart_item['data'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) ) );
}
return $product_price;
}
// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
// Get cart items count
$items_count = get_non_on_sale_cart_items_count();
// get the percent based on quantity
$percent = get_discount_percent($items_count);
if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {
if( $cart_item['data']->get_price() != $cart_item['discount'][0] )
$product_name .= ' <em>(' . $percent . '% discounted)</em>';
elseif ( $cart_item['data']->is_on_sale() && $percent != 0 ){
$product_name .= ' <em>(Item on sale)</em>';
}
}
return $product_name;
}
// Change cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
function set_custom_discount_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get cart items count
$items_count = get_non_on_sale_cart_items_count();
// get the percent based on quantity
$percentage = get_discount_percent($items_count);
foreach( $cart->get_cart() as $cart_item ){
// For items non on sale set a discount based on quantity as defined in
if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {
$cart_item['data']->set_price($cart_item['discount'][$percentage]);
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于Woocommerce 3 中的购物车项目数量累进百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!