读取源代码。来自woocommerce ajax。php(在插件的根文件夹中)
Javascript/Ajax add to cart
/**
* AJAX add to cart
*
* @access public
* @return void
*/
function woocommerce_ajax_add_to_cart() {
global $woocommerce;
check_ajax_referer( \'add-to-cart\', \'security\' );
$product_id = (int) apply_filters(\'woocommerce_add_to_cart_product_id\', $_POST[\'product_id\']);
$passed_validation = apply_filters(\'woocommerce_add_to_cart_validation\', true, $product_id, 1);
if ($passed_validation && $woocommerce->cart->add_to_cart($product_id, 1)) :
// Return html fragments
$data = apply_filters(\'add_to_cart_fragments\', array());
do_action( \'woocommerce_ajax_added_to_cart\', $product_id);
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\' => get_permalink( $product_id )
);
$woocommerce->set_messages();
endif;
echo json_encode( $data );
die();
}
add_action(\'wp_ajax_woocommerce_add_to_cart\', \'woocommerce_ajax_add_to_cart\');
add_action(\'wp_ajax_nopriv_woocommerce_add_to_cart\', \'woocommerce_ajax_add_to_cart\');
Non-AJAX: Requires Page Load
如果您没有使用AJAX,那么您使用的是
woocommerce_add_to_cart_action()
来自woocommerce功能。在
init
钩它有点长,所以我将让您从提到的文件中读取它。基本上,它会查找add-to-cart的查询参数(应该等于产品ID)并从那里开始。