WooCommerce 短代码用于 ajaxify 显示的消息:“购买 X 个更多产品以获得折扣" [英] WooCommerce shortcode to ajaxify message that display: "Buy X more products to get a discount"

查看:32
本文介绍了WooCommerce 短代码用于 ajaxify 显示的消息:“购买 X 个更多产品以获得折扣"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过深入搜索,我创建了一个 WooCommerce 短代码.这样做的目的是显示购买 X 多产品以获得折扣".留言.

我的代码:

add_shortcode('check', 'custommessage');函数自定义消息(){$items_count = WC()->cart->get_cart_contents_count();$min_count = 6;if( $items_count <$min_count ){echo sprintf('%s 产品更多折扣' , $min_count - $items_count );}}


在 WooComerce 购物车页面上,将 X 个产品添加到购物车或移除数量后,其有效并自动刷新消息

但是当我将短代码(放在侧购物车上)时,它不会自动刷新 X 计数.虽然也是AJAX驱动的.

有什么建议可以解决这个问题吗?

解决方案

使用 woocommerce_add_to_cart_fragments 过滤器钩子

来ajax化你的消息以便在添加/删除项目时更新(通过ajax)>

所以你得到:

function custom_message() {//初始化$message = __( '默认消息', 'woocommerce' );//真的如果(WC()->购物车){//获取购物车中的商品数量.$items_count = WC()->cart->get_cart_contents_count();$min_count = 6;如果( $items_count <$min_count ){$message = sprintf( __( '%s 产品有更多折扣', 'woocommerce' ), $min_count - $items_count );} 别的 {$message = __( 'Some message', 'woocommerce' );}}返回 $message;}//刷新购物车ajax事件函数 filter_woocommerce_add_to_cart_fragments( $fragments ) {$fragments['div.display-message'] = '

'.custom_message() .'</div>';返回 $fragments;}add_filter('woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1);


1) 然后通过短代码显示,使用:

function display_my_message() {return '

'.custom_message() .'</div>';}//注册短代码add_shortcode('display_message', 'display_my_message');

<块引用>

短代码使用

在现有页面中:

[display_message]

或者在 PHP 中:

echo do_shortcode("[display_message]");

2) 要通过任何需要的钩子显示消息(在本例中,我显示了 2 个钩子.1 个在购物车页面上,1 个在单个产品页面上,调整满足您的需求).使用:

//通过所需的钩子显示消息函数显示消息(){echo '

'.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);

After a deep search I created a WooCommerce shortcode. The purpose of this is to display a "Buy X more products to get a discount" message.

My code:

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 );
    }
}


On WooComerce cart page, after adding X products to the cart or removing the quantity it works and refreshes automatically the message

But when I put the shortcode (on side cart) it does not auto refresh the X count. Although it is also AJAX driven.

Any advice how i can solve this?

解决方案

To ajaxify your message so it updates when an item is added/removed (via ajax) use the woocommerce_add_to_cart_fragments filter hook

So you get:

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) Then to display this via a shortcode, use:

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]");

OR

2) To display the message via any desired hook (in this example I show 2 hooks. 1 on the cart page and 1 on the single product page, adapt to your needs). Use:

// 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 短代码用于 ajaxify 显示的消息:“购买 X 个更多产品以获得折扣"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆