【问题标题】:Woocommerce - change shipping cost via hooks (functions.php)Woocommerce - 通过钩子(functions.php)更改运费
【发布时间】:2018-02-09 05:32:50
【问题描述】:

我正在使用 Woocommerce 订阅和会员资格,我想根据我的用户拥有的会员计划对运费进行一些折扣。例如,银卡会员可享受所有运输方式的 -30% 折扣。 要检查我使用的当前会员计划:

$user_id = get_current_user_id();// Get current user ID

if ( wc_memberships_is_user_active_member( $user_id, 'silver' ) ) {
    //echo 'This is a special message only for Silver members!';
} else {
    //echo 'I\'m sorry, you are not a silver member. There\'s nothing here for you!';
}

但我没有找到任何方法可以通过功能改变运费..

有可能吗?谢谢

【问题讨论】:

    标签: wordpress woocommerce hook


    【解决方案1】:

    我在这里写了一些示例:http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/

    add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
    function woocommerce_package_rates( $rates ) {
        $user_id = get_current_user_id();
        if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) { return $rates; }
        $discount_amount = 30; // 30%
    
        foreach($rates as $key => $rate ) {
            $rates[$key]->cost = $rates[$key]->cost - ( $rates[$key]->cost * ( $discount_amount/100 ) );
        }
    
        return $rates;
    }
    

    以上代码将为所有运输方式执行您想要的操作。

    【讨论】:

    • 您好!感谢您的回答。我有点困惑,您的代码不起作用..我进入您的文章并尝试将所有运输提要设置为 10 的基本代码,它也不起作用
    • 哦,我的错,我必须清空我的购物车!谢谢你的把戏。但是我还有一个问题,如何改变税收呢?因为现在不正确
    • @Efbi,我编辑了我的答案...忘记添加$user_id = get_current_user_id();
    • 好的,我会再问一个问题,所以 woo 没有预料到税收问题真是太棒了。非常感谢这个答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2017-10-08
    相关资源
    最近更新 更多