折扣产品价格,为 WooCommerce 中的特定用户角色自动添加免费产品

问题描述 投票:0回答:1

我有一个 WordPress 网站,其中我有“高级”用户角色。我希望所有具有此角色的用户在购买墙上图片和免费蜡烛时都能获得 10% 的折扣。我希望折扣价同时显示在商店页面和购物车/结账页面中。

我远非 PHP 专家,但这是我的尝试。
我的代码似乎无法正常工作。例如,免费产品不会添加到购物车。

有谁可以帮我解决这个问题吗?

非常感谢您的帮助:)

function apply_discounts_based_on_user_role_and_product_type( $cart ) {
    if ( is_user_logged_in() ) {
        $user_roles = wp_get_current_user()->roles;

        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item['data']->get_name() === 'Wall Picture' ) {
                if ( in_array( 'premium', $user_roles ) ) {
                    $discount = $cart_item['data']->get_price() * 0.1;
                    $cart->add_fee( 'Premium Discount', -$discount, true );
                }
            } elseif ( $cart_item['data']->get_name() === 'Candle' ) {
                if ( in_array( 'premium', $user_roles ) ) {
                    $cart->remove_cart_item( $cart_item_key );
                }
            }
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'apply_discounts_based_on_user_role_and_product_type' );

function update_product_price_based_on_user_role( $price, $product ) {
    if ( is_user_logged_in() ) {
        $user_roles = wp_get_current_user()->roles;

        if ( $product->get_name() === 'Wall Picture' ) {
            if ( in_array( 'premium', $user_roles ) ) {
                $discounted_price = $product->get_price() * 0.9;
                return wc_price( $discounted_price );
            }
        } elseif ( $product->get_name() === 'Candle' ) {
            if ( in_array( 'premium', $user_roles ) ) {
                return __('Free', 'text-domain');
            }
        }
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'update_product_price_based_on_user_role', 10, 2 );
php wordpress woocommerce product discount
1个回答
0
投票

请注意,所有费用仅显示在购物车和结帐页面中,但从技术上讲,不可能在产品(商店、产品档案、单一产品或迷你购物车)中显示它们。

因此,最好更改购物车中的产品价格(以打折价格),并在其他地方(商店、单品、购物车、迷你购物车和结帐)将其显示为打折。对于自动添加的免费产品,我们显示“免费”并将价格设置为零。

在第一个函数中,您需要通过 ID(而不是名称)定义要打折的产品和免费产品。

代码:

// HERE set the related product IDs (settings)
function discounted_products_settings() {
    return array(
        'free_product_id'     => 26, // HERE set the free product ID
        'targeted_product_id' => 18, // HERE set the targeted product ID to discount
    );
}

// Display discounted product price in shop and single product.
add_filter( 'woocommerce_get_price_html', 'user_role_product_price_html', 20, 2 );
function user_role_product_price_html( $price_html, $product ) {
    global $current_user;

    // Targeting user role
    if ( ! in_array( 'administrator', $current_user->roles ) ) return $price_html;
    //if ( ! in_array( 'premium', $current_user->roles ) ) return $price_html;

    extract(discounted_products_settings()); // Load the product IDs (settings)

    if( $product->get_id() == $targeted_product_id ) {
        $regular_price    = wc_get_price_to_display( $product, array('price' => $product->get_regular_price()) );
        $discounted_price = wc_get_price_to_display( $product, array('price' => $product->get_regular_price() * 0.9) );

        return wc_format_sale_price( $regular_price, $discounted_price ) . $product->get_price_suffix();
    }
    return $price_html;
}

// Cart and mini cart displayed price html
add_filter( 'woocommerce_cart_item_price', 'display_cart_item_price_html', 20, 2 );
function display_cart_item_price_html( $price_html, $cart_item ) {
    global $current_user;

    // Targeting user role
    if ( ! in_array( 'administrator', $current_user->roles ) ) return $price_html;
    //if ( ! in_array( 'premium', $current_user->roles ) ) return $price_html;

    extract(discounted_products_settings()); // Load the product IDs (settings)

    $product = wc_get_product($cart_item['product_id']); // Get the product Object from the ID

    if( $targeted_product_id == $cart_item['product_id'] ) {
        $args  = array( 'price' => $product->get_regular_price() ); 
        $args2 = array( 'price' => $product->get_regular_price() * 0.9 ); // Set discounted price calculation

        if ( WC()->cart->display_prices_including_tax() ) {
            $regular_price    = wc_get_price_including_tax( $product, $args );
            $discounted_price = wc_get_price_including_tax( $product, $args2 );
        } else {
            $regular_price    = wc_get_price_excluding_tax( $product, $args );
            $discounted_price = wc_get_price_excluding_tax( $product, $args2 );
        }
        return wc_format_sale_price( $regular_price, $discounted_price );
    }
    elseif ( $free_product_id == $cart_item['product_id'] ) {
        return esc_html__('Free', 'text-domain');
    }
    return $price_html;
}

// Cart and Checkout displayed product subtotal html
add_filter( 'woocommerce_cart_item_subtotal', 'display_cart_item_subtotal_html', 10, 2 ); 
function display_cart_item_subtotal_html( $item_subtotal_html, $cart_item ) { 
    global $current_user;

    // Targeting user role
    if ( ! in_array( 'administrator', $current_user->roles ) ) return $price_html;
    //if ( ! in_array( 'premium', $current_user->roles ) ) return $price_html;

    extract(discounted_products_settings()); // Load the product IDs (settings)

    if( $targeted_product_id == $cart_item['product_id'] ) {
        $product = wc_get_product($cart_item['product_id']); // Get the product Object from the ID
        $args    = array( 'price' => $product->get_regular_price() );

        if ( WC()->cart->display_prices_including_tax() ) {
            $regular_price       = wc_get_price_including_tax( $product, $args );
            $regular_subtotal    = $regular_price * $cart_item['quantity'];
            $discounted_subtotal = $cart_item['line_subtotal'] * $cart_item['line_subtotal_tax'];
        } else {
            $regular_price       = wc_get_price_excluding_tax( $product, $args );
            $regular_subtotal    = $regular_price * $cart_item['quantity'];
            $discounted_subtotal = $cart_item['line_subtotal'];
        }
        return wc_format_sale_price( $regular_subtotal, $discounted_subtotal );
    }
    return $item_subtotal_html;
}

// Set cart item prices, and auto add the free product
add_action( 'woocommerce_before_calculate_totals', 'user_role_add_free_product' );
function user_role_add_free_product( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $current_user;

    // Targeting user role
    if ( ! in_array( 'administrator', $current_user->roles ) ) return;
    //if ( ! in_array( 'premium', $current_user->roles ) ) return;
        
    extract(discounted_products_settings()); // Load the product IDs (settings)

    $discount_enabled = $free_product_key = false; // Initializing variables
    
    foreach ( $cart->get_cart() as $item_key => $item ) {
        $product = wc_get_product($item['product_id']); // Get the product Object from the ID

        if ( $targeted_product_id == $item['product_id'] ) {
            $price = $product->get_regular_price() * 0.9; // calculate the price
            $item['data']->set_price($price); // Set the discounted price
            $discount_enabled = true;
        } 
        elseif ( $item['product_id'] == $free_product_id ) {
            $item['data']->set_price(0); // Set the price to zero
            // Adjust free product quantity if needed
            if ( $item['quantity'] > 1 ) {
                $cart->set_quantity( $item_key, 1 ); 
            }
            $free_product_key = $item_key;
        }
    }

    if ( $discount_enabled && ! $free_product_key ) {
        // add free product when discounted product is in cart
        $cart->add_to_cart( $free_product_id, 1 ); 
    }
    elseif ( ! $discount_enabled && $free_product_key ) {
        // remove free product when discounted product is not anymore in cart
        $cart->remove_cart_item( $free_product_key ); 
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.