Get_refreshed_fragments là gì

  • it is making 2 calls of wc-ajax=get_refreshed_fragments if nothing is in the cart.

    Why? and can it be changed to 1?

    To make this issue apear enable a theme like twenty18 and woocs and woocommerce

  • Viewing 1 replies [of 1 total]

    Viewing 1 replies [of 1 total]

    Sao chép mã này vào tệp của bạn. Ví dụ: my-theme-wc-single-ajax-add-cart.js .

    function myThemeWc_SingleProductAddToCart[thisObj] { if [typeof[$] === 'undefined'] { var $ = jQuery.noConflict[]; } var thisForm = thisObj.closest['form']; var button = thisForm.find['.button']; var formUrl = thisForm.attr['action']; var formMethod = thisForm.attr['method']; if [typeof[formMethod] === 'undefined' || formMethod == ''] { formMethod = 'POST'; } var formData = new FormData[thisForm[0]]; formData.append[button.attr['name'], button.val[]]; button.removeClass['added']; button.addClass['loading']; myThemeWc_SingleProductCartAjaxTask = $.ajax[{ url: formUrl, method: formMethod, data: formData, cache: false, contentType: false, processData: false }] .done[function[data, textStatus, jqXHR] { $[document.body].trigger['wc_fragment_refresh']; $.when[myThemeWc_SingleProductCartAjaxTask] .then[myThemeWc_SingleProductUpdateCartWidget] .done[function[] { button.removeClass['loading']; button.addClass['added']; setTimeout[function[] { button.removeClass['added']; }, 2000]; }]; }] .fail[function[jqXHR, textStatus, errorThrown] { button.removeClass['loading']; }] .always[function[jqXHR, textStatus, errorThrown] { $['.cart'].off['submit']; myThemeWc_SingleProductListenAddToCart[]; }]; }// myThemeWc_SingleProductAddToCart function myThemeWc_SingleProductListenAddToCart[] { if [typeof[$] === 'undefined'] { var $ = jQuery.noConflict[]; } $['.cart'].on['submit', function[e] { e.preventDefault[]; myThemeWc_SingleProductAddToCart[$[this]]; }]; }// myThemeWc_SingleProductListenAddToCart /** * Update WooCommerce cart widget by called the trigger and listen to the event. * * @returns {undefined} */ function myThemeWc_SingleProductUpdateCartWidget[] { if [typeof[$] === 'undefined'] { var $ = jQuery.noConflict[]; } var deferred = $.Deferred[]; $[document.body].on['wc_fragments_refreshed', function[] { deferred.resolve[]; }]; return deferred.promise[]; }// myThemeWc_SingleProductUpdateCartWidget var myThemeWc_SingleProductCartAjaxTask; // on page load -------------------------------------------- jQuery[function[$] { $[document.body].on['wc_fragments_refreshed', function[] { console.log['woocommerce event fired: wc_fragments_refreshed']; }]; myThemeWc_SingleProductListenAddToCart[]; }];

    Bạn có thể cần thay thế tiền tố hàm, biến myThemeWc_thành những gì bạn muốn.

    Mã này sử dụng nút thêm vào trang sản phẩm đơn lẻ WooCommerce ban đầu nhưng dừng chức năng của nó và sau đó sử dụng ajax thay thế bằng cách giữ nguyên tất cả các giá trị trong biểu mẫu.

    Sau đó, xếp hàng đợi tệp js này.

    add_action['wp_enqueue_scripts', 'mythemewc_enqueue_scripts']; function mythemewc_enqueue_scripts[] { if [class_exists['\\WooCommerce'] && is_product[]] { wp_enqueue_script['mythemewc-single-product', trailingslashit[get_stylesheet_directory_uri[]] . 'assets/js/my-theme-wc-single-ajax-add-cart.js', ['jquery'], false, true]; } }

    Bạn có thể phải viết mã nút css của mình để làm cho nó hiển thị biểu tượng đang tải, đã thêm.
    Đây là css.

    .woocommerce #respond input#submit.added::after, .woocommerce a.btn.added::after, .woocommerce button.btn.added::after, .woocommerce input.btn.added::after, .woocommerce .single_add_to_cart_button.added::after { font-family: WooCommerce; content: '\e017'; margin-left: .53em; vertical-align: bottom; } .woocommerce #respond input#submit.loading, .woocommerce a.btn.loading, .woocommerce button.btn.loading, .woocommerce input.btn.loading, .woocommerce .single_add_to_cart_button.loading { opacity: .25; padding-right: 2.618em; position: relative; } .woocommerce #respond input#submit.loading::after, .woocommerce a.btn.loading::after, .woocommerce button.btn.loading::after, .woocommerce input.btn.loading::after, .woocommerce .single_add_to_cart_button.loading::after { font-family: WooCommerce; content: '\e01c'; vertical-align: top; font-weight: 400; position: absolute; right: 1em; -webkit-animation: spin 2s linear infinite; animation: spin 2s linear infinite; }

    2 hữu ích 0 bình luận chia sẻ

    Chủ Đề