Woocommerce挂钩,以手动方式将当前产品以及其他产品添加到购物车(以编程方式)

时间:2019-01-03 13:29:14

标签: php wordpress woocommerce

我想在购物车中添加两个产品,但是我想仅在用户按下“添加到购物车”并且当前产品已添加到购物车时才触发第二个产品的功能。

  • 用户在一种产品上按下了“添加到购物车”
  • 针对用户操作完成“添加到购物车”后,从$_POST[]数据中添加一种产品。

我正在使用以下代码,但它与使用此钩子的其他一些代码搞混了:woocommerce_add_to_cart

add_filter( 'woocommerce_add_to_cart_handler', 'sec_product_cart', 100, 2 );
        function sec_product_cart($adding_to_cart_get_type, $adding_to_cart) {
            // Grab all the optional items
            $exp = '/^optional_item_(\d*)$/';
            $values = Array();
            foreach( $_POST as $key => $val )
            {
                $match = Array();
                //If this is one of the item name variables
                if( preg_match( $exp, $key, $match ) )
                {

                    if($_POST[$key] > 0){
                        $cart_item_id = str_replace("optional_item_","", $key);
                        // Add item and the quanity to the cart
                        WC()->cart->add_to_cart( $cart_item_id, $_POST[$key] ); 
                        // $values[] = Array($cart_item_id, $_POST[$key]);
                    }
                }
            }
        }

0 个答案:

没有答案
相关问题