【问题标题】:Woocommerce "Add to cart" ajaxWoocommerce“添加到购物车”ajax
【发布时间】:2015-08-18 07:29:47
【问题描述】:

在产品分类页面,当有人点击“加入购物车”时,woocommerce通过Ajax在该按钮下方添加“查看购物车”。我发现处理这个的脚本是/assets/js/frontend/add-to-cart.js

现在,我还想添加“继续结帐”,以便有人可以立即结帐。

这是脚本的输出:

jQuery( function( $ ) {

// wc_add_to_cart_params is required to continue, ensure the object exists
if ( typeof wc_add_to_cart_params === 'undefined' )
    return false;

// Ajax add to cart
$( document ).on( 'click', '.add_to_cart_button', function(e) {

// AJAX add to cart request
var $thisbutton = $( this );

if ( $thisbutton.is( '.product_type_simple' ) ) {

    if ( ! $thisbutton.attr( 'data-product_id' ) )
        return true;

    $thisbutton.removeClass( 'added' );
    $thisbutton.addClass( 'loading' );

    var data = {
        action: 'woocommerce_add_to_cart',
    };

    $.each( $thisbutton.data(), function( key, value ) {
        data[key] = value;
    });

    // Trigger event
    $( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );

    // Ajax action
    $.post( wc_add_to_cart_params.ajax_url, data, function( response ) {

        if ( ! response )
            return;

        var this_page = window.location.toString();

        this_page = this_page.replace( 'add-to-cart', 'added-to-cart' );

        if ( response.error && response.product_url ) {
            window.location = response.product_url;
            return;
        }

        // Redirect to cart option
        if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {

            window.location = wc_add_to_cart_params.cart_url;
            return;

        } else {

            $thisbutton.removeClass( 'loading' );

            fragments = response.fragments;
            cart_hash = response.cart_hash;

            // Block fragments class
            if ( fragments ) {
                $.each( fragments, function( key, value ) {
                    $( key ).addClass( 'updating' );
                });
            }

            // Block widgets and fragments
            $( '.shop_table.cart, .updating, .cart_totals' ).fadeTo( '400', '0.6' ).block({
                message: null,
                overlayCSS: {
                    opacity: 0.6
                }
            });

            // Changes button classes
            $thisbutton.addClass( 'added' );

            // View cart text
            if ( ! wc_add_to_cart_params.is_cart && $thisbutton.parent().find( '.added_to_cart' ).size() === 0 ) {
                $thisbutton.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
                    wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
            }

            // Replace fragments
            if ( fragments ) {
                $.each( fragments, function( key, value ) {
                    $( key ).replaceWith( value );
                });
            }

            // Unblock
            $( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();

            // Cart page elements
            $( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {

                $( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();

                $( 'body' ).trigger( 'cart_page_refreshed' );
            });

            $( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
                $( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
            });

            // Trigger event so themes can refresh other areas
            $( 'body' ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
        }
    });

    return false;

}

return true;
});

有没有人做过类似的事情?

【问题讨论】:

  • 将商品添加到购物车后,会出现一个链接,将用户带到购物车。你想做什么不同的事情?
  • 有购物篮,有结账。结帐是付款过程的最后一步 1.nanshy.com/basket 2.nanshy.com/checkout 我想立即带用户结帐或购物。所以基本上我只需要在“查看购物篮”下方再添加一个链接

标签: javascript ajax wordpress woocommerce cart


【解决方案1】:

如果您从 Woocommerce 存储库中查看 here,您会看到 add-to-cart.js 是从该类本地化的。

很遗憾,没有过滤器可以只添加您自己的链接。您可以尝试将 add-to-cart.js 复制到您的主题,并使用 this method 将已注册的 add-to-cart.js 的新 src 设置为您的新本地副本。

您可以从那里更改在 Woocommerce 存储库中找到的 this conditional

所以,技术上是的,你可以这样做,但有一些警告:

  • 您需要对变体产品重复此过程
  • 如果翻译是一个问题,您也需要解决这个问题
  • 插件更新时,您现在必须梳理这些文件,找出可能破坏功能或导致安全问题的任何差异。

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2013-07-15
    • 2014-02-25
    • 2015-10-10
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多