dpstir0081 2016-08-12 22:50
浏览 16
已采纳

WooCommerce - 制作一套优惠券,为订单添加固定费用

A client has a very odd request: they would like for a set of coupon codes to add a fee to an the order. Is this possible?

The problem they are trying to solve is this:

Currently the store offers free shipping to the lower 48 United States, for which I have shipping classes and table rates properly configured. However, the coupons the customer has sold on a daily deal site are supposed to remove the free shipping benefit and add a fixed shipping fee.

How can achieve this?

  • 写回答

1条回答 默认 最新

  • dthjnc306679 2016-08-12 23:40
    关注

    Yes you can do it with that snippet code (for a set of coupons):

    function coupon_add_cart_fee( $bookable_total = 0 ) {
        $has_coupon = false;
    
        // Set here your specials coupons slugs (one by line - last one have no coma)
        $coupon_codes = array (
            'your_coupon_code_slug1',
            'your_coupon_code_slug2',
            'your_coupon_code_slug3' 
        );
    
        // Set here your fee amount or make fees calculation (see the links in reference)
        $fee = 20;
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // checking if that "special" coupon is applied to customer cart
        foreach ($coupon_codes as $coupon_code) {
            if ( WC()->cart->has_discount( $coupon_code ) ) {
    
                // If yes apply the fee to the cart
                if ( !$has_coupon ) {
                    $has_coupon = true;
                    break;
                }
            }
        }
    
        if ( $has_coupon ) {
            WC()->cart->add_fee( 'Fees: ', $fee, false );
        }
    }
    add_action( 'woocommerce_cart_calculate_fees','coupon_add_cart_fee' );
    

    This code is tested and it works. It goes on function.php file of your active child theme or theme.


    TAX OPTIONS with add_fee() method

    IMPORTANT: The fact that TAX is working or not with add_fee() method depends first of your tax settings in woocommerce.

    Class WC_Cart add_fee() method, adds additional fee to the cart.

    add_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = ''  )> <!-- language: lang-css -->
    
    Parameters:
        $name      Unique name for the fee. Multiple fees of the same name cannot be added.
        $amount    Fee amount.
        $taxable   (default: false) Is the fee taxable?
        $tax_class    (default: '') The tax class for the fee if taxable. A blank string is standard tax class.
    

    Reference:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄