WooCommerce 中特定产品标签 ID 的购物车批量折扣 [英] Cart bulk quantity discount for specific product tag IDs in WooCommerce
问题描述
我想创建一个函数来为 WooCommerce 创建有条件的批量动态定价,根据添加到购物车的数量不同的单价.
我有两个产品标签 ID(326、327),每个标签中有一堆产品.我想为两个标签和相同数量提供不同的折扣.
- 标签 ID 326
- 10 件以上 15% 折扣
- 25 件以上可享受 20% 的折扣
- 标签 ID 327
- 10 件以上 20% 折扣
- 25 件以上可享受 35% 的折扣.
这是我正在使用的代码:
add_action('woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999);函数woocommerce_quantity_based_pricing($cart){if ( is_admin() && !defined( 'DOING_AJAX' ) ) 返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;如果(has_term(326,'product_tag')){$threshold1 = 10;$discount1 = 0.15;$threshold2 = 25;$discount2 = 0.3;} elseif( has_term( 327, 'product_tag' ) ) {$threshold1 = 10;$discount1 = 0.2;$threshold2 = 25;$discount2 = 0.35;}foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );$cart_item['data']->set_price( $price );} elseif ( $cart_item['quantity'] >= $threshold2 ) {$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );$cart_item['data']->set_price( $price );}}}
不幸的是没有想要的结果.有什么建议吗?
要根据产品标签 ID 应用折扣,您可以使用
I want to create a function to create conditional bulk dynamic pricing for WooCommerce, different unit prices based on the quantity added to cart.
I have two product tags ids (326, 327) and there is a bunch of product in each tag. I want to give a different discount for both tags and for the same quantity.
- Tag ID 326
- 15% discount above 10 units
- 20% discount above 25 units
- Tag ID 327
- 20% discount above 10 units
- 35% discount above 25 units.
This is the code I am working with:
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999 );
function woocommerce_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
if( has_term( 326, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.15;
$threshold2 = 25;
$discount2 = 0.3;
} elseif( has_term( 327, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.2;
$threshold2 = 25;
$discount2 = 0.35;
}
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
}
}
}
Unfortunately without the desired result. Any advice?
To apply a discount based on product tag IDs, you can use WC_Product::get_tag_ids(), and then compare it.
I have provided an extra piece of code via the woocommerce_cart_item_price
hook, which will allow you to strikethrough the original price and show the new price next to it
So you get:
// Used to calculate totals
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Set Tag IDs
$tag_id_1 = 326;
$tag_id_2 = 327;
// Tag ID 1
// 15% discount above 10 units
$tag_id_1_threshold_1 = 10;
$tag_id_1_discount_1 = 0.15;
// 20% discount above 25 units
$tag_id_1_threshold_2 = 25;
$tag_id_1_discount_2 = 0.20;
// Tag ID 2
// 20% discount above 10 units
$tag_id_2_threshold_1 = 10;
$tag_id_2_discount_1 = 0.20;
// 35% discount above 25 units
$tag_id_2_threshold_2 = 25;
$tag_id_2_discount_2 = 0.35;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get price
$price = $cart_item['data']->get_price();
// Get quantity
$quantity = $cart_item['quantity'];
// Get product tag ID's
$tag_ids = $cart_item['data']->get_tag_ids();
// Check for tag ID
if ( in_array ( $tag_id_1, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_1_threshold_1 && $quantity < $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_1;
} elseif ( $quantity >= $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_2;
}
} elseif ( in_array ( $tag_id_2, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_2_threshold_1 && $quantity < $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_1;
} elseif ( $quantity >= $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_2;
}
}
// Isset & NOT empty
if ( isset( $discount ) && ! empty( $discount ) ) {
// Set new price
$cart_item['data']->set_price( $price * ( 1 - $discount ) );
// Reset
$discount = '';
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
// Strikethrough the original price
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get reqular price
$regular_price = $product->get_regular_price();
// New price
$new_price = $cart_item['data']->get_price();
// NOT empty and NOT equal
if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
// Output
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
}
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
这篇关于WooCommerce 中特定产品标签 ID 的购物车批量折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!