从昨天开始,我就一直在处理这个问题,编写并重写代码来修复这个400 XRH错误。我找不到办法来修理它。
在向您展示代码之前,下面是一些上下文:
我正在尝试将可变产品添加到归档循环中。我已经成功地将变体选择表单带到了那里,我可以使用AJAX发送正确的数据。问题似乎与请求有关。
这是我的代码:
// The main php function
function variation_add_to_cart_product(){
$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 = absint($_POST[\'variation_id\']);
$passed_validation = apply_filters(\'woocommerce_add_to_cart_validation\', true, $product_id, $quantity);
$product_status = get_post_status($product_id);
if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && \'publish\' === $product_status) {
do_action(\'woocommerce_ajax_added_to_cart\', $product_id);
if (\'yes\' === get_option(\'woocommerce_cart_redirect_after_add\')) {
wc_add_to_cart_message(array($product_id => $quantity), true);
}
WC_AJAX :: get_refreshed_fragments();
} else {
$data = array(
\'error\' => true,
\'product_url\' => apply_filters(\'woocommerce_cart_redirect_after_error\', get_permalink($product_id), $product_id));
echo wp_send_json($data);
}
wp_die();
}
// Its actions
add_action( \'wp_ajax_variation_add_to_cart_product\', \'variation_add_to_cart_product\' );
add_action( \'wp_ajax_nopriv_variation_add_to_cart_product\', \'variation_add_to_cart_product\' );
// The loading of the javascript file and the ajax url
add_action( \'wp_enqueue_scripts\', \'vl_load_js_script\' );
function vl_load_js_script() {
wp_register_script(\'vl_script\', plugins_url( \'js/vl-js.js\', __FILE__ ));
wp_enqueue_script(\'vl_script\');
$vl_ajax_array = array(
\'ajax_url\' => admin_url( \'admin-ajax.php\' )
);
// pass Ajax Url to script.js
wp_localize_script(\'vl_script\', \'vl_script\', $vl_ajax_array );
}
// The jQuery AJAX function
jQuery(function ($) {
// Targeting the variation add_to_cart button
$(document).on(\'click\', \'.vl_add_to_cart_button\', function (e) {
e.preventDefault();
// Selecting the clicked button
var $thisbutton = $(this),
// Selecting the variation form
$form = $thisbutton.parents(\'.woo-entry-inner\').find(\'.woocommerce-variation-add-to-cart-enabled\'),
// Selecting the variation\'s product id
product_id = $form.find(\'input[name=product_id]\').val(),
// Selecting the variation\'s id
variation_id = $form.find(\'input[name=variation_id]\').val(),
// Selecting the variation\'s quantity
variation_qty = $form.find(\'input[name=variation_qty]\').val();
// Creating the data array to send the data
var data = {
action: \'vl_ajax_add_to_cart\',
product_id: product_id,
product_sku: \'\',
quantity: variation_qty,
variation_id: variation_id,
};
$(document.body).trigger(\'adding_to_cart\', [$thisbutton, data]);
$.ajax({
type: \'post\',
url: vl_script.ajax_url,
data: data,
beforeSend: function (response) {
$thisbutton.removeClass(\'added\').addClass(\'loading\');
},
complete: function (response) {
$thisbutton.addClass(\'added\').removeClass(\'loading\');
},
success: function (response) {
if (response.error && response.product_url) {
window.location = response.product_url;
return;
} else {
$(document.body).trigger(\'added_to_cart\', [response.fragments, response.cart_hash, $thisbutton]);
}
},
});
return false;
});
});
发送的请求有效负载为
action=vl_ajax_add_to_cart&product_id=3468&product_sku=&quantity=5&variation_id=5249
.
答案是0
.
ajax地址为localhost/biotruck/wp-admin/admin-ajax.php
.