在 WooCommerce 感谢页面中显示优惠券使用次数和限制

问题描述

我想在 WooCommerce 感谢页面上显示优惠券的剩余数量

例如:您使用了测试优惠券3次,还剩10次

这是我的代码:

add_action('woocommerce_after_cart_table','coupon_count');
function coupon_count() {
    global $woocommerce;

    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
         $my_coupon = $woocommerce->cart->get_coupons() ;
         foreach($my_coupon as $coupon){

            if ( $post = get_post( $coupon->id ) ) {

                        $counter = $coupon->get_usage_count();
                        echo "<span class='name-coupon'><b>Total usage for coupon </b><b>'</b><b>".$coupon->code."</b><b>'</b><b>: </b></span>";
                        echo "<span class='coupon-counter'>".($counter)."</span>";

            }
        }
    }
}

但我有两个问题:

1.此代码只显示使用次数,不显示剩余次数。

2.在感谢页面用woocommerce_after_cart_table替换woocommerce_thankyou不会执行代码。

解决方法

  • 在感谢页面上使用订单对象而不是购物车对象。所以我们将使用那个对象来代替

  • 您可以使用 get_usage_limit_per_user() 获取每位客户的优惠券使用限制(针对单个客户)

  • OR 使用 get_usage_limit() 获取优惠券使用限制。

所以你得到:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    foreach( $order->get_coupon_codes() as $coupon_code ) {
        // Get the WC_Coupon object
        $coupon = new WC_Coupon( $coupon_code );

        // Get usage count
        $count = $coupon->get_usage_count();
        
        // Get coupon usage limit per customer
        $limit = $coupon->get_usage_limit_per_user();
        
        // OR use this instead,to get coupon usage limit.
        // $limit = $coupon->get_usage_limit();
        
        // NOT empty
        if ( ! empty ( $count ) && ! empty ( $limit ) ) {
            // Calculate remaining
            $remaining = $limit - $count;
        
            // Output
            echo sprintf( '<span class="coupon-class">You used the <strong>%s</strong> coupon <strong>%d</strong> times and there are <strong>%d</strong> more left</span>',$coupon_code,$count,$remaining );
        }
    }
}
add_action( 'woocommerce_thankyou','action_woocommerce_thankyou',10,1 );
,
add_action('woocommerce_after_cart_table','coupon_count');
function coupon_count() {
    global $woocommerce;

    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
         $my_coupon = $woocommerce->cart->get_coupons() ;
         foreach($my_coupon as $coupon){

            if ( $post = get_post( $coupon->id ) ) {

                        $counter = $coupon->get_usage_count();
                        $remaining = $coupon->get_usage_limit()-$counter;
                        echo "<span class='name-coupon'><b>Total usage for coupon </b><b>'</b><b>".$coupon->code."</b><b>'</b><b>: </b></span>";
                        echo "<span class='coupon-counter'>".($counter)."</span>";
                        echo "<span class='coupon-counter'> there are ".($remaining)." more left</span>";

            }
        }
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...