简体   繁体   English

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

[英]Add fee to Woocommerce Shipping Class without the use of zones or flat rate

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.我正在为我们简单的非营利漫画书店使用 Woocommerce 和一个名为 Table Rate Shipping Plus(由 mangohour 提供)的插件。 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.我创建了一个“庞大”的运输类别,我需要它在选择该类别的产品时自动向购物车添加少量费用(仅一次,无需堆叠),而无需使用 woocommerce 的运输区域和统一费率系统.

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.我似乎无法在任何地方找到任何可以执行这种简单操作的东西,而且我对 php 的了解不够,无法自己为 functions.php 编写函数或过滤器。

Can someone point me in the right direction?有人可以指出我正确的方向吗? I've stared myself blind...我一直盯着自己瞎...

If I gave you this code to put in functions.php, would you understand how to customize it?如果我给你这段代码放在functions.php中,你知道如何自定义吗? Or should I turn it into a simple plugin for you?或者我应该把它变成一个简单的插件给你?

Heres a visual of it working on my local WP testing environment.这是它在我的本地 WP 测试环境中工作的视觉效果。

伊格


Installing into functions.php安装到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.向下滚动到实际代码开始的位置,然后将整个块复制并粘贴到主题文件夹 functions.php 的最底部。 Just a simple copy and paste.只是一个简单的复制和粘贴。


Instructions for using.使用说明。

Step 1. Create your shipping classes within WooComerce Settings > Shipping > Shipping Classes.步骤 1. 在 WooComerce 设置 > 运输 > 运输类别中创建您的运输类别。 For example on my test site I created 'bulky' and 'light' shipping classes within wooComerce settings.例如,在我的测试站点上,我在 wooComerce 设置中创建了“bulky”和“light”运输类。 Remember the slug you set for step 2.记住您为第 2 步设置的 slug。

Step 2. At EX1 I place the case sensitive slug of a WooCommerce shipping class wrapped in ''.第 2 步。在EX1我将 WooCommerce 运输类的区分大小写的 slug 放在 ''. At EX2 I place the description for the fee you would like displayed at checkout within ''.EX2我将您希望在结账时显示的费用描述放在“”中。 Finnaly at EX3 you simply place the numeric value for the fee, this does not go within ''.最后,在EX3您只需输入费用的数值,这不在 '' 内。

//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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 中为“统一费率”运输方式添加工具提示 - Add tooltip to “flat rate” shipping method in WooCommerce 为 Woocommerce 中每 3 件商品的统一运费添加额外费用 - Add an additional cost to flat rate shipping each 3 items in Woocommerce 在woocommerce中将“统一费率”运输方式设置为默认值 - Set “flat rate” shipping method as default in woocommerce 当 Woocommerce 提供免费送货服务时,隐藏运费统一费率” - Hide shipping Flat rate" when free shipping is available in Woocommerce 在 Woocommerce 中为运输区添加州而不是邮政编码 - Add states for shipping zones instead of postcodes in Woocommerce 隐藏WooCommerce中特定运输类别的所有统一费率运输方法 - Hide all Flat rate Shipping methods for specific shipping classes in WooCommerce 根据WooCommerce统一运费方式的自定义字段计算添加交货时间 - Add a delivery time based on custom fields calculations for WooCommerce Flat rate Shipping method 为 WooCommerce 中特定类别的每 2 件商品的固定运费增加额外费用 - Add extra cost to flat rate shipping each 2 items from specific category in WooCommerce 删除WooCommerce 2.6和3+中特定类别的运输统一费率方法 - Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+ 使用Jquery ajax更新WooCommerce“统一费率”运费 - Update WooCommerce “flat rate” shipping cost using Jquery ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM