[英]Displaying Taxes in Woocommerce by User Role
我的任务是修改现有的 B2C WooCommerce 商店以启用 B2B 批发订单。 到目前为止,用户角色编辑器和用户角色的 WooCommerce 价格的组合使我能够设置批发价格并使商店正常运行。
我面临的问题是,现在我被告知批发价格需要在结账前显示不含增值税,但由于我们位于英国,因此无法以这种方式向消费者显示价格,这意味着我需要比 WooCommerce 本身允许的更精细的控制。
我注意到已经有一个关于在 woocommerce 中基于角色的税收为用户角色设置税收的已解决问题,但我真正需要的是一段代码或一个插件,可以让我设置“商店中的显示价格”变量WooCommerce 的标准税收面板为一个用户角色排除税收,但默认为其他所有人包含税收。
那可能吗?
将以下内容放在我的functions.php 中对我有用。 如果角色是“零售商”,我会覆盖 WooCommerce 选项以在商店和购物车中显示税费。
add_filter('pre_option_woocommerce_tax_display_shop', 'override_tax_display_setting');
add_filter('pre_option_woocommerce_tax_display_cart', 'override_tax_display_setting');
function override_tax_display_setting() {
if ( current_user_can('retailer') ) {
return "excl";
} else {
return "incl";
}
}
@hagbard_2605 的解决方案对我有用,以下自定义插件适用于 WordPress 4.6
和 WooCommerce 2.2.3
:
<?php
/*
Plugin Name: My WooCommerce Prices Excluding Tax for Distributors
Plugin URI: https://www.pronamic.eu/
Description: Display WooCommerce prices exlcuding tax for distributors.
Author: Pronamic
Version: 1.0.0
Author URI: https://www.pronamic.eu/
*/
/**
* Override WooCommerce tax display option for distributors.
*
* @see http://stackoverflow.com/questions/29649963/displaying-taxes-in-woocommerce-by-user-role
* @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/admin/settings/class-wc-settings-tax.php#L147-L158
* @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/admin/settings/class-wc-settings-tax.php#L166-L178
* @see https://github.com/WordPress/WordPress/blob/4.6.1/wp-includes/option.php#L37-L52
*/
function my_override_woocommerce_tax_display( $value ) {
if ( current_user_can( 'retailer' ) ) {
return 'excl';
}
return $value;
}
add_filter( 'pre_option_woocommerce_tax_display_shop', 'my_override_woocommerce_tax_display' );
add_filter( 'pre_option_woocommerce_tax_display_cart', 'my_override_woocommerce_tax_display' );
WordPress pre_option_
过滤器用于为具有retailer
角色/能力的用户覆盖 WooCommerce woocommerce_tax_display_shop
和woocommerce_tax_display_cart
选项。
我没有足够的分数来评论这里的答案,但@hagbard_2605 的答案不起作用。 我什至找不到他提到的过滤器。 但是,您可以找到“woocommerce_tax_setting”过滤器: https : //github.com/woothemes/woocommerce/blob/5ef335b169ff4e19a4c5b393963a369446922b0c/includes/admin/settings/views/settings-tax.php#L7 。 也许那会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.