在 Woocommerce 中为登录用户启用销售价格,为未登录用户启用常规价格

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

在 Woocommerce 中,我有一个具有多种变体的可变产品,每个变体都有自己的常规价格和促销价格。

我希望用户:

  • 已登录,促销价为活动价
  • 未登录,正常价格为活跃价格。

我已在我的子主题 function.php 文件中添加了这个挂钩函数:

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
    wc_delete_product_transients($variation->get_id());
     $variation_id = $variation->get_id();
     $variable_product1= new WC_Product_Variation($variation_id);
     if(is_user_logged_in()){
        $regular_price = $variable_product1 ->regular_price;
             $data['price_html'] = woocommerce_price($regular_price);
             return $data;
             }  else { 
              $sale_price = $variable_product1 ->sale_price;
         $data['price_html'] = woocommerce_price($sale_price);
         return $data;
        
        }
    
}

显示的价格有效......但是当我将任何产品添加到购物车时,它仅保留在购物车中的销售价格......

php wordpress woocommerce product-variations product-price
2个回答
2
投票

下面的代码将:

  • 仅在所有地方为非登录用户启用正常价格
  • 仅对所有地方的登录用户启用促销价
  • 隐藏促销闪光徽章
  • 删除销售价格范围

代码:

// Variable and simple product displayed prices (removing sale price range)
add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );
function custom_get_price_html( $price, $product ) {

    if( $product->is_type('variable') )
    {
        if( is_user_logged_in() ){
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('max') ) );
        } else {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
        }

        if( $price_min != $price_max ){
            if( $price_min == 0 && $price_max > 0 )
                $price = wc_price( $price_max );
            elseif( $price_min > 0 && $price_max == 0 )
                $price = wc_price( $price_min );
            else
                $price = wc_format_price_range( $price_min, $price_max );
        } else {
            if( $price_min > 0 )
                $price = wc_price( $price_min);
        }
    }
    elseif( $product->is_type('simple') )
    {
        if( is_user_logged_in() )
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        else
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );

        if( $active_price > 0 )
            $price = wc_price($active_price);
    }
    return $price;
}

// Product Variation displayed prices
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3);
function custom_variation_price( $data, $product, $variation ) {

    $reg_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
    $sale_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_sale_price() ) );

    if( is_user_logged_in() )
        $data['price_html'] = wc_price( $sale_price );
    else
        $data['price_html'] = wc_price( $reg_price );

    return $data;
}

// Set the correct prices in cart
add_action( 'woocommerce_before_calculate_totals', 'set_item_cart_prices', 20, 1 );
function set_item_cart_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! is_user_logged_in() ){
            $cart_item['data']->set_price( $cart_item['data']->get_regular_price() );
        }
    }
}

// Remove sale badge
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );

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

更新启用在 WooCommerce 中显示自定义产品价格的功能的后缀价格


0
投票

这对我有用!非常感谢您的代码! 此代码可以帮助我的客户非常轻松地为其“会员”添加价格。

他们只是完成常规价格和促销价格。 批发插件要复杂得多,在这种情况下不需要。 *我的意思是只有登录用户才能看到“销售价格”。

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