简体   繁体   English

Woocommerce多个产品无法购物?

[英]Woocommerce multiple products to cart not working?

I am having problem with adding multiuple custom products to woocommerce cart. 我在将多个自定义产品添加到woocommerce购物车时遇到问题。 How to add the multiple products to cart programatically. 如何以编程方式将多个产品添加到购物车。 Please check with my below code and advise on this. 请检查我下面的代码,并为此提供建议。 I am having this code in custom page which is template folder. 我在自定义页面(模板文件夹)中有此代码。

$values = array();
         $post_values = array();
         $i=0;
    foreach($_POST['post_quantity'] as $key =>$value){


   /*$lastid[$i] = multiple product inserted id's */


$wpdb->insert('wp_posts', array(
      'post_title'    => 'xxxxx  xxx ID: '.$_POST['quoted_sid'][$key].'  - Product ID: '.$_POST['product_id'][$key],
      'post_content'  => $_POST['product_description'][$key],
      'post_date' => date('Y-m-d H:i:s'),
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    ));
    $lastid[$i] = $wpdb->insert_id;

            $date[$i] = date('Y-m-d H:i:s');

        add_post_meta($lastid[$i], '_regular_price', $_POST['product_price'][$key]);
        add_post_meta($lastid[$i], '_price', $_POST['product_price'][$key]);
        add_post_meta($lastid[$i],'_visibility','visible');

    add_post_meta($lastid[$i], '_stock_status', 'instock' );
    add_post_meta($lastid[$i], '_weight', '11' );
    add_post_meta($lastid[$i], '_sku', 'Quoted xxxx_'.$_POST['product_id'][$key] );
    add_post_meta($lastid[$i], '_duplicate_price', $_POST['product_duplicate_price'][$key] );
    add_post_meta($lastid[$i], '_material', $_POST['product_material'][$key] );

$i++;
        //$woocommerce->cart->add_to_cart(  $lastid[$i], $quantity=$_POST['post_quantity'][$key], $cart_item_data ); 

    }

    $woocommerce->cart->add_to_cart(  $lastid[$i], 
        $quantity=$_POST['post_quantity'][$key], $cart_item_data ); 

I tried like this the below function is working but i am not sure how to add quanity and other cart_item_data_fields 我试图这样下面的功能正在工作,但我不确定如何添加数量和其他cart_item_data_fields

$product_ids = filter_var_array( $lastid, FILTER_SANITIZE_SPECIAL_CHARS );
        foreach ( $product_ids as $product_id ) {
            $woocommerce->cart->add_to_cart( $product_id );
        } 

How to pass the quantity and other cart_item_data fields. 如何传递数量和其他cart_item_data字段。

You are passing the value of the variable assignment of quantity in the function invocation. 您正在函数调用中传递数量的变量赋值。 So the value is just false if the assignment result returns false. 因此,如果赋值结果返回false,则该值为false。 You need to either pass in a pre-assigned variable or the actual expression value. 您需要传递预分配的变量或实际的表达式值。

(When you are declaring function definition, you can assign function arguments to "constant" values for defaults.) (声明函数定义时,可以将函数参数分配给默认的“恒定”值。)

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

相关问题 在访问WooCommerce时自动将多种产品添加到购物车 - Automatically add multiple products to cart on visit WooCommerce woocommerce 购物车,带有多个小计来自相同产品的多个价格 - woocommerce cart with multiple subtotals from same products with multiple prices 无法在购物车中的WooCommerce中添加多个可变订阅产品 - Unable to add multiple variable subscription products in WooCommerce in Cart WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations 使用 SKU 和 URL 参数一次将多个产品添加到 WooCommerce 购物车 - Adding multiple products to WooCommerce cart at once with SKU and URL parameters WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart Woocommerce可变产品未添加到购物车 - Woocommerce Variable Products not adding to the Cart WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 将多种产品添加到购物车 - Add multiple products to cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM