在WooCommerce中以编程方式自动将礼品产品变化添加到购物车?

编程入门 行业动态 更新时间:2024-10-11 21:23:06
本文介绍了在WooCommerce中以编程方式自动将礼品产品变化添加到购物车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to create a buy one get one offer for woocommerce when someone adds a product to the cart. The code below works but only with simple products, I've tried adding the variation ID to the variable but it does not work. Any ideas? thanks to the community

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 ); } }

推荐答案

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

To add a product variation to cart you need the parent variable product id and the product variation Id (both).

现在,如果客户在购物车页面中更新购物车,并删除了必需的产品ID,则您正在使用的代码已过时,将无法使用.在这种情况下,礼品产品将在重新加载页面或跳转到另一页面后被删除.因此, template_redirect 不是要使用的正确钩子.

Now the code that you are using is obsolete and will not work if customer update cart in cart page, removing the required product id. In this case, the Gift product will be removed after reloading the page or jumping to another page. So template_redirect is not the right hook to be used.

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

Try the following instead, that handle simple products and product variations too:

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文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

更多推荐

在WooCommerce中以编程方式自动将礼品产品变化添加到购物车?

本文发布于:2023-11-24 21:52:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   中以   礼品   方式   产品

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!