do_shortcode not considering price filters with woocommerce native products shortcode

34 Views Asked by At

I have some filters like this inside my plugin

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_sale_price', 'custom_price', 99, 2 );
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_sale_price', 'custom_variable_price', 99, 3 );
... and so on
function custom_variable_price( $price, $variation, $product ) {
    
    if((!is_admin() && !wp_doing_ajax()) && (is_shop() || is_product_category() || is_product_tag() || is_product() || is_front_page())) {

        return get_discounted_price($product, $price);

    }

    return $price;

}

This works and changes my prices dynamically everywhere, even with woocommerce native products shortcode (through page editor) EXCEPT when calling it inside do_shortcode in functions. And cannot figure out why.

Here is the example where these filters do not work

add_action('woocommerce_after_cart', 'offer_additional_products_cart');
function offer_additional_products_cart() {
    
    if(is_cart()) {
        
        echo '
        <div class="company_home_title">'.__('Do not miss', 'company_translations').'</div>
        ';
        
        echo do_shortcode('[products limit="4" orderby="popularity" class="quick-sale" on_sale="true"]');
        
    }
    
} 
1

There are 1 best solutions below

0
LoicTheAztec On BEST ANSWER

This is completely normal because you are using do_shortcode() in cart page and your custom price is not allowed in cart page (see your restrictions in your IF statement).

To allow your custom product price in cart page, you should add is_cart() in your IF statement, then do_shortcode() will work as expected, displaying the right custom product prices.

But you should better use only ! is_admin() in the IF statement inside your functions like:

function custom_price( $price, $product ) {
    if ( ! is_admin() ) {
        $price = get_discounted_price($product, $price);
    }
    return $price;
}

Because with your current code, the prices don't get changed in cart and checkout pages...