【发布时间】:2017-11-04 19:48:44
【问题描述】:
我在 WooCommerce 优惠券代码中创建了一个名为“2FOR1WOW”的代码,并将此代码添加到 functions.php,但它不起作用。票证被应用,确认消息说没问题,但总数没有减少。
任何帮助将不胜感激!
// Hook before calculate fees - "Buy 2 get cheapest free" coupon
add_action('woocommerce_cart_calculate_fees' , 'buy2_coupon');
/**
* Add discount for "Buy 2 get cheapest free" coupon
* @param WC_Cart $cart
*/
function buy2_coupon( WC_Cart $cart ){
// add the coupons here
$buy2_coupons = array('2FOR1WOW', 'anothercouponcode');
// return if cart has less than 2 items
if( $cart->cart_contents_count < 2 ){
return;
}
$applied_coupons = $cart->get_applied_coupons();
$matches = array_intersect($buy2_coupons, $applied_coupons);
// return if no coupon matches
if (empty($matches)) return;
// loop through the items in cart to find the cheapest
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$product_price[] = $_product->get_price_including_tax();
}
$cheapest = min($product_price);
$cart->add_fee( 'Coupon 2FOR1WOW', -$cheapest, true, 'standard' );
}
【问题讨论】:
-
问题的标题是不是有点像woocommerce产品的营销口号?
标签: php wordpress woocommerce cart coupon