wordpress Woocommerce - 如何链接到产品变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33202678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce - How To Link to Product Variation?
提问by MrMo
I am trying to find a way to have a link to a specific product variation.
我试图找到一种方法来链接到特定的产品变体。
I thought I had it solved with this plugin... https://wordpress.org/plugins/woocommerce-direct-variation-link/
我以为我已经用这个插件解决了... https://wordpress.org/plugins/woocommerce-direct-variation-link/
However the I have 2 word product attribute names and can't seem to get it to work? I put _, -, & and + between the 2 words but not go.
但是,我有 2 个单词的产品属性名称,似乎无法使用它?我把 _、-、& 和 + 放在两个词之间,但没有去。
Here is the doc...In my case I would have "color style" instead of just "color"mysite.com/product/happy-ninja/?color=blue
这是文档...在我的情况下,我将使用“颜色样式”而不仅仅是“颜色”mysite.com/product/happy-ninja/?color=blue
mysite.com/product/happy-ninja/?color=blue&size=small (additional variations should be separated by '&')
mysite.com/product/happy-ninja/?color=blue&size=small(其他变体应以“&”分隔)
mysite.com/product/happy-ninja/?color=blue+green (where the variation value is "Blue Green" with the space replaced by a '+')
mysite.com/product/happy-ninja/?color=blue+green(其中变化值为“蓝绿色”,空格替换为“+”)
回答by Ashwin Anandani
Further to the last poster, I couldn't get his code to work properly, but I did discover that WooCommerce generates these for the Cart most of the time:
在最后一张海报之后,我无法让他的代码正常工作,但我确实发现 WooCommerce 大部分时间都会为购物车生成这些代码:
- Visit a variation product yourself
- Add a variation to the Cart
- Open your cart and hover over the item --> the link in your status bar (or right click > Copy Link Address) is it!
- 亲自访问变体产品
- 向购物车添加变体
- 打开您的购物车并将鼠标悬停在该项目上 --> 状态栏中的链接(或右键单击 > 复制链接地址)是它!
It should have a suffix like this: http://website.com/product/product-name/?attribute_colours=Grey
它应该有这样的后缀:http: //website.com/product/product-name/?attribute_colours =Grey
Note that colours
is lowercase because it's the slug while Grey
is the actual name for the Variation.
请注意,它colours
是小写的,因为它Grey
是变体的实际名称。
For spaces in your Variation name, use a +
instead of the space.
对于变体名称中的空格,请使用 a+
代替空格。
For selecting multiple variation attributes, I believe you can just start with &
and spit the ?attribute_[slug]=Variation
again.
对于选择多个变体属性,我相信您可以直接开始&
并?attribute_[slug]=Variation
再次吐出。
回答by user7380555
I have tried so many solutions and plugins myself, but i found a way to easily do it without a plugin. I'm not sure if this worked back in oktober 2015, but it does work now.
我自己尝试了很多解决方案和插件,但我找到了一种无需插件即可轻松完成的方法。我不确定这在 2015 年 10 月是否有效,但现在确实有效。
I have explained it in this youtube video https://www.youtube.com/watch?v=Y_hMI4bXN6A
我已经在这个 youtube 视频https://www.youtube.com/watch?v=Y_hMI4bXN6A 中进行了解释
What you want to do is add something like '/?attribute_color=red' behind your product.
您想要做的是在您的产品后面添加诸如“/?attribute_color=red”之类的内容。
website.com/shop/product1/?attribute_color=red
website.com/shop/product1/?attribute_length=10m
However what i found out by trial and error is what matters is the type of attribute you use. You can add a attribute in 2 ways, a pre-made one (text/select) or a custom attribute. Linking directly with this method only works if you create a custom product attribute.
但是,我通过反复试验发现重要的是您使用的属性类型。您可以通过两种方式添加属性,一种是预制的(文本/选择),一种是自定义属性。仅当您创建自定义产品属性时,直接使用此方法链接才有效。
For it to work you have to pay attention if you used capitol letters or not. For the name of the custom product attribute it wont matter if you use capitols in the backend (as long as you dont use theme in the url). For the values however you can only use normal letters and no capitols, both in the backend as in the url itself.
为了让它工作,你必须注意是否使用了大写字母。对于自定义产品属性的名称,如果您在后端使用 capitols 并不重要(只要您不在 url 中使用主题)。但是,对于这些值,您只能在后端和 url 本身中使用普通字母而不能使用大写字母。
Example
例子
Name:Color
名称:颜色
Values:red | Black, green
值:红色 | 黑色、绿色
website.com/shop/product1/?attribute_color=red > will work
website.com/shop/product1/?attribute_Color=red > will not work
website.com/shop/product1/?attribute_color=Red > will not work
website.com/shop/product1/?attribute_color=Black > will work
website.com/shop/product1/?attribute_color=black > will not work
I have explained it in this youtube video https://www.youtube.com/watch?v=Y_hMI4bXN6A
我已经在这个 youtube 视频https://www.youtube.com/watch?v=Y_hMI4bXN6A 中进行了解释
回答by marvinthegreat
I am using a plugin Variation Swatches for WooCommerceso I am not sure if this is core wordpress / woocommerce functionality or not.
我正在为 WooCommerce使用插件Variation Swatches,所以我不确定这是否是核心 wordpress/woocommerce 功能。
What I did was open one of my products and in ""Product data" went to "Linked Products" tab and linked one of my variations to the product. Updated and went to the product page. After clicking on the linked product I got url that looks like this:
我所做的是打开我的一个产品,然后在“产品数据”中转到“链接产品”选项卡并将我的一个变体链接到产品。更新并转到产品页面。点击链接的产品后,我得到了网址看起来像这样:
example.eg/product/product-name-slug/?attribute_pa_kids-size=27&attribute_pa_color=navy
following ?attribute_pa_
is my attribute name slug (kids-size) and following =
is slug for the single attribute slug (27, 28, blue, red, navy ...).
As you can see it works for multiple attributes in url.
If you don't know your slugs, just link your variation product to another and copy the link.
以下?attribute_pa_
是我的属性名称 slug (kids-size),以下=
是单属性 slug (27, 28, blue, red, navy ...)。如您所见,它适用于 url 中的多个属性。如果您不知道您的 slug,只需将您的变体产品链接到另一个产品并复制链接。
回答by Richard Aubin
I have not tested this, but try replacing your space in your variation name to %20 which is the url-encoded version of the space character.
我尚未对此进行测试,但尝试将变体名称中的空格替换为 %20,这是空格字符的 url 编码版本。
回答by Unicco
You can just:
你可以:
$variation_product_url = $variation_product->get_permalink();
WooCommerce handles the rest; adding the correct parameters to the url.
WooCommerce 处理其余的;将正确的参数添加到 url。
回答by GDY
I think many will search for a solution to link specific variations by a url parameter so here is a longer answer for that topic.
我认为许多人会寻找通过 url 参数链接特定变体的解决方案,因此这里是该主题的更长答案。
As many others mentioned here, you can access any variation with the attribute parameter. The way you choose an variation on the product details page is an select menu. The name attribute of that select is the parameter name and the options value attribute is the parameter value. Here is an example:
正如此处提到的许多其他人一样,您可以使用属性参数访问任何变体。您在产品详细信息页面上选择变体的方式是选择菜单。该选择的名称属性是参数名称,选项值属性是参数值。下面是一个例子:
If you want to link the highlighted variation your link will be like https://example.com/some_product/?attribute_pa_size=small
如果您想链接突出显示的变体,您的链接将类似于 https://example.com/some_product/?attribute_pa_size=small
Of course you can add multiple parameters like https://example.com/some_product/?attribute_pa_size=small&attribute_pa_color=red
当然,您可以添加多个参数,例如 https://example.com/some_product/?attribute_pa_size=small&attribute_pa_color=red
The thing is, using dev tools to dig out these information is not a very convenient solution. You can pass the woocommerce variation id through get_permalink the get an actual url … here is a quick snippet that does exactly that … The variation-permalink should be visible in the wordpress admin of the specific product variation tab/accordeon.
问题是,使用开发工具挖掘这些信息并不是一个非常方便的解决方案。您可以通过 get_permalink 传递 woocommerce 变体 ID,获取实际 url ……这是一个快速片段,正是这样做的……变体永久链接应该在特定产品变体选项卡/手风琴的 wordpress 管理中可见。
add_action( 'woocommerce_product_after_variable_attributes', 'gdy_add_wc_varition_permalink', 100, 3 );
function gdy_add_wc_varition_permalink( $loop, $variation_data, $variation ) {
echo '<a href="' . get_permalink( $variation->ID ) . '" target="_blank">' . __( 'Permalink for this variation' ) . '</a>';
}
回答by Lasse Kristiansen
You can create direct links to variants in any case, meaning both with variants using custom attributes and variants using predefined attributes.
您可以在任何情况下创建指向变体的直接链接,这意味着变体使用自定义属性和变体使用预定义属性。
There is just a slight difference in the URL parameter you need to append to the URL.
您需要附加到 URL 的 URL 参数略有不同。
Examples:
例子:
Custom attribute variant URL:
自定义属性变体 URL:
domain.com/shop/product-x/?attribute_color=blue
Predefined attribute variant URL:
预定义的属性变体 URL:
domain.com/shop/product-x/?attribute_pa_color=blue
The only difference is "pa_".
唯一的区别是“pa_”。
Note: The question is quite old, but I got here and didn't find a full answer, so figure I'd add it for others to benefit from.
注意:这个问题很老了,但我到了这里并没有找到完整的答案,所以我想我会添加它让其他人从中受益。
回答by Norbert Nowocin
Simply look at your HTML code of single product page, find your product variable select combo ID and add at he end of URL: ?attribute_YOUR_ID=YOUR_VALUE
只需查看您的单个产品页面的 HTML 代码,找到您的产品变量选择组合 ID 并在 URL 末尾添加:?attribute_YOUR_ID=YOUR_VALUE
You canuse combinations if you have multiple variable options, separate them with &
如果您有多个变量选项,您可以使用组合,用&分隔它们
example:
例子:
website.com/shop/product1/?attribute_id_of_variable_combo=value_of_combo