您好我正在寻找一个代码,如果添加了普通产品,我将订阅ID (2282)添加到购物车,但如果用户已经是订阅者,则 NOT 。< / p>
随意提问。我是GMT + 1时区的
Wordpress - 4.8.1
WooCommerce - 3.1.1
WooCommerce订阅 - 2.2.11
WooCommerce会员资格 - 1.8.8
主题 - 店主 - 2.2.3
我看了一眼,并试图愚弄这个。没有成功
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 2282;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->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 )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
如果为非活跃订阅者添加到购物车的产品,我如何有条件地添加订阅?
答案 0 :(得分:2)
对于WooCommerce订阅我没有找到任何功能来检查当前用户是否有有效订阅。
所以我有这个答案仍然适用于WooCommerce 3+和最后一个WC订阅插件:Detecting if the current user has an active subscription
因此,使用挂在woocommerce_add_to_cart动作钩子中的自定义函数并使用我的旧答案中的条件函数代码,您将使其正常工作:
// Conditional function detecting if the current user has an active subscription
function has_active_subscription( $user_id=null ) {
// if the user_id is not set in function argument we get the current user ID
if( null == $user_id )
$user_id = get_current_user_id();
// Get all active subscrptions for a user ID
$active_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription', // Subscription post type
'post_status' => 'wc-active', // Active subscription
) );
// if user has an active subscription
if(!empty($active_subscriptions)) return true;
else return false;
}
// Conditionally checking and adding your subscription when a product is added to cart
add_action( 'woocommerce_add_to_cart', 'add_subscription_to_cart', 10, 6 );
function add_subscription_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Here define your subscription product
$subscription_id = 2282;
$found = false;
if ( is_admin() || has_active_subscription() || $product_id == $subscription_id ) return; // exit
// Checking if subscription is already in cart
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $subscription_id ){
$found = true;
break;
}
}
// if subscription is not found, we add it
if ( ! $found )
WC()->cart->add_to_cart( $subscription_id );
}
代码进入任何插件php文件(钩子可以在活动主题的function.php文件中使用)。
代码经过测试并有效。
如果订阅已从购物车中删除,我们会在结帐时重新检查:
add_action( 'woocommerce_before_checkout_form', 'check_subscription_in_checkout', 10, 0 );
function check_subscription_in_checkout() {
// Here define your subscription product
$subscription_id = 2282;
$found = false;
if ( is_admin() || has_active_subscription() || ! is_checkout() ) return; // exit
// Checking if subscription is already in cart
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $subscription_id ){
$found = true;
break;
}
}
// if subscription is not found, we add it
if ( ! $found )
WC()->cart->add_to_cart( $subscription_id );
}