根据购物车总 woocommerce 在结帐页面上显示 html

时间:2021-05-04 22:12:51

标签: php wordpress woocommerce

我在结帐页面上将“特殊”产品添加到我的购物车中,只有当购物车价值达到 90 或以上时,产品才可用,当价值不正确时,我将产品隐藏在页面上,但我不能将产品添加到将购物车的价值提高到 90 以上的项目时如何。这是我当前的 ajax,

$(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;
}

0 个答案:

没有答案