【发布时间】:2013-11-28 09:15:21
【问题描述】:
当有人输入我制作的 hiddenproduct 优惠券时,我已经广泛搜索了一种解决方案,可以将免费商品(我用 woocommerce 隐藏)添加到购物车。这是我正在使用的代码,它是这个的修改版本:http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/。不同之处在于我不是使用购物车总数来添加它,而是尝试使用应用的优惠券。
这是我当前的代码,它没有将产品添加到购物车:
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 1211;
$found = false;
$coupon_id = 1212;
if( $woocommerce->cart->applied_coupons == $coupon_id ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
我很确定错误在这里发生'($woocommerce->cart->applied_coupons == $coupon_id)',但我不知道正确的标识符。
这是在我的functions.php中
谁能帮帮我?谢谢
【问题讨论】:
-
如果我使用 if( $woocommerce->cart->applied_coupons) { 我可以让它将产品添加到购物车,但它可以使用任何优惠券。我如何才能在此基础上仅使用特定的优惠券 ID 添加它?
标签: php wordpress woocommerce