簡體   English   中英

Woocommerce 按用戶角色折扣

[英]Woocommerce discount by user role

編輯:如你所見,我是新來的。 很抱歉讓您頭疼這是目前的問題:代碼現在正在運行。 但是,當商店頁面上的產品價格與購物車和結帳頁面不同時,這可能與稅收有關。

為了清楚起見,我已插入不含增值稅的價格,並且在商店頁面上不含增值稅。 但在購物車頁面上計算增值稅。

概括:

  • 產品價格為:10 歐元
  • 折扣 22% 為 7.80 歐元
  • 含 24% 增值稅:~ 9.67 歐元
  • 但在購物車和結帳頁面上顯示總計:11.16 歐元

商店頁面 – 快速打開購物車

我目前正在使用 Wordpress + Woocommerce 處理一個非常復雜的項目,我對 PHP 和 Wordpress/Woocommerce 掛鈎不太熟悉。 我正在嘗試對 X 類產品應用百分比折扣。我遇到了類似的問題

遇到非數值

在儀表板上,價格與購物車/結帳頁面上的產品存檔頁面不同。

這是我的函數代碼。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);
}

試試這個代碼:

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 );
}

我認為這里沒有什么大問題。 只有一個鈎子不再使用(已棄用)。

如果這沒有幫助,請發布錯誤消息。

看看這一行:

$price = $price * 0.95;

如果$price"1$"會發生什么? 在這種情況下, $price無法轉換為數字以執行引用公式的計算結果。 因此,為了解決您的問題,您需要確定錯誤發生的確切行(從服務器錯誤日志中),找出導致錯誤的變量/參數並將其轉換為數值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM