Spr*_*der 11 javascript ajax woocommerce
我有一个包含以下数据的项目:
var item = {
id : "124",
name : "xxx",
price : "13.13",
quantity : 1,
options : {
"size" : "xl",
"color": "pink"
}
};
Run Code Online (Sandbox Code Playgroud)
当用户点击"添加到购物车"时,我想使用WC API制作Ajax请求,并将上述项目添加到购物车.
jQuery.ajax({
url: "some/woocommerce/api/add/to/cart/request/path",
data: item,
type: "POST"
});
Run Code Online (Sandbox Code Playgroud)
然后在购物车页面上,我想使用WC API制作另一个Ajax请求并检索购物车的内容.
我没有找到任何关于如何使用Javascript从客户端执行此操作的文档(官方或非官方).
有谁知道并且能为我提供一个例子吗?
此外,有谁知道为什么WooCommerce Api文档是如此可怕(缺乏有关如上所述的明显/标准问题的任何信息).我正在认真考虑让我们公司改用Shopify.
hel*_*ing 24
您可以调查WooCommerce如何通过代码中的ajax直接向购物车添加商品....回调位于includes/class-wc-ajax.php.WooCommerce已经在产品"循环"(产品档案)上做了这个,所以我认为你不需要重新发明轮子,如果他们现有的代码不能用于你想要做的事情,那么你应该能够借用很重要的.
该文件的开头有一个包含所有WooCommerce Ajax操作的循环,但我们可以追踪到添加到购物车基本上是这样的:
add_action( 'wp_ajax_nopriv_woocommerce_add_to_cart', array( 'WC_AJAX', 'add_to_cart' ) );
Run Code Online (Sandbox Code Playgroud)
它的回调位于文件的下方:
/**
* AJAX add to cart
*/
public static function add_to_cart() {
ob_start();
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data );
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
wc_add_to_cart_message( $product_id );
}
// Return fragments
self::get_refreshed_fragments();
} else {
// If there was an error adding to the cart, redirect to the product page to show any errors
$data = array(
'error' => true,
'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
);
wp_send_json( $data );
}
die();
}
Run Code Online (Sandbox Code Playgroud)
如果你想查看他们的添加到购物车ajax调用,那里的JS就位于assest/js/frontend/add-to-cart.js.
编辑
现在我知道你想要添加一个变体,也许我们可以调整上面的内容.
首先,我认为你需要将AJAX url传递给你的脚本:
wp_enqueue_script( 'wc-variation-add-to-cart', 'source-to-script/your-script.js' );
$vars = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'wc-variation-add-to-cart', 'WC_VARIATION_ADD_TO_CART', $vars );
Run Code Online (Sandbox Code Playgroud)
然后你的AJAX调用看起来像这样:
jQuery.ajax({
url: WC_VARIATION_ADD_TO_CART.ajax_url,
data: {
"action" : "woocommerce_add_variation_to_cart",
"product_id" : "124",
"variation_id" : "125",
"quantity" : 1,
"variation" : {
"size" : "xl",
"color": "pink"
},
},
type: "POST"
});
Run Code Online (Sandbox Code Playgroud)
基本上要添加特定变体,除了所有特定选项外,还需要变体ID.
最后,woocommerce_add_variation_to_cartajax操作的新回调将遵循以下方式:
add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );
function so_27270880_add_variation_to_cart() {
ob_start();
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
$variation_id = isset( $_POST['variation_id'] ) ? absint( $_POST['variation_id'] ) : '';
$variations = ! empty( $_POST['variation'] ) ? (array) $_POST['variation'] : '';
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data );
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
wc_add_to_cart_message( $product_id );
}
// Return fragments
WC_AJAX::get_refreshed_fragments();
} else {
// If there was an error adding to the cart, redirect to the product page to show any errors
$data = array(
'error' => true,
'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
);
wp_send_json( $data );
}
die();
}
Run Code Online (Sandbox Code Playgroud)
大多数情况下,我只是复制WooCommerce的方法并添加添加变体所需的2个变量.完全未经测试,但我希望它有所帮助.
小智 5
如果有人感兴趣,我还编写了一个“从购物车中删除”功能来对抗添加到购物车。我的项目要求在结账时使用单选按钮,所以我需要添加一个产品并删除它已经存在的替代品:
这是php:
function remove_from_cart( $product_id ) {
$cart_contents = WC()->cart->get_cart();
if (empty($product_id)) { $product_id = $_POST['product_id']; }
foreach ($cart_contents as $product_key => $product){
if ($product['variation_id'] == $product_id){
WC()->cart->remove_cart_item($product_key);
}
}
}
add_action( 'wp_ajax_nopriv_remove_from_cart', 'remove_from_cart' );
Run Code Online (Sandbox Code Playgroud)
然后是 jQuery:
function remove_item_from_cart(product){
var data = {
"action" : "remove_from_cart",
"product_id" : product,
};
jQuery.post(WC_VARIATION_ADD_TO_CART.ajax_url, data, function(response) {
$(document.body).trigger("update_checkout");
});
}
Run Code Online (Sandbox Code Playgroud)
php 中的 if(empty)... 这样我也可以在需要时从服务器端调用该函数。
| 归档时间: |
|
| 查看次数: |
21800 次 |
| 最近记录: |