【问题标题】:Woocommerce discount by user role用户角色的 Woocommerce 折扣
【发布时间】:2021-08-09 14:20:24
【问题描述】:

编辑:如您所见,我是新来的。很抱歉让你头疼 这是目前的问题: 代码现在可以工作了!但是当商店页面上的产品价格与购物车和结帐页面不同时,这可能与税收有关。

为了清楚起见,我已插入不含增值税的价格,并且在商店页面上未包含增值税。但在购物车页面上会计算增值税。

总结:

  • 产品价格为:10 欧元
  • 22% 的折扣是 7.80 欧元
  • 含 24% 增值税:~ 9.67 欧元
  • 但在购物车和结帐页面上显示总计:11.16 欧元

Shop page – Quick cart open

我目前正在使用 Wordpress + Woocommerce 处理一个非常复杂的项目,但我对 PHP 和 Wordpress/Woocommerce 钩子不太熟悉。我正在尝试对 X 类产品应用百分比折扣。我遇到了诸如

之类的问题

遇到一个非数字值

在仪表板上,价格与购物车/结帐页面上的产品存档页面不同。

这是我在functions.php上的代码:

add_filter('woocommerce_product_variation_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_product_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_get_price', 'custom_price', 10, 2);

function custom_price($price, $product)
{
  if (!is_user_logged_in()) return $price;

  if (has_role('hintaluokka-5')) {
    $price = $price * 0.95;
  } elseif (has_role('hintaluokka-12')) {
    $price = $price * 0.88;
  } elseif (has_role('hintaluokka-15')) {
    $price = $price * 0.85;
  } elseif (has_role('hintaluokka-22')) {
    if (has_term('kastikkeet', 'product_cat', $product->ID)) {
      $price = $price * 0.78;
    } else {
      $price = $price * 0.9;
    }
  }
  return $price;
}

function has_role($role = '', $user_id = null)
{
  if (is_numeric($user_id))
    $user = get_user_by('id', $user_id);
  else
    $user = wp_get_current_user();

  if (empty($user))
    return false;

  return in_array($role, (array) $user->roles);
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    试试这个代码:

    add_filter( 'woocommerce_product_variation_get_price', 'custom_price', 10, 2 );
    add_filter( 'woocommerce_product_get_price', 'custom_price', 10, 2 );
    function custom_price( $price, $product ) {
        if ( ! is_user_logged_in() ) {
            return $price;
        }
    
        if ( has_role( 'hintaluokka-5' ) ) {
            $price *= 0.95;
        } elseif ( has_role( 'hintaluokka-12' ) ) {
            $price *= 0.88;
        } elseif ( has_role( 'hintaluokka-15' ) ) {
            $price *= 0.85;
        } elseif ( has_role( 'hintaluokka-22' ) ) {
            if ( has_term( 'kastikkeet', 'product_cat', $product->ID ) ) {
                $price *= 0.78;
            } else {
                $price *= 0.9;
            }
        }
    
        return $price;
    }
    
    function has_role( $role = '', $user_id = null ) {
        if ( is_numeric( $user_id ) ) {
            $user = get_user_by( 'id', $user_id );
        } else {
            $user = wp_get_current_user();
        }
    
        if ( empty( $user ) ) {
            return false;
        }
    
        return in_array( $role, $user->roles, true );
    }
    

    我认为这里没有什么大问题。只有一个钩子不再使用(已弃用)。

    发布错误消息以防万一这没有帮助。

    【讨论】:

    • 谢谢,显然某件产品上发生了某种神奇的事情,破坏了这一切。现在已修复。但我仍然在商店页面和购物车/结帐页面上得到不同的价格。不知道是不是跟税有关系。但是我已将税款配置为不在购物车上的商店页面上显示,而是在订单摘要上显示包括税费在内的总额。
    【解决方案2】:

    看看这一行:

    $price = $price * 0.95;
    

    如果$price"1$" 会发生什么?在这种情况下,$price 无法转换为数字以执行引用公式的计算结果。因此,为了解决您的问题,您需要确定错误发生的确切行(从服务器错误日志中),找出导致错误的变量/参数是什么并将其转换为数值。

    【讨论】:

    • 价格来自钩子,输入为 int/float
    • 折扣码现在可以使用,但在购物车/结帐页面上无法正常使用。
    • @SamuliVälimäki 您需要确定确切的行。
    • @Mr.Jo 如果我们知道引发错误的确切行是什么,那么我将使用更多信息编辑此答案。在那之前,我需要解释问题本身,但不能确定我作为示例提供的行是有问题的行。如果没有,那么我们将需要提问者提供更多信息。
    • 正如我对原始问题的回答;没有更多的错误!只是商店页面+快速购物车和实际购物车和结帐页面上的价格不同。
    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 2019-07-20
    • 2021-01-06
    • 2018-12-23
    • 2020-10-18
    • 2018-02-26
    • 2014-02-12
    • 2016-10-07
    相关资源
    最近更新 更多