Search code examples
phpwordpresswoocommerceshortcode

Woocommerce product qty shortcode not working on mobile browsers


I've got an with a site, we've got this shortcode on it:

if( !function_exists('show_specific_product_quantity') ) {

function show_specific_product_quantity( $atts ) {

    // Shortcode Attributes
    $atts = shortcode_atts(
        array(
            'id' => '', // Product ID argument
        ),
        $atts,
        'product_qty'
    );

    if( empty($atts['id'])) return;

    $stock_quantity = 0;

    $product_obj = wc_get_product( intval( $atts['id'] ) );
    $stock_quantity = $product_obj->get_stock_quantity();

    if( $stock_quantity > 0 ) {
        return $stock_quantity;
    } else {
        return 0;
    }

}

add_shortcode( 'product_qty', 'show_specific_product_quantity' );}

Which shows the current stock level of the product ID and it works perfectly on desktop browsers but mobile browsers it only shows the max stock level all the time.

It's simply used like this:

[product_qty id='2329']

And every desktop browser, including the emulated mobile viewports in Chrome and Elementors responsive mode show it correctly. Even on Chrome mobile, if you set it to desktop mode, the correct stock level is shown. It's only on native mobile browsers that it doesn't seem to work.

Why might this be happening?


Solution

  • Turns out it was a weird caching problem that was being caused by Siteground's Optimisation plugin. I haven't figured out which if the three caching options is the cause yet, but I'll update this answer when I do.

    It was also preventing custom add to cart links from working, again only in mobile. So a weird problem and one I'll be reporting to Siteground.