dpw5865 2019-02-22 19:25
浏览 169
已采纳

在不使用区域或统一费率的情况下向Woocommerce Shipping Class添加费用

I'm having a great deal of trouble finding what I need, so I turn to the gurus over here!

I'm using Woocommerce and a plugin called Table Rate Shipping Plus (by mangohour) for our simple non-profit comic bookstore. We use weight based shipping as a standard in Sweden, so the flat rate system is not an option for our shop.

I have created a 'bulky' shipping class and I would need it to, upon choosing products in that class, automatically add a small fee to the cart (only once, without stacking) without the use of woocommerce's shipping zones and the flat rate system.

I can't seem to find anything that does this simple act anywhere, and I'm not php-savvy enough to be able to write functions or filters for the functions.php myself.

Can someone point me in the right direction? I've stared myself blind...

  • 写回答

1条回答 默认 最新

  • dongshan1811 2019-02-22 21:11
    关注

    If I gave you this code to put in functions.php, would you understand how to customize it? Or should I turn it into a simple plugin for you?

    Heres a visual of it working on my local WP testing environment.

    Imgur


    Installing into functions.php

    Scroll down to where the actual code begins, and copy and paste that entire block, right onto the very bottom of your theme folders functions.php. Just a simple copy and paste.


    Instructions for using.

    Step 1. Create your shipping classes within WooComerce Settings > Shipping > Shipping Classes. For example on my test site I created 'bulky' and 'light' shipping classes within wooComerce settings. Remember the slug you set for step 2.

    Step 2. At EX1 I place the case sensitive slug of a WooCommerce shipping class wrapped in ''. At EX2 I place the description for the fee you would like displayed at checkout within ''. Finnaly at EX3 you simply place the numeric value for the fee, this does not go within ''.

    //Example:
    $shippingClasses['EX1'] = ['description' => 'EX2', 'fee' => EX3];
    
    //How it will look:
    $shippingClasses['bulky'] = ['description' => 'Bulky Fee', 'fee' => 7];
    $shippingClasses['light'] = ['description' => 'Light Fee', 'fee' => 4];
    

    And thats it! Thats all you have to do.


    Code

    function fees_fees_fees() {
    
        $shippingClasses['bulky'] = ['description' => 'Bulky Fee', 'fee' => 5];
        $shippingClasses['light'] = ['description' => 'Light Fee', 'fee' => 7];
    
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
    
            foreach($shippingClasses as $key => $val) {
            if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
                  WC()->cart->add_fee( __($val['description'], 'woocommerce'), $val['fee'] ); }
            }
        }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'fees_fees_fees' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了
  • ¥15 gazebo-rviz教程
  • ¥15 付费求做一个自助抢单插件
  • ¥15 bat批处理,关于数据复制问题
  • ¥50 同步两个不同结果的array中某些属性
  • ¥15 悬赏15远程操控解决问题
  • ¥15 CST复制的模型无法单独修改参数?
  • ¥15 前端页面想做个定时任务,但是使用requestAnimationFrame,setinterval和settimeout都不行
  • ¥15 根据以下文字信息,做EA模型图