WooCommerce产品数量快速代码不从事移动浏览器

发布于 2025-01-25 20:16:47 字数 957 浏览 2 评论 0原文

我有一个网站,我们已经有一个快速代码:

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' );}

它显示了当前产品ID的库存水平,并且在桌面浏览器上完美运行,但移动浏览器它只会一直显示最大库存级别。

它只是这样使用的:

[product_qty id='2329']

每个桌面浏览器,包括Chrome中的模拟移动视口和Elementors响应模式正确显示。即使在Chrome Mobile上,如果将其设置为桌面模式,也会显示正确的库存级别。只有在本机移动浏览器上,它似乎不起作用。

为什么会发生这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2025-02-01 20:16:47

事实证明,这是一个奇怪的缓存问题,是由Siteground的优化插件引起的。我还没有弄清楚三个缓存选项是原因,但是当我这样做时我会更新此答案。

它还可以防止自定义添加到购物车链接工作,仅在移动设备中才能使用。因此,一个奇怪的问题,我将向Siteground报告。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文