Javascript 如何在woocommerce中通过ajax调用从前端添加产品
我使用下面的代码添加产品(自定义新产品到购物车中,该购物车不存在于products表中)Javascript 如何在woocommerce中通过ajax调用从前端添加产品,javascript,ajax,wordpress,woocommerce,Javascript,Ajax,Wordpress,Woocommerce,我使用下面的代码添加产品(自定义新产品到购物车中,该购物车不存在于products表中) 我希望上述代码以ajax方式工作,这意味着在向购物车添加产品和更新产品时,页面不得刷新。您面临什么问题?你没有在问题中描述这个问题。 //No Errors Found if ( $error_pTitle == "" && $foundProduct=="") { //echo "No error Completed Validatio
我希望上述代码以ajax方式工作,这意味着在向购物车添加产品和更新产品时,页面不得刷新。您面临什么问题?你没有在问题中描述这个问题。
//No Errors Found
if ( $error_pTitle == "" && $foundProduct=="") {
//echo "No error Completed Validation of fields";
//add them in an array
$post = array(
'post_status' => "publish",
'post_title' => $customProductTitle,
'post_type' => "product",
);
//create product for product ID
$product_id = wp_insert_post( $post, __('Cannot create product', 'izzycart-function-code') );
//type of product
wp_set_object_terms($product_id, 'simple', 'product_type');
//Which Category
wp_set_object_terms( $product_id, $product_CO_terms, 'product_cat' );
//add product meta
update_post_meta( $product_id, '_regular_price', $customProductPrice );
update_post_meta( $product_id, '_price', $customProductPrice );
update_post_meta( $product_id, '_visibility', 'visible' );
update_post_meta( $product_id, '_stock_status', 'instock' );
$product = wc_get_product( $product_id );
//Add Product To the customers cart once the Product is added
$found = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id,$customProductQty );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id,$customProductQty );
}
}
elseif($foundProduct!=""){
echo $foundProduct;
}
else
{
echo "error cant add product ";
echo $error_pTitle ;
}
}