【问题标题】:Displaying Taxes in Woocommerce by User Role按用户角色在 Woocommerce 中显示税金
【发布时间】:2015-06-21 09:33:39
【问题描述】:

我的任务是修改现有的 B2C WooCommerce 商店以启用 B2B 批发订单。到目前为止,用户角色编辑器和 WooCommerce 价格(按用户角色)的组合使我能够设置批发价格并使商店正常运作。

我面临的问题是,现在我被告知需要在结帐前显示不含增值税的批发价格,但由于我们位于英国,因此无法以这种方式向消费者显示价格,这意味着我需要比 WooCommerce 本身允许的更精细的控制。

我注意到在Role based taxes in woocommerce 上已经有一个关于为用户角色设置税费的问题已解决,但我真正需要的是一段代码或一个插件,可以让我设置“在商店中显示价格”变量在 WooCommerce 的标准税务面板中为一个用户角色排除税,但默认为其他所有人包含税。

这可能吗?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    @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_shopwoocommerce_tax_display_cart 选项。

    【讨论】:

    • 刚刚在 WooCommerce 3.4.5 上测试过它仍然有效,非常感谢!
    【解决方案2】:

    我没有足够的分数来评论这里的答案,但是@hagbard_2605 的答案不起作用。我什至找不到他提到的过滤器。但是,您可以找到“woocommerce_tax_setting”过滤器:https://github.com/woothemes/woocommerce/blob/5ef335b169ff4e19a4c5b393963a369446922b0c/includes/admin/settings/views/settings-tax.php#L7。也许这会奏效。

    【讨论】:

      【解决方案3】:

      将以下内容放在我的 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";
          }
      }
      

      【讨论】:

      • 您好,此代码对我有用,但我有 2 个角色必须不含税。此代码当前一次仅适用于一个角色。我尝试将代码更改为: if ( current_user_can( ' role1 ', ' role2 ') ) { return "excl"; } 其他 { 返回“包括”; } 但它不起作用。
      猜你喜欢
      • 2017-08-12
      • 2013-08-24
      • 2020-11-17
      • 2013-07-25
      • 2018-03-22
      • 2017-02-11
      • 2021-05-28
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多