Home / eCommerce / WWPP – Show correct wholesale prices in the Elementor side cart
Duplicate Snippet

Embed Snippet on Your Site

WWPP – Show correct wholesale prices in the Elementor side cart

Code Preview
php
<?php
/**
 * WWPP: Show correct wholesale prices in the Elementor side cart.
 */
// Recalculate cart totals before mini-cart renders to ensure fresh pricing data.
add_action( 'woocommerce_before_mini_cart', function () {
    if ( WC()->cart instanceof WC_Cart && ! WC()->cart->is_empty() ) {
        WC()->cart->calculate_totals();
    }
}, 1 );
// Filter the mini-cart item display price for wholesale users.
add_filter( 'woocommerce_cart_item_price', function ( $price_html, $cart_item, $cart_item_key ) {
    // Do not apply this filter on the main cart or checkout pages to avoid conflicts.
    if ( is_cart() || is_checkout() ) {
        return $price_html;
    }
    // Verify Wholesale Suite Premium is active and fetch the user's wholesale role.
    if ( ! class_exists( 'WWPP_Wholesale_Roles' ) ) {
        return $price_html;
    }
    
    $wwpp_wholesale_roles = WWPP_Wholesale_Roles::getInstance();
    $user_wholesale_role  = $wwpp_wholesale_roles->getUserWholesaleRole();
    if ( empty( $user_wholesale_role ) ) {
        return $price_html;
    }
    // Ensure the item actually has a wholesale price applied.
    if ( empty( $cart_item['wwp_data']['wholesale_priced'] ) || 'yes' !== $cart_item['wwp_data']['wholesale_priced'] ) {
        return $price_html;
    }
    // Temporarily unhook the default pricing filter to avoid double-filtering or conflicts.
    if ( class_exists( 'WWP_Wholesale_Prices' ) ) {
        $wwp_wholesale_prices = WWP_Wholesale_Prices::getInstance();
        remove_filter(
            'woocommerce_cart_item_price',
            array( $wwp_wholesale_prices, 'filter_product_price' ),
            100
        );
    }
    // Fetch the raw product object from the cart item data.
    $product = $cart_item['data'];
    // Format and return the correct display price (including/excluding taxes based on settings).
    return wc_price(
        wc_get_price_to_display(
            $product,
            array( 'price' => $product->get_price() )
        )
    );
}, 25, 3 );

Comments

Add a Comment