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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化