在 Woocommerce 中设置本地取货运输方式的百分比折扣 [英] Set a percentage discount to Local pickup shipping method in Woocommerce

查看:33
本文介绍了在 Woocommerce 中设置本地取货运输方式的百分比折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 WooCommerce 插件的 WordPress 网站.如果买家选择本地取货作为送货方式,我愿意为他们提供购物车总额 5% 的折扣.

I have a WordPress site which uses WooCommerce plugin. I would like to offer buyer 5% reduction from cart total if they select local pickup as a shipping method.

我已经尝试了 - 5 * [qty] 但它似乎不起作用.

I already tried - 5 * [qty] and it doesn't seem to be working.

我也试过 -0.95 * [cost] 没有运气

I also tried -0.95 * [cost] with no luck

推荐答案

我使用的是 WooCommerce 3,通过在活动主题的 function.php 中编写一个函数来实现上述结果.

I am using WooCommerce 3 and achieved the above result by writing a function inside the function.php of active theme.

function prefix_add_discount_line( $cart ) {
  $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
  $chosen_shipping_no_ajax = $chosen_methods[0];
  if ( 0 === strpos( $chosen_shipping_no_ajax, 'local_pickup' ) ) {

    // Define the discount percentage
    $discount = $cart->subtotal * 0.05;
    // Add your discount note to cart
    $cart->add_fee( __( 'Collection discount applied', 'yourtext-domain' ) , -$discount );
  }
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line');

这篇关于在 Woocommerce 中设置本地取货运输方式的百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆