WooCommerce产品数量快速代码不从事移动浏览器
我有一个网站,我们已经有一个快速代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这是一个奇怪的缓存问题,是由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.