Php 在基于购物车的结帐页面上显示html
我在结帐页面将“特殊”产品添加到我的购物车中,只有当购物车的价值达到90或以上时,这些产品才可用。当价值不正确时,我在页面上隐藏了这些产品,但我无法在添加将购物车的价值提高到90以上的项目时获取这些产品。这是我目前的ajaxPhp 在基于购物车的结帐页面上显示html,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在结帐页面将“特殊”产品添加到我的购物车中,只有当购物车的价值达到90或以上时,这些产品才可用。当价值不正确时,我在页面上隐藏了这些产品,但我无法在添加将购物车的价值提高到90以上的项目时获取这些产品。这是我目前的ajax $(document).on('change', '.gift_select_qty', function(e){ e.preventDefault(); var $thisbutton = $(this), product_qty = $(
$(document).on('change', '.gift_select_qty', function(e){
e.preventDefault();
var $thisbutton = $(this),
product_qty = $(this).val(),
product_id = $(this).data('product_id');
if(product_qty > 0) {
var data = {
action: 'woocommerce_ajax_add_to_cart',
product_id: product_id,
product_sku: '',
quantity: product_qty,
};
var url = wc_add_to_cart_params.ajax_url
} else {
var data = {
action: 'remove_item_from_cart',
product_id: product_id,
product_sku: '',
quantity: product_qty
};
var url = wc_add_to_cart_params.ajax_url;
}
$(document.body).trigger('added_to_cart', [$thisbutton, data]);
$.ajax({
type: 'post',
url: url,
data: data,
success: function (response) {
if (response.error && response.product_url) {
window.location = response.product_url;
return;
} else {
$(document.body).trigger('update_checkout', [response.fragments, response.cart_hash, $thisbutton]);
}
},
});
return false;
});
以及它调用的PHP
function woocommerce_ajax_add_to_cart() {
$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();
}
是否有一种方法可以在ajax响应中获得购物车总数,我已经尝试将其与下面的代码挂钩,但它不起作用
add_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
$fragments['cart_total'] = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total()));
return $fragments;
}