在网上store 中,我想针对特定群体的顾客推出%的折扣.

我已经创建了一个新群组和客户,并添加了下面的代码,这将为购物车提供折扣.

/* discount biblioteka */
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit
    
    // only for 'biblioteka' role
    if ( ! current_user_can('biblioteka') )
        return; // Exit

    // Kortings percentage
    $percentage = 40;
    
    $discount = $cart->get_subtotal() * $percentage / 100; // Calculation
    
    // Applying discount
    $cart->add_fee( sprintf( __("biblioteka discount (%s)", "woocommerce"), $percentage . '%'), - $discount, true );
}

我希望这个折扣也作为促销价格显示在store 中,我在下面添加了代码,这使得网站停止工作.


/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)

function custom_price_assign( $price, $product ) {
    // Check if the user has a role of wholesaler
    if ( current_user_can('biblioteka') ){
        return $price * 0.40;
    }
    return $price;
}

对于特殊用户群体,我最简单的方式是什么?将正价和40%的价格显示为促销价?

推荐答案

try 以下修改后的代码,该代码将显示store 和单个产品中的折扣产品价格:

// Conditional function: Targeting specific user role
function is_biblioteka_role() {
    global $current_user;
    return in_array( 'biblioteka', $current_user->roles );
}

// Display discounted product price in shop and single product.
add_filter( 'woocommerce_get_price_html', 'user_role_product_discounted_price_html', 20, 2 );
function user_role_product_discounted_price_html( $price_html, $product ) {
    // Targeting specific user role
    if ( ! is_biblioteka_role() ) return $price_html;

    $percentage = 40; // Kortings percentage
    $price = $product->get_price();

    // Only for prices greater than zero
    if ( ! ($price > 0) ) return $price_html;

    $regular_price    = wc_get_price_to_display( $product );
    $discounted_price = wc_get_price_to_display( $product, array( 'price' => ($price * (100 - $percentage) / 100) ) );
    return wc_format_sale_price( $regular_price, $discounted_price ) . $product->get_price_suffix();
}

// Cart subtotal discount
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Exit
    if ( ! is_biblioteka_role() ) return; // Targeting specific user role

    $percentage = 40; // Kortings percentage
    $cart->add_fee(
        sprintf( __("Biblioteka discount (%s)", "woocommerce"), $percentage.'%'), 
        -($cart->get_subtotal() * $percentage / 100), true 
    );
}

代码保存在您的子主题的functions.php文件中(或插件中).经过测试并工作.

Php相关问答推荐

SQLSTATE [42S02]:找不到基表或视图:1146 Table eshop. sessions不存在'''

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

Symfony/Panther Web抓取不适用于登录后的内容(云功能)

我需要多个SELECT语句的结果(使用由php ECHO创建的表中显示的Limit(X)

PHP如何将ARRAY_MAP与Reset()配合使用

如何使用php一次更新两个数据库中的MySQL表

当配置的URL为空时禁用Laravel Slack日志(log)记录

在PHP中处理Linux输入事件(/dev/input/Event*)

未收到Strava WebHook事件数据

如何配置我的.env文件以在laravel的一个项目中连接两个不同的数据库?

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

用户注册表单中的 HTTP 错误 405

用自定义代码替换 WooCommerce 单一产品简短描述

使用产品自定义字段作为 WooCommerce 中特定用户角色的折扣百分比

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

判断类别是否存在 Shopware 6

PHP 自动函数参数和变量

使用 splat 运算符时按引用传递 (...)

如何使用先前 SELECT 的结果来 SELECT SQL 数据