简体   繁体   English

在 Woocommerce 中男女通用产品:如何根据男性或女性类别选择显示不同的图像

[英]In Woocommerce for Unisex Product: How can I Show A Different Image Based on Men Or Women Category Selection

Many of my products are unisex, how can I display my male mockup images for unisex products listed within the "Men's T-Shirts" category and vice versa for women?我的许多产品都是男女皆宜的,如何显示“男士 T 恤”类别中列出的男女通用产品的男性样机图像,反之亦然? Ideally the image would be pulled from the image gallery of the product so the male & female image would be the the 2nd & 3rd image within the product image gallery.理想情况下,图片将从产品图片库中提取,因此男性和女性图片将成为产品图片库中的第二张和第三张图片。

I found a code that allows for displaying different images from the shop page vs the single product page but I can't seem to figure out the code to base it on a "if within cateogry" calling.我找到了一个代码,该代码允许显示商店页面与单个产品页面中的不同图像,但我似乎无法弄清楚该代码是否基于“如果在类别中”调用。 This is the code: add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2); function remove_featured_image($html, $attachment_id ) { global $post, $product; $featured_image = get_post_thumbnail_id( $post->ID ); if ( $attachment_id == $featured_image ) $html = ''; return $html; }这是代码: add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2); function remove_featured_image($html, $attachment_id ) { global $post, $product; $featured_image = get_post_thumbnail_id( $post->ID ); if ( $attachment_id == $featured_image ) $html = ''; return $html; } add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2); function remove_featured_image($html, $attachment_id ) { global $post, $product; $featured_image = get_post_thumbnail_id( $post->ID ); if ( $attachment_id == $featured_image ) $html = ''; return $html; }

And was playing with this code to select the image from the gallery: $attachment_id = $product->get_gallery_attachment_ids()[0]; echo "<img src='". wp_get_attachment_url( $attachment_id ). "'>";并正在使用此代码从图库中选择图像: $attachment_id = $product->get_gallery_attachment_ids()[0]; echo "<img src='". wp_get_attachment_url( $attachment_id ). "'>"; $attachment_id = $product->get_gallery_attachment_ids()[0]; echo "<img src='". wp_get_attachment_url( $attachment_id ). "'>";

I'm not sure if I'm playing with the right code.我不确定我是否在玩正确的代码。 I would appreciate any help.我将不胜感激任何帮助。

  • Jane

You can use a simple, no code solution.您可以使用简单的无代码解决方案。 Create a separate products for men and women, then use a plugin like: https://www.wooextend.com/product/group-stock-manager-shared-stock-woocommerce/为男性和女性创建单独的产品,然后使用如下插件: https ://www.wooextend.com/product/group-stock-manager-shared-stock-woocommerce/

to connect both products so they share the same stock.连接两种产品,使它们共享相同的库存。 This way you can add separate images and categories but when "men" product is bought, the stock of "women" product will decrease too.这样您可以添加单独的图像和类别,但是当购买“男士”产品时,“女士”产品的库存也会减少。 Very simple and effective.非常简单有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM