John Davidson

php - Display two prices if user is logged in. Change varible price as from. Display the custom price variation for a variable product

0 comments
Message:


I am using "Enable sale price for logged users and regular price for unlogged users in Woocommerce" first function code, that hides sales price. That way I can use sales price and regular price for logged in vs none logged in users and it Works great.


The problem I am running into is that it always hides the sale price, but if user is logged in is should be display two prices (sale price and regular price):


For solution I am using the following


// Display the sales price for a simple product only if_user_is_logged_in /


add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );

function custom_get_price_html( $price, $product ) {

if( $product->is_type('simple') )
{
if( is_user_logged_in() )
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_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) . $product->get_price_suffix();
}
// Return $price
return $price;
}

//Change the display of prices for variable products to "from the minimum price" and "from the minimum sale price" for if_user_is_logged_in /


add_filter( 'woocommerce_variable_price_html', 'custom1_variation_price', 20, 2 );

function custom1_variation_price( $price, $product ) {

$min_regular_price = $product->get_variation_regular_price( 'min', true );
$min_sale_price = $product->get_variation_sale_price( 'min', true );
$max_regular_price = $product->get_variation_regular_price( 'max', true );
$max_sale_price = $product->get_variation_sale_price( 'max', true );


if ( ! ( $min_regular_price == $max_regular_price && $min_sale_price == $max_sale_price ) ) {

if( is_user_logged_in() ) {
if ( $min_sale_price < $min_regular_price ) {
$price = sprintf( 'от <del>%1$s</del><ins>%2$s</ins>', wc_price( $min_regular_price ), wc_price( $min_sale_price ) );
} else {
$price = sprintf( 'от %1$s', wc_price( $min_regular_price ) );
}
}
else {
if ( $min_sale_price < $min_regular_price ) {
$price = sprintf( 'от %1$s', wc_price( $min_regular_price ), wc_price( $min_sale_price ) );
} else {
$price = sprintf( 'от %1$s', wc_price( $min_regular_price ) );
}
}

}

return $price;

}

// Display the custom price variation for a variable product as ['regular price'] if a guest. And display custom price variation ['sale price' - 'regular price (cross out)'] if user_is_logged_in /


add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3);
function custom_variation_price( $price, $product, $variation ) {

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

if( is_user_logged_in() )
$reg_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
else
$price['price_html'] = wc_price( $reg_price );


// Return $price
return $price;
}


  1. The code works, but I'm not sure if it is correct.

  2. My regular custom variation price for guests in variable products is not displayed correctly.



My Answer:
// Check if user is logged in
if ( is_user_logged_in() ) {
// Get the product price
$price = get_post_meta( get_the_ID(), '_price', true );

// Display the regular price
echo 'Regular Price: $' . $price . '
';

// Get the custom price variation
$custom_price = get_post_meta( get_the_ID(), '_custom_price', true );

// Display the custom price variation
echo 'Custom Price: $' . $custom_price;
} else {
// If user is not logged in, display only the regular price
$price = get_post_meta( get_the_ID(), '_price', true );
echo 'Price: $' . $price;
}
?>

Rate this post

5 of 5 based on 2771 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us