1
votes

I'll try to be more comprehensive as possibile because this is quite complex, at least to explain lol

As you can see at https://www.cascinacanova.it/en/acquista-online/ here I have my standard Woocommerce store. Prices are visibile and, upon registration, those prices changes dynamically: old prices are strikethrough and the new discount price shows up. This is possibile due to "Wholesale Prices for WooCommerce by Wholesale Suite" and this code, which lets new users to register with "wholesale customers" privileges:

add_role( 'wholesale_customer', __( 'Wholesale Customer' ), array(
  'read' => true, 
));
  
add_filter( 'woocommerce_new_customer_data', 'bbloomer_assign_custom_role' );
  
function bbloomer_assign_custom_role( $args ) {
  $args['role'] = 'wholesale_customer';
  return $args;
}

Here's where the tricky part starts: in https://www.cascinacanova.it/en/i-nostri-vini/ I have some "shop now" buttons that must show the price, and that's the easy part. Following this Stack Overflow post I've done the following:

This shortcode on the button:

[product_price id="37"]

This code in functions.php

function custom_price_shortcode_callback( $atts ) {

    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'product_price' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
        // Get an instance of the WC_Product object
        $product = wc_get_product( intval( $atts['id'] ) );

        // Get the product prices
        $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

        // Your price CSS styles
        $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
        $style2 = 'style="font-size:25px;color:#e79a99"';

        // Formatting price settings (for the wc_price() function)
        $args = array(
            'ex_tax_label'       => false,
            'currency'           => 'EUR',
            'decimal_separator'  => '.',
            'thousand_separator' => ' ',
            'decimals'           => 2,
            'price_format'       => '%2$s %1$s',
        );

        // Formatting html output
        if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
            $html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
        else
            $html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
    }
    return $html;
 }
 add_shortcode( 'product_price', 'custom_price_shortcode_callback' );

Here's the problem because this code goes and takes the regular discount price, as is set by Woocommerce, and not what's we called "wholesale price".

The major problem is that the price still needs to be dynamic, even here, outside the store. So, I'll need a way to edit this code in order to show the product price and the wholesale price upon registration.

Let me know if you need extra info and thanks in advance for your help!

1

1 Answers

1
votes

I used wholesale_customer_wholesale_price as a custom price meta.
If it calls otherwise change it in the function.

If the user has the wholesale_customer role he will get the wholesaler price from the wholesale_customer_wholesale_price meta:

  • If the value exists and the wholesaler price is less than the regular price, the wholesaler price will be shown.

  • If the value is empty or the wholesaler price is greater than or equal to the regular price it will show the default price.

For all other user roles the default prices will be displayed.

add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
function custom_price_shortcode_callback( $atts ) {

    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'product_price' );

    $html = '';

    if ( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        // Get an instance of the WC_Product object
        $product = wc_get_product( intval( $atts['id'] ) );

        // Check user role
        if ( current_user_can( 'wholesale_customer' ) ) {
            $wholesale_price = str_replace( ',', '.', $product->get_meta( 'wholesale_customer_wholesale_price', true ) );
            if ( ! empty( $wholesale_price ) && (float) $wholesale_price < (float) $product->get_regular_price() ) {
                $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
                $sale_price    = wc_get_price_to_display( $product, array( 'price' => $wholesale_price ) ); // Get the sale price
                $price         = wc_get_price_to_display( $product, array( 'price' => $wholesale_price ) ); // Get the active price
            } else {
                $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
                $sale_price    = ''; // Set the sale price
                $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Set the active price
            }
        } else {
            // Get the product prices
            $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
            $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
            $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
        }
        
        // Your price CSS styles
        $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
        $style2 = 'style="font-size:25px;color:#e79a99"';

        // Formatting price settings (for the wc_price() function)
        $args = array(
            'ex_tax_label'       => false,
            'currency'           => 'EUR',
            'decimal_separator'  => '.',
            'thousand_separator' => ' ',
            'decimals'           => 2,
            'price_format'       => '%2$s&nbsp;%1$s',
        );

        // Formatting html output
        if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
            $html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
        else
            $html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
    }
    return $html;
}

The code has been tested and works.