加入购物车"ajax
Woocommerce "Add to cart" ajax
在产品类别页面上,当用户点击"Add to cart"时,woocommerce通过Ajax在该按钮下方添加了"View cart"。我发现处理这个的脚本是/assets/js/frontend/add-to-cart.js
现在,我还想添加"proceed to checkout",这样就可以立即去结帐了。
这是脚本的输出:
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;
});
有没有人做过类似的事情?
如果你从Woocommerce的repo查看这里,你可以看到add-to-cart.js是从那个类本地化的。
不幸的是,没有过滤器来添加您自己的链接。你可以尝试将add-to-cart.js复制到你的主题中,并使用此方法将注册的add-to-cart.js的新src设置为新的本地副本。
从那里你可以改变这个条件在Woocommerce仓库中找到。
所以,从技术上讲,你可以这样做,但是有一些注意事项:
- 您需要对变型产品重复此过程
- 如果翻译是一个问题,你也需要解决
- 任何时候插件更新,你现在必须梳理这些文件的任何差异,可能破坏功能或导致安全问题。
相关文章:
- "日期“;AJAX请求返回的类型值未定义
- "资源不足错误“;当从Chrome控制台重复发出ajax请求时
- "未捕获的SyntaxError:意外的令牌ILLEGAL“;当我通过AJAX发送html时
- 如何在“;beforeSend:function()"使用ajax和jquery
- $.Ajax投掷“;无效的参数"在IE9上
- 得到"parserror”;在jquery ajax中
- "显示更多“<h: 命令按钮>使用ajax
- "堆叠外空间”;同时通过AJAX调用填充jsTree
- "r 〃;在jquery-ajax-html的警报中
- 如何在ajax中返回文本文件值Asynchronous"Synchronous正在被弃用”;
- 得到"来自(127.0.0.1'33187)“;当使用$.ajax执行get请求时
- "SyntaxError:JSON.parse:意外字符"将多个变量从AJAX传递到PHP时出错
- 如何获得"这个值“;在jQuery.ajax上
- 得到"否'访问控制允许来源'标头存在于请求的资源上"对于我的JS AJAX而不是“;
- 使用ajax打印“;你好,世界"当你点击一个按钮时
- "对象没有't支持这一行动”;请致电Ajax.autocompleter
- 如何"递归AJAX回调”;在JavaScript工作中
- "风格"显示AJAX弹出窗口(vb.net)时出错
- javascript+jquery-ajax:有条件的"数据“;要素
- "重新打开最后一个关闭的选项卡“;导致显示最后一个ajax请求内容