WooCommerce 短代码以 ajaxify 消息显示:"Buy X more products to get a discount"
WooCommerce shortcode to ajaxify message that display: "Buy X more products to get a discount"
经过深入搜索,我创建了一个 WooCommerce 简码。这样做的目的是显示“多买 X 件产品以获得折扣”消息。
我的代码:
add_shortcode('check', 'custommessage');
function custommessage() {
$items_count = WC()->cart->get_cart_contents_count();
$min_count = 6;
if( $items_count < $min_count ){
echo sprintf('%s products more for discount' , $min_count - $items_count );
}
}
在 WooComerce 购物车页面上,将 X 件产品添加到购物车或删除数量后,它会起作用并自动刷新消息
但是当我将短代码(放在购物车旁边)时,它不会自动刷新 X 计数。虽然也是AJAX驱动
有什么解决办法的建议吗?
为了 ajax 化您的消息,以便在项目为 added/removed(通过 ajax)时更新消息,请使用 woocommerce_add_to_cart_fragments
过滤器挂钩
所以你得到:
function custom_message() {
// Initialize
$message = __( 'Default message', 'woocommerce' );
// True
if ( WC()->cart ) {
// Get number of items in the cart.
$items_count = WC()->cart->get_cart_contents_count();
$min_count = 6;
if ( $items_count < $min_count ) {
$message = sprintf( __( '%s products more for discount', 'woocommerce' ), $min_count - $items_count );
} else {
$message = __( 'Some message', 'woocommerce' );
}
}
return $message;
}
// Refreshing on cart ajax events
function filter_woocommerce_add_to_cart_fragments( $fragments ) {
$fragments['div.display-message'] = '<div class="display-message">' . custom_message() . '</div>';
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 );
1) 然后通过短代码显示它,使用:
function display_my_message() {
return '<div class="display-message">' . custom_message() . '</div>';
}
// Register shortcode
add_shortcode( 'display_message', 'display_my_message' );
SHORTCODE USAGE
In an existing page:
[display_message]
Or in PHP:
echo do_shortcode("[display_message]");
或
2) 通过 任何所需的挂钩 显示消息(在这个例子中,我显示了 2 个挂钩。1 在购物车页面上, 1在单品页面,适应您的需求)。使用:
// Display message via the desired hooks
function display_message() {
echo '<div class="display-message">' . custom_message() . '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'display_message', 10, 0 );
add_action( 'woocommerce_before_add_to_cart_form', 'display_message', 10, 0 );
经过深入搜索,我创建了一个 WooCommerce 简码。这样做的目的是显示“多买 X 件产品以获得折扣”消息。
我的代码:
add_shortcode('check', 'custommessage');
function custommessage() {
$items_count = WC()->cart->get_cart_contents_count();
$min_count = 6;
if( $items_count < $min_count ){
echo sprintf('%s products more for discount' , $min_count - $items_count );
}
}
在 WooComerce 购物车页面上,将 X 件产品添加到购物车或删除数量后,它会起作用并自动刷新消息
但是当我将短代码(放在购物车旁边)时,它不会自动刷新 X 计数。虽然也是AJAX驱动
有什么解决办法的建议吗?
为了 ajax 化您的消息,以便在项目为 added/removed(通过 ajax)时更新消息,请使用 woocommerce_add_to_cart_fragments
过滤器挂钩
所以你得到:
function custom_message() {
// Initialize
$message = __( 'Default message', 'woocommerce' );
// True
if ( WC()->cart ) {
// Get number of items in the cart.
$items_count = WC()->cart->get_cart_contents_count();
$min_count = 6;
if ( $items_count < $min_count ) {
$message = sprintf( __( '%s products more for discount', 'woocommerce' ), $min_count - $items_count );
} else {
$message = __( 'Some message', 'woocommerce' );
}
}
return $message;
}
// Refreshing on cart ajax events
function filter_woocommerce_add_to_cart_fragments( $fragments ) {
$fragments['div.display-message'] = '<div class="display-message">' . custom_message() . '</div>';
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 );
1) 然后通过短代码显示它,使用:
function display_my_message() {
return '<div class="display-message">' . custom_message() . '</div>';
}
// Register shortcode
add_shortcode( 'display_message', 'display_my_message' );
SHORTCODE USAGE
In an existing page:
[display_message]
Or in PHP:
echo do_shortcode("[display_message]");
或
2) 通过 任何所需的挂钩 显示消息(在这个例子中,我显示了 2 个挂钩。1 在购物车页面上, 1在单品页面,适应您的需求)。使用:
// Display message via the desired hooks
function display_message() {
echo '<div class="display-message">' . custom_message() . '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'display_message', 10, 0 );
add_action( 'woocommerce_before_add_to_cart_form', 'display_message', 10, 0 );