Home / eCommerce / Show Wholesale Prices Text In Single Product Page For Wholesale Role Customers
Duplicate Snippet

Embed Snippet on Your Site

Show Wholesale Prices Text In Single Product Page For Wholesale Role Customers

Code Preview
php
<?php
/**
 * First Remove The Existing Pricing Filter So That It Will Be Available To All Users
 */
add_action('init', 'remove_existing_pricing_html', 10);
function remove_existing_pricing_html() {
    global $wc_wholesale_prices;
    remove_filter('woocommerce_get_price_html', [$wc_wholesale_prices->wwp_for_non_wholesale_customer,'add_click_wholesale_price_for_non_wholesale_customers'], 10 );
}
/**
 * Include Pricing HTML for all users
 */
add_action('init', 'include_the_pricing_html_logic_for_all_users', 11);
/**
 * Make sure to load all scripts for all users, which is why empty user role is passed
 */
add_action('wp_enqueue_scripts',  'return_false_for_wholesale', 10);
function return_false_for_wholesale() {
    global $wc_wholesale_prices;
    if( method_exists($wc_wholesale_prices->wwp_script_loader, 'load_front_end_styles_and_scripts') ) {
        add_filter('wwp_user_wholesale_role', function($roles, $current_user) {
            return [];
        }, 9999999999999999, 2);
    }
}
function include_the_pricing_html_logic_for_all_users() {
    add_filter('woocommerce_get_price_html', 'show_new_html_price_tag', 15, 2 );
}
function show_new_html_price_tag( $price, $product ) {
    global $wc_wholesale_prices;
    $product_id            = $product->get_id();
    $user_wholesale_role   = $wc_wholesale_prices->wwp_wholesale_roles->getUserWholesaleRole();
    $show_wholesale_prices = get_option( 'wwp_prices_settings_show_wholesale_prices_to_non_wholesale' );
    if ( $product_id && 'yes' === $show_wholesale_prices && ( ! is_user_logged_in() || current_user_can( 'manage_woocommerce' ) || is_shop() || is_product() ) ) {
        if ( in_array( WWP_Helper_Functions::wwp_get_product_type( $product ), array( 'simple', 'variable' ), true ) ) {
            $price .= $wc_wholesale_prices->wwp_for_non_wholesale_customer->display_replacement_message_to_non_wholesale( $product );
        }
    }
    return $price;
}

Comments

Add a Comment