WooCommerce |可变产品 |将相同的图像分配给每个值的属性 X(在 Y 中) [英] WooCommerce | Variable Product | Assign same image to Attribute X (out of Y) per value

查看:61
本文介绍了WooCommerce |可变产品 |将相同的图像分配给每个值的属性 X(在 Y 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 变量产品中,有创建变体的属性和属性值.

In WooCommerce variable product, there are attributes and attribute values, which create variations.

E.g. :
Attribute: 1 (Size) | Values: XS/S/M/L/XL
Attribute: 2 (Style/Color) | Values: Red/Green/Blue/Black/...
Attribute: 3 (Fabric) | Cotton/Polyester/Some other stuff...

假设每个样式值有 1 张图片.我们还假设您总共有 500 个变体(由于三重属性,它们可以叠加).

Let's assume you have 1 picture per style value. Let's also assume you have 500 total variations (due to triple attributes, which stacks).

是否有一种简单的方法可以将每个ATTRIB2 VALUE"关联一张图片,这样您就不必手动编辑变体中的每张图片?

具有 500 个单一变体的 100 种产品似乎是很多猴子的工作,必须有更好的方法.

100 products with 500 single variations seems like a lot of monkey work, there has to be a better way.

1 张所有蓝色变体的图片;1 张所有红色变体的图片...等等...

1 picture for ALL blue variations; 1 picture for all red variations... etc...  

附带问题:有没有办法使用批量设置常规价格"来应用于所有变体,而不仅仅是活动页面"上的变体?

Side-question: Is there a way to use the bulk "Set Regular Price" to apply to ALL variations instead of only the ones on active "page"?

推荐答案

以下在我的测试安装中对我有用:

The following works for me in my test installation:

/**
 * Replace Woocommerce Product Variation Images
 * based on variation slug 
 */

add_filter( 'woocommerce_product_variation_get_image_id', 'so_filter_wooc_product_variation_images', 10, 2 ) ;

function so_filter_wooc_product_variation_images( $image_id, $data ) {

    //the variation product slug includes "-black"...
    if ( strpos( $data->slug, '-black') ) { 

        //replace with new image by id number
        return $new_image_id ;

    } 

    //else return the image_id according to default settings
    return $image_id ;

}

现在,您不必像我在这里所做的那样搜索产品标号,这是必然的:还有其他变量,部分取决于您的安装设计,可以将一组变量产品与另一组变量产品区分开来.但是,默认情况下,产品变体的产品标签将组合属性术语.因此,如果您的产品是测试产品";您有两种尺寸,大"和小",以及两种颜色,黑色"和白色";他们能够产生变化;并且变体是定价的(这是必需的),您将获得格式为 test-product-big-blacktest-product-big-white 的产品 slug,等等,所以这似乎是实现大致目标的合理方法,专注于属性.

Now, you don't have search the product slug as I have done here, necessarily: There are other variables that, depending in part on your installation design, may distinguish one set of variable products from another. By default, however, the product slug for a product variation will combine the attribute terms. So, if your product is "Test Product"; you have two sizes, "Big" and "Small," and two colors, "Black" and "White"; they're enabled to produce variations; and the variations are priced (which is required), you'll get product slugs in the format test-product-big-black and test-product-big-white, and so on, so this seems like a sound method for achieve approximately what you want, focused on the attribute.

然而,这不是万无一失的方法:如果属性名称出现在商品标题中,则可能偶尔会出现重叠 - 例如,对于标题为测试黑色产品"的产品.显然,如何以编程方式解决这个问题是一个更复杂的问题:我在这里提供的函数仍然说明了一般概念.

It's not, however, a foolproof method: if the attribute name occurs in the item title, then there's some chance of occasional overlap - for, say, a product with the title "Test Black Product." How to solve this issue programmatically is, obviously, a more complicated question: The function I've provided here still illustrates the general concept.

至于 $new_image_id_number,你可以通过各种不同的方式获得它,所以我把它保持开放.例如,您可以通过将鼠标悬停在要在媒体库中使用的图像上来查找附件 ID.或者,您可以创建一个站点选项 - 老式的或者可能使用 Advanced Custom Fields Pro - 并从图像上传中提取 ID.

As for $new_image_id_number, you could get that in various different ways, so I've left that open. For example, you could just find the attachment ID by hovering over the image you want to use in the Media Library. Alternatively, you could create a site option - the old-fashioned away or maybe with Advanced Custom Fields Pro - and extract the ID from an image upload.

无论如何,这个可行的解决方案非常简单.过滤器钩子基于abstract-wc-data.php中的protected函数...

Anyway, this working solution turns out to be fairly simple. The filter hook is based on the protected function in abstract-wc-data.php...

$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );

...由钩子前缀"(在本例中为 woocommerce_product_variation_)和相关属性(在本例中为 image_id)构建而成.

...which is constructed from the "hook prefix," in this case woocommerce_product_variation_, and the property in question, in this case image_id.

(我最初很困惑,查看产品变体类时,我发现了一个具有类似结构的过滤器,该过滤器基本上复制了某些不适用于此处的情况的功能.)

(I was initially confused when, viewing the product variation class, I found a filter with a similar structure that essentially duplicates the functionality for certain cases that don't apply here.)

这篇关于WooCommerce |可变产品 |将相同的图像分配给每个值的属性 X(在 Y 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆