[英]Adding item to woocommerce cart
在編寫插件時,我正在嘗試將項目添加到 Ajax 中的 WooCommerce 購物車。
我在我的主要 my-plugin.php 中添加了以下幾行
add_action( 'wp_ajax_add_items_to_cart', 'ajax_add_items_to_cart' );
add_action( 'wp_ajax_nopriv_add_items_to_cart', 'ajax_add_items_to_cart' );
function ajax_add_items_to_cart() {
global $woocommerce;
$product_id = absint( $_POST['product_id'] );
$quantity = absint( $_POST['quantity'] );
$variation = 0;
$cart_item_data['key'] = sanitize_text_field( $_POST['key'] );
$cart_item_data['other_data'] = sanitize_text_field( $_POST['other_data'] );
$cart_item_key = $woocommerce->cart->add_to_cart($product_id, $quantity, $variation, $cart_item_data);
wp_die();
}
但購物車仍然是空的。 任何提示?
好的,事實上,這段代碼沒有問題,我的問題出在代碼的其他地方,我向 WooCommerce 發送了一個未知的 product_id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.