【问题标题】:Auto add to cart a Gift product variation programmatically in WooCommerce?在 WooCommerce 中以编程方式将礼品产品变体自动添加到购物车?
【发布时间】:2020-09-22 01:27:44
【问题描述】:

当有人将产品添加到购物车时,我正在尝试为 woocommerce 创建买一送一优惠。下面的代码有效,但仅适用于简单的产品,我尝试将变体 ID 添加到变量中,但它不起作用。有任何想法吗?感谢社区

add_action( 'template_redirect', 'bbloomer_add_gift_if_id_in_cart' );
 
function bbloomer_add_gift_if_id_in_cart() {
 
   if ( is_admin() ) return;
   if ( WC()->cart->is_empty() ) return;
 
   $product_bought_id = 421;
   $product_gifted_id = 1256;
 
   // see if product id in cart
   $product_bought_cart_id = WC()->cart->generate_cart_id( $product_bought_id );
   $product_bought_in_cart = WC()->cart->find_product_in_cart( $product_bought_cart_id );
 
   // see if gift id in cart
   $product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
   $product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id );
 
   // if not in cart remove gift, else add gift
   if ( ! $product_bought_in_cart ) {
      if ( $product_gifted_in_cart ) WC()->cart->remove_cart_item( $product_gifted_in_cart );
   } else {
      if ( ! $product_gifted_in_cart ) WC()->cart->add_to_cart( $product_gifted_id );
   }
}

【问题讨论】:

    标签: php wordpress woocommerce cart product-variations


    【解决方案1】:

    要将产品变体添加到购物车,您需要父变量产品 ID 和产品变体 ID(两者)。

    现在您使用的代码已过时,如果客户在购物车页面更新购物车,删除所需的产品 ID,则该代码将不起作用。在这种情况下,礼品产品将在重新加载页面或跳转到另一个页面后被移除。所以template_redirect 不是正确的钩子。

    尝试以下方法,也可以处理简单的产品和产品变体:

    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 (or variation Id)
        $parent_gift_id      = 40; // The parent variable product Id (gift) for a product variation (set to zero for simple products)
        $product_gift_id     = 41; // the variation Id or the product Id (gift)
        
        $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( $cart_item['data']->get_id() == $product_gift_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 ) {
            // For simple products
            if( $parent_gift_id == 0 ) {
                $cart->add_to_cart( $product_gift_id );
            } 
            // For product variations (of a variable product)
            else {
                $cart->add_to_cart( $parent_gift_id, 1, $product_gift_id );
            }
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 我已成功使用您在此答案中发布的代码,现在我想对其进行扩展。有没有一种简单的方法可以将 $required_product_id 更改为产品必须所在的类别数组,而不是一个产品 ID?还是需要完全不同的方法?
    • 我还尝试将$required_product_id = 37; 更改为这样的数组:$required_product_id = array (37, 38, 39);(考虑到这些产品 ID 中的任何一个都会触发将礼物添加到购物车中),但它没有t 抛出错误,但我放入购物车的任何产品 ID 都没有添加礼物。如何将所需的产品 ID 更改为可能的 ID 数组?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多