php – 以编程方式为Woocommerce 3添加有条件的折扣

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 以编程方式为Woocommerce 3添加有条件的折扣脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种在结账时以编程方式创建优惠券的方法,并在结账时将其删除.这需要在奖金制度的基础上进行,我会检查是否允许客户获得奖金.重要的是,我不想把它作为普通优惠券,因为客户不应该通过自己知道代码来附加它.

我只找到了附加优惠券或以编程方式创建优惠券的解决方案.我在结账时没有发现临时优惠券.

同样重要的是,这张优惠券可以与其他优惠券相结合,而不是更多.

这是我的代码

if ( get_discount_points() < 100 ) {
    //Customer has bonus status 1
} elseif ( get_discount_points() < 200 ) {
    //Customer has bonus status 2
} else {
    //Customer has bonus status x

按折扣百分比计算
    }

这甚至可能吗?

解决方法

为了简单起见,你可以使用负费用(每个步骤点@L_777_6@折扣百分比),如:

function get_customer_discount(){
    if( $points = get_discount_points() ){
        if ( $points < 100 ) {
            return 1; // 1 % discount
        } elseif ( $points < 200 ) {
            return 2; // 2.5 % discount
        } else {
            return 4; // 5 % discount
        }
    } else {
        return false;
    }
}


add_action( 'woocommerce_cart_calculate_fees','custom_discount',10,1 );
function custom_discount( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for 2 ITems or more
    if( $PErcentage = get_customer_discount() ){
        $discount = WC()->cart->get_subtotal() * $percentage / 100;

        // Apply discount to 2nd item for non on sale items in cart
        if( $discount > 0 )
            $cart->add_fee( sPRintf( __("Discount %s%%"),$percentage),-$discount );
    }
}

代码位于活动主题(或活动主题)的function.PHP文件中.经过测试和工作.

@H_403_41@

脚本宝典总结

以上是脚本宝典为你收集整理的php – 以编程方式为Woocommerce 3添加有条件的折扣全部内容,希望文章能够帮你解决php – 以编程方式为Woocommerce 3添加有条件的折扣所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。