ajax - 使用 ajax 添加到购物车时如何添加 Woocommerce 通知(无需重新加载)?
问题描述
我尝试使产品页面(以及其他任何地方)上的添加到购物车按钮和通知与 ajax 一起使用。但是通知(例如,您不能将该金额添加到购物车 - 我们有 X 库存,而您的购物车中已经有 Y。)不起作用。
我如何使它工作?
这就是我现在所拥有的:
用于 AJAX 的 JS 添加到购物车处理
/**
* JS for AJAX Add to Cart handling
*/
function ace_product_page_ajax_add_to_cart_js() {
?><script type="text/javascript" charset="UTF-8">
jQuery(function($) {
$('form.cart').on('submit', function(e) {
e.preventDefault();
var form = $(this);
form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
var formData = new FormData(form.context);
formData.append('add-to-cart', form.find('[name=add-to-cart]').val() );
// Ajax action.
$.ajax({
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'ace_add_to_cart' ),
data: formData,
type: 'POST',
processData: false,
contentType: false,
complete: function( response ) {
response = response.responseJSON;
if ( ! response ) {
return;
}
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;
}
var $thisbutton = form.find('.single_add_to_cart_button'); //
var $thisbutton = null; // don't want the 'View cart' button
// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
// Remove existing notices
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
// Add new notices
form.closest('.product').before(response.fragments.notices_html)
form.unblock();
}
});
});
});
</script><?php
}
add_action( 'wp_footer', 'ace_product_page_ajax_add_to_cart_js' );
添加到购物车处理程序:
/**
* Add to cart handler
*/
function ace_ajax_add_to_cart_handler() {
WC_Form_Handler::add_to_cart_action();
WC_AJAX::get_refreshed_fragments();
}
add_action( 'wc_ajax_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
add_action( 'wc_ajax_nopriv_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
// Remove WC Core add to cart handler to prevent double-add
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
为通知添加片段:
/**
* Add fragments for notices
*/
function ace_ajax_add_to_cart_add_fragments( $fragments ) {
$all_notices = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
ob_start();
foreach ( $notice_types as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
wc_get_template( "notices/{$notice_type}.php", array(
'messages' => array_filter( $all_notices[ $notice_type ] ),
) );
}
}
$fragments['notices_html'] = ob_get_clean();
wc_clear_notices();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'ace_ajax_add_to_cart_add_fragments' );
解决方案
尝试在最后一个片段中替换'messages' => array_filter( ...
它'notices' => array_filter( ...
应该可以工作。
推荐阅读
- reactjs - 反应表单输入字段不会让我改变值
- npm - 如何在 jsontohtml node-js (npm) 模块中为按钮添加点击事件
- python - Gremlin Python - “服务器已断开 - 请尝试重新连接”错误
- sql - 如何使用 SQL 查询删除具有固定日期但可变时间的某些行?
- python - 使用 Python 解析时处理文件格式更改的最佳方法?
- wordpress - 将 LinkedIn 帖子嵌入 Wordpress 博客
- rust - 如何将泛型函数转换为带有引用参数的函数指针?
- fetch - Mirage Passthrough 返回响应对象而不是从网络调用返回的 JSON 数据
- c# - 从 RethinkDb 读取二进制文件
- typescript - 循环内带有字符串的 TypeScript 索引对象