我有一个WooCommerce网站。我想为某些选定的产品添加买1和获得1免费等促销活动。
我该怎么做?
由于
答案 0 :(得分:3)
是的,如果没有插件,它是可能的。有不同的方法,但最好和最简单的方法之一是...... 基于产品类别和自动应用的优惠券50% (简单和更好)。
1)首先在后端产品中创建产品类别>类别,例如' two4one'。
将此类别设置为您希望制定此促销计划的所有产品(2对1)
2)在woocommerce中创建特别优惠券(自动应用):
在woocommerce中创建一个优惠券,您将其命名为
'2for1'
。
你将对它进行特殊设置:
- 一般>折扣类型:产品%折扣
- 一般>优惠券金额:
50
(这是上面的百分比)- 使用限制>仅限个人使用:已启用
- 使用限制>产品类别:
two4one
(在此处添加您的特殊产品类别)因此,优惠券仅限折扣为
two4one
类别的产品。如果所有two4one
类别的产品都已从购物车中删除,优惠券也将被删除。
3)代码:优惠券创建并正确设置后,您可以使用此隐藏功能代码:
// Add to Cart 2 products at the same time of the "two4one" product category
add_action( 'woocommerce_add_to_cart', 'add_to_cart_qty_by_two', 10 );
function add_to_cart_qty_by_two($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
// When a product has 'two4one' as category increment quantity by one (2 products)
if( has_term( 'two4one', 'product_cat', $product_id ) ) {
$quantity += $quantity;
WC()->cart->set_quantity($cart_item_key, $quantity);
}
}
// Auto applying coupon if products with two4one" category are in cart
add_action( 'woocommerce_before_calculate_totals', 'auto_add_a_coupon_discount', 10 );
function auto_add_a_coupon_discount( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $item ) {
// When a product has 'two4one' as category auto apply coupon '2for1'.
if( has_term( 'two4one', 'product_cat', $item["product_id"] ) && !$cart_object->has_discount('2for1') )
WC()->cart->add_discount('2for1');
}
}
// If customer discrease or increse quantity it will be restored to an even number on checkout
add_action( 'woocommerce_before_checkout_form', 'checking_promotional_products', 10 );
function checking_promotional_products() {
foreach ( WC()->cart->cart_contents as $item_key => $item ) {
// if it's a promo product category and quantity is an even number
if( has_term( 'two4one', 'product_cat', $item["product_id"] ) && $item["quantity"] % 2 != 0 ) {
// checking that item quantity is always an even number (if not adds 1)
$quantity = $item["quantity"] + 1;
WC()->cart->set_quantity($item_key, $quantity);
}
}
}
此代码位于您的活动子主题(或主题)或任何插件文件的function.php文件中。