Javascript Woocommerce添加到具有post_id的购物车
我正在使用woocommerce动态制作产品,需要将它们添加到购物车中。我有一个ajax函数,在php插件文件中调用此函数:Javascript Woocommerce添加到具有post_id的购物车,javascript,php,wordpress,woocommerce,plugins,Javascript,Php,Wordpress,Woocommerce,Plugins,我正在使用woocommerce动态制作产品,需要将它们添加到购物车中。我有一个ajax函数,在php插件文件中调用此函数: $name = $_POST['name']; $price = $_POST['price']; $post_id = wp_insert_post( array( 'post_title' => 'new Product', 'post_content' =>
$name = $_POST['name'];
$price = $_POST['price'];
$post_id = wp_insert_post( array(
'post_title' => 'new Product',
'post_content' => 'here is product content',
'post_status' => 'publish',
'post_type' => "product",
'ID' => 1,
) );
wp_set_object_terms( $post_id, 'simple', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_virtual', 'yes' );
update_post_meta( $post_id, '_regular_price', $price );
update_post_meta( $post_id, '_sale_price', '' );
update_post_meta( $post_id, '_purchase_note', '' );
update_post_meta( $post_id, '_featured', 'no' );
update_post_meta( $post_id, '_weight', '' );
update_post_meta( $post_id, '_length', '' );
update_post_meta( $post_id, '_width', '' );
update_post_meta( $post_id, '_height', '' );
update_post_meta( $post_id, '_sku', '' );
update_post_meta( $post_id, '_product_attributes', array() );
update_post_meta( $post_id, '_sale_price_dates_from', '' );
update_post_meta( $post_id, '_sale_price_dates_to', '' );
update_post_meta( $post_id, '_price', '' );
update_post_meta( $post_id, '_sold_individually', '' );
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, 'product_id', '444' );
update_post_meta( $post_id, '_stock', '' );
WC()->cart->add_to_cart($post_id);
}
这会创建一个新产品,但不会将其添加到购物车中。有人知道为什么吗?下面的代码可以工作
$post_id = wp_insert_post( array(
'post_title' => 'new Product',
'post_content' => 'here is product content',
'post_status' => 'publish',
'post_type' => "product"
) );
WC()->cart->add_to_cart($post_id);
为什么您要将ID指定为静态?嗨,谢谢您的时间,我尝试了这个,但没有为我工作。你知道我是否需要用woocommerce初始化任何东西来使用WC()函数,以便它实际推送到购物车上吗?