从 WooCommerce 目录中隐藏自动添加的礼品产品
Hide auto added gift product from WooCommerce catalog
我有以下无法解决的情况,你能用插件或代码指导我吗?
我有商品A和商品B,不能出现在店里,也不能单独添加,因为是礼物。
如果有人将产品A添加到购物车,那么系统应该添加产品B。它就像一个BOGO,但我无法解决的问题是免费产品必须是不可见的并且不允许是单独购买。
根据 答案代码,以下是将特定产品添加到购物车时自动添加隐藏礼品的完整方法:
// Auto add a hidden gift product to cart based on sprcific product
add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_gift_to_cart' );
function wc_auto_add_gift_to_cart( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
$required_product_id = 37; // The required product Id for a gift
$product_gift_id = 53; // The gift product Id
$has_required = $gift_key = false; // Initializing
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if required product is in cart
if( in_array( $required_product_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$has_required = true;
}
// Check if gifted product is already in cart
if( in_array($product_gift_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$gift_key = $cart_item_key;
}
}
// If gift is in cart, but not the required product: Remove gift from cart
if ( ! $has_required && $gift_key ) {
$cart->remove_cart_item( $gift_key );
}
// If gift is not in cart and the required product is in cart: Add gift to cart
elseif ( $has_required && ! $gift_key ) {
$cart->add_to_cart( $product_gift_id );
}
}
// Hide gift product from catalog (shop and archive pages)
add_action( 'woocommerce_product_query', 'hide_gift_product_from_catalog' );
function hide_gift_product_from_catalog( $q ) {
// Not in admin
if ( ! is_admin() ) {
$product_gift_id = 53;
$q->set('post__not_in', array($product_gift_id) );
}
}
// Redirect gift product single page to shop
add_action( 'template_redirect', 'redirect_gift_single_product_to_shop' );
function redirect_gift_single_product_to_shop() {
$product_gift_id = 53;
//
if ( get_the_id() == $product_gift_id ) {
wp_redirect( wc_get_page_permalink( 'shop' ) );
exit;
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关:
我有以下无法解决的情况,你能用插件或代码指导我吗?
我有商品A和商品B,不能出现在店里,也不能单独添加,因为是礼物。
如果有人将产品A添加到购物车,那么系统应该添加产品B。它就像一个BOGO,但我无法解决的问题是免费产品必须是不可见的并且不允许是单独购买。
根据
// Auto add a hidden gift product to cart based on sprcific product
add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_gift_to_cart' );
function wc_auto_add_gift_to_cart( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
$required_product_id = 37; // The required product Id for a gift
$product_gift_id = 53; // The gift product Id
$has_required = $gift_key = false; // Initializing
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if required product is in cart
if( in_array( $required_product_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$has_required = true;
}
// Check if gifted product is already in cart
if( in_array($product_gift_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
$gift_key = $cart_item_key;
}
}
// If gift is in cart, but not the required product: Remove gift from cart
if ( ! $has_required && $gift_key ) {
$cart->remove_cart_item( $gift_key );
}
// If gift is not in cart and the required product is in cart: Add gift to cart
elseif ( $has_required && ! $gift_key ) {
$cart->add_to_cart( $product_gift_id );
}
}
// Hide gift product from catalog (shop and archive pages)
add_action( 'woocommerce_product_query', 'hide_gift_product_from_catalog' );
function hide_gift_product_from_catalog( $q ) {
// Not in admin
if ( ! is_admin() ) {
$product_gift_id = 53;
$q->set('post__not_in', array($product_gift_id) );
}
}
// Redirect gift product single page to shop
add_action( 'template_redirect', 'redirect_gift_single_product_to_shop' );
function redirect_gift_single_product_to_shop() {
$product_gift_id = 53;
//
if ( get_the_id() == $product_gift_id ) {
wp_redirect( wc_get_page_permalink( 'shop' ) );
exit;
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关: