小编典典

您可以直接在代码中调查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' ) );

它的回调距离文件更远:

/**

* 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();

}

如果要查看其添加到购物车ajax调用,则该JS位于assest/js/frontend/add-to-cart.js。

编辑

现在,我知道您正在寻找添加变体的方法, 也许 我们可以对以上内容进行调整。

首先,我认为您需要将AJAX网址传递给脚本:

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 );

然后,您的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"

});

基本上,要添加特定的变体,除了所有特定选项外,还需要变体的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();

}

通常,我只是复制WooCommerce的方法,并添加添加变体所需的2个变量。完全未经测试,但希望对您有所帮助。

2020-07-26

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐