[英]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.