【问题标题】:Hooks around add to cart for Woocommerce钩子添加到 Woocommerce 的购物车
【发布时间】:2017-11-28 12:35:51
【问题描述】:

我正在使用 WC Marketplace 经营一家 WooCommerce 商店。我试图用下面的钩子实现的是,如果篮子里已经有来自不同供应商的产品,则防止将新项目添加到篮子中。例如如果购物者将供应商 y 的产品 x 添加到他的购物篮中,如果他们要添加供应商 b 的产品 a,则该商品将不会被添加,并且会通知用户错误。

我有两个问题:
- 首先钩子什么时候运行,是在主函数触发之前还是之后?我有一个函数woocommerce_add_to_cart 的钩子。所以我想知道函数woocommerce_add_to_cart运行之后或者之前钩子会触发。
- 我的第二个问题是,我附上了下面的钩子,您认为这可行吗?

function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { 
    $same_vendor = 1;

    $empty = WC_Cart::is_empty();

    //If there is an item in the cart then,
    if (!$empty) {
        //Get the VendorId of the product being added to the cart.
        $vendor = get_wcmp_product_vendors($product_id);
        $vendor_id = $vendor->id;

        foreach( WC()->cart->get_cart() as $cart_item ) {
            //Get the vendor Id of the item
            $cart_product_id = $cart_item['product_id'];
            $cart_vendor = get_wcmp_product_vendors($product_id);
            $cart_vendor_id = $cart_vendor->id;

            //If two products do not have the same Vendor then set $same_vendor to 0
            if($vendor_id !== $cart_vendor_id) {
                $same_vendor = 0;
            }
        }

        if ($same_vendor === 0) {
            WC()->cart->remove_cart_item( $cart_item_key );
            //How do I show a message to tell the customer.
        }
    }
}

问候

【问题讨论】:

  • 基本上你希望用户在购物车中只添加一个产品?
  • 不,我希望他们只将来自 1 个供应商的产品添加到购物车。

标签: php wordpress woocommerce cart hook-woocommerce


【解决方案1】:

下面是WC_Cart add_to_cart() method中涉及的钩子:

A) 将商品添加到购物车之前:

  1. 验证过滤钩woocommerce_add_to_cart_validation
  2. 项目数量更改过滤器挂钩woocommerce_add_to_cart_quantity (不使用 ajax)
  3. 项目数据更改过滤器挂钩woocommerce_add_cart_item_data (不使用 ajax)
  4. 以及其他一些与“单独销售”产品相关的内容(请参阅here

A) 将商品添加到购物车后:

  1. 更换购物车物品过滤钩woocommerce_add_cart_item
  2. 添加事件,动作挂钩woocommerce_add_to_cart

在你的情况下要清楚:

  1. 正如您现在所见,woocommerce_add_to_cart 不是函数,而只是一个动作挂钩。
  2. 挂钩位置:位于WC_Cartadd_to_cart()方法内部(源代码末尾)。
  3. 当钩子被触发时:一旦WC_Cartadd_to_cart()方法被执行,它就会被触发。
  4. 目的是什么:在执行此方法时执行一些自定义代码(事件)

关于您的代码:
最好使用专用的过滤器钩子 woocommerce_add_to_cart_validation,如果购物车中已经有来自其他供应商的产品,它将阻止想要将新商品添加到购物车的客户,显示 可选自定义消息:

add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) { 
    if ( WC()->cart->is_empty() ) return $passed;

    // Get the VendorId of the product being added to the cart.
    $current_vendor = get_wcmp_product_vendors($product_id);

    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Get the vendor Id of the item
        $cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);

        // If two products do not have the same Vendor
        if( $current_vendor->id != $cart_vendor->id ) {
            // We set 'passed' argument to false
            $passed = false ;

            // Displaying a custom message
            $message = __( "This is your custom message", "woocommerce" );
            wc_add_notice( $message, 'error' );
            // We stop the loop
            break; 
        }
    }
    return $passed;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件中。

经过测试并且有效。


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2014-02-25
    • 2015-10-10
    • 1970-01-01
    • 2015-03-11
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多