简体   繁体   English

允许根据 WooCommerce 中的购物车总数将特定产品添加到购物车

[英]Allow add to cart for specific products based on cart total in WooCommerce

In woocommerce, I am trying to find a way to only allow a product to be added a to cart only when a specific cart total amount is reached.在 woocommerce 中,我试图找到一种方法,仅当达到特定的购物车总量时才允许将产品添加到购物车。

Example: We want to sell a bumper sticker for $1 , but only if a user already has $25 worth of other products already in the cart.示例:我们想以1 美元的价格出售保险杠贴纸,但前提是用户的购物车中已有价值 25美元的其他产品。 This is similar to Amazon's "add on" feature.这类似于亚马逊的“附加”功能。 However I can't find a similar WooCommerce plugin or function.但是我找不到类似的 WooCommerce 插件或功能。

I have tried yet some code without success… Any help will be appreciated.我已经尝试了一些代码但没有成功......任何帮助将不胜感激。

Can be done with a custom function hooked in woocommerce_add_to_cart_validation filter hook, where you will define:可以使用挂在woocommerce_add_to_cart_validation过滤器钩子中的自定义函数来完成,您将在其中定义:

  • a product Id (or multiple product Ids).一个产品 ID(或多个产品 ID)。
  • the threshold cart amount to be reached.要达到的阈值购物车数量。

It will avoid for those defined products to be added to cart (displaying a custom notice) until that specific cart amount is reached.在达到特定的购物车数量之前,它将避免将那些定义的产品添加到购物车(显示自定义通知)。

The code:编码:

add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {

    // HERE define one or many products IDs in this array
    $products_ids = array( 37, 27 );

    // HERE define the minimal cart amount that need to be reached
    $amount_threshold = 25;

    // Total amount of items in the cart after discounts
    $cart_amount = WC()->cart->get_cart_contents_total();

    // The condition
    if( $cart_amount < $amount_threshold && in_array( $product_id, $products_ids ) ){
        $passed = false;
        $text_notice = __( "Cart amount need to be up to $25 in order to add this product", "woocommerce" );
        wc_add_notice( $text_notice, 'error' );
    }
    return $passed;
}

Code goes in function.php file of your active child theme (active theme).代码位于活动子主题(活动主题)的 function.php 文件中。

Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据WooCommerce中的特定购物车总额添加费用 - Add fee based on specific cart total in WooCommerce 根据 WooCommerce 购物车总数添加或删除特定购物车项目 - Add or remove specific cart Item based on WooCommerce cart total "允许将 Woocommerce 中特定产品类别的最多 3 个产品添加到购物车" - Allow add to cart 3 products max for a specific product category in Woocommerce Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products Woocommerce-将某些类别的产品的运费加到购物车总额中 - Woocommerce - Add delivery charge to cart total for products in certain category 允许在 WooCommerce 中为特定用户角色添加相同父类别的产品到购物车 - Allow add to cart products of the same parent category for specific user role in WooCommerce 如何在woocommerce购物车页面中基于当前产品添加相关产品? - How to add related products based on current products in woocommerce cart page? 插件:Woocommerce &gt; 如何允许将作者的产品添加到购物车 - Plugin: Woocommerce > How to allow add to cart products from author 在WooCommerce中仅允许从相同父类别的产品添加到购物车 - Only allow add to cart from products of the same parent category in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM