让WooCommerce加载页面上的所有变体图像加载负载

发布于 2025-01-17 13:50:54 字数 351 浏览 0 评论 0原文

我正在使用 Woocommerce v6.3.1WooCommerce v1.1.19 的变体样本 在我的 Wordpress 网站上显示所有不同的变体。

我注意到它以舒适的速度加载产品页面,但不同属性的图像会进行 ajax 调用并在访问点加载该变体的图像(即单击样本按钮),这有相当大的滞后。然后该图像被缓存,因此该变体的后续访问没有任何加载时间,但我希望在页面加载时加载该初始调用。

我知道我可以对图像进行优化,而且我也这样做了。目前图像的平均大小为 500kb。

有没有办法让所有变体的所有图像在页面加载时加载?如果这些 ajax 调用仍然是异步进行的,那就太好了!

I am using Woocommerce v6.3.1 and Variation Swatches for WooCommerce v1.1.19 to display all the different variations on my Wordpress website.

I notice it loads a product page at a comfortable speed but the images for a different attribute makes an ajax call and loads the image for that variation at the point of access(ie the swatch button is clicked), which has quite a considerable lag. The image is then cached so the subsequent access of that variation does not have any load time but it's that initial call that I would prefer be loaded on page load.

I am aware that I can make optimizations on the images and I have done so as well. The average size of an image is 500kb at the moment.

Is there any way to make all the images of all variations load on page load? It would be great if those ajax calls are still made asynchronously too!

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

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

发布评论

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

评论(1

謌踐踏愛綪 2025-01-24 13:50:54

您可以将所有变体图像放入“ IMG”元素中。并通过设置“显示:无”使“ IMG”元素消失。这样,浏览器将在遇到元素后立即开始下载inages,并在下次需要显示时被缓存。您可以在产品循环中执行此操作:

$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
    echo '<img src="' . $variation['image_src'] . '" style="display:none;">';
}

在循环外,您可以使用此操作:

$product = new WC_Product_Variable( $product_id );

You can put all the variation images in ‘img’ elements. And make the ‘img’ element dissapear by setting ‘display:none’. That way, the browser will start downloading the inages as soon as it encounters the elements and they will be cached for the next time they need to display. You can do this in the product loop:

$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
    echo '<img src="' . $variation['image_src'] . '" style="display:none;">';
}

Outside the loop, you can use this:

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