如何在 Magento 产品列表中以每种颜色显示可配置产品?
我有一个可配置的产品,有多种不同的颜色和尺寸可供选择。我希望可配置产品为每种颜色出现一次。我的想法是将每种颜色的可配置产品的一个简单产品分配给可配置产品的类别。然后我想更改列表,以便(彩色)简单产品链接到它的主产品(可配置产品)。
另一种方法是,将可配置产品分配到一个类别,然后用不同的颜色多次列出它。但我认为这太复杂了。
解决方案
真诚地,我丢失了我的代码。但我的管理方法如下:
- 将所有从属产品的
visibility
设置为catalog
,以便它们 出现在产品列表中 - 覆盖产品模型及其 getProductUrl 函数:
public function getProductUrl($useSid = null)
{
$product = $this;
$product->loadParentProductIds();
$parentIds = $product->getParentProductIds();
if(count($parentIds) > 0 && $product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
{
$parent = Mage::getModel("catalog/product")->setId($parentIds[0])->load();
return $this->getUrlModel()->getProductUrl($parent, $useSid);
}
return $this->getUrlModel()->getProductUrl($product, $useSid);
}
这样每个从属产品都会链接到其主产品。棘手的部分是将属性附加到 url。您可以将 #attributecode1=value1&attributecode2=value2
添加到 url 以预选属性选择框。我只快速完成了这一部分肮脏,我很确定有人可以做得更好。
预选示例:
http://demo.magentocommerce.com/ anashria-女士-premier-leather-sandal-7.html http://demo.magentocommerce.com/ anashria-womens-premier-leather-sandal-7.html#502=43
I have a configurable product which is available in many different colors and sizes. I want the configurable product to appear once for every color. My idea is to assign one simple product of the configurable product in every color to the category of the configurable product. Then I want to change the listing, so that the (colored) simple product links to it's master product (the configurable one).
The other way would be, to just assign the configurable product to a category and then list it multiple times with different colors. But I think this would be to complicated.
Solution
Sincerely I have lost my code. But here is how I've managed it:
- Set
visibility
for all slave products tocatalog
so that they
appear in the product listing - Override the Product Model and it's getProductUrl function:
public function getProductUrl($useSid = null)
{
$product = $this;
$product->loadParentProductIds();
$parentIds = $product->getParentProductIds();
if(count($parentIds) > 0 && $product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
{
$parent = Mage::getModel("catalog/product")->setId($parentIds[0])->load();
return $this->getUrlModel()->getProductUrl($parent, $useSid);
}
return $this->getUrlModel()->getProductUrl($product, $useSid);
}
This way each slave product links to it's master product. The tricky part is to attach the attributes to the url. You can add #attributecode1=value1&attributecode2=value2
to the url to preselect the attribute select boxes. I only had this part quick & dirty and am pretty sure someone can do this much better.
Example for preselection:
http://demo.magentocommerce.com/anashria-womens-premier-leather-sandal-7.html
http://demo.magentocommerce.com/anashria-womens-premier-leather-sandal-7.html#502=43
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不明白为什么您不根据每种颜色的尺寸制作可配置的产品?这样您就不需要破坏 Magento 的工作方式。
如果您制作一个简单的产品,该产品是前端可见的可配置产品的一部分,那么它不会链接到可配置产品(如果它是可配置产品的一部分)(正如您所发现的)。这对您来说也没有任何意义,因为如果您的可配置产品基于尺寸和颜色,那么简单的产品将具有固定的尺寸和颜色。
如果您只是为每种衬衫颜色制作一个可配置的产品,那么您就完成了,功能齐全且无黑客攻击。然后,您还可以使用相关产品来展示其他衬衫颜色。
黑客攻击越少越好。这是我的意见。
I don't understand why you just don't make a configurable product based on size for every color? That way you don't need to hack the way Magento works.
If you make a simple product that is part of a configurable product visible on the frontend, it will not link to a configurable product, if it is part of one (as you have found out). It wouldn't really make sense for you either because if your configurable products are based on size AND color, the simple products are going to have a set size and set color.
You would be done, fully functional, and hack-free if you just made a configurable product for each shirt color. Then, you can also use related products to show other shirt colors.
The less hacking, the better. That's my opinion.
修复此线程,以防其他人需要在 Magento 2 中执行此操作。
下面是我的解决方案。请记住,它很 hacky,会破坏很多东西,所以只有当您是 Magento 开发人员,知道他/她在做什么并且可以修复或忍受此代码的负面影响时才使用。
已知问题:
无效,这可能会在其他地方引起问题。
Necroing this thread in case others need to do this in Magento 2.
Below is my solution. Please keep in mind that it is hacky and will break many things, so only use if you're a Magento developer who knows what he/she is doing and can either fix or live with the negative side-effects of this code.
Known issues:
invalid, and this may cause issues elsewhere.
一种方法是将尺寸和颜色作为目录号的一部分(或您用于产品的任何唯一识别号)
因此,假设您有一个有 2 种颜色和 3 种尺寸的小部件,它的目录号是“ qwe123”。您可以将以下两项以及适当的图像输入系统中。我假设您已经有办法处理尺寸。
这种方式不需要额外的编程,但是如果您想链接到产品页面上提供的其他颜色,那么您将必须解析目录号的第一部分并搜索匹配的颜色。
One way would be to make the size and color part of the catalog number (or whatever unique identifying number you are using for the product)
So lets say you have a widget that comes in 2 colors and 3 sizes, and it's catalog number is "qwe123". You would enter the following 2 items into the system, along with appropriate images. I'm assuming you already have a way to deal with the sizes.
There is no extra programing involved to do it this way, but if you want to link to the other colors that are available from the product page then you will have to parse out the first part of the catalog number and search for the ones that match.
为了将简单产品重定向到可配置的父产品,您可以创建 Magento\Catalog\Model\Product::getProductUrl() 的“nofollow noreferrer">插件(拦截器),用于更改简单产品的 URL:
在可配置中预选简单产品产品,简单产品的地址应如下所示:
其中
/mona-pullover-hoodlie.html
- 可配置的产品 URL,143
code>、93
- 属性 ID、167
、53
- 选项 ID。属性 ID 和选项 ID 可以使用
Magento\ConfigurableProduct\Model\Product\Type\Configurable::getConfigurableAttributesAsArray($product)
函数获取。我在 Magento Marketplace 上制作了一个
VCT Simple Product URL
模块来解决这个问题。In order to redirect simple products to configurable parent product, you can create a Plugin (Interceptor) for
Magento\Catalog\Model\Product::getProductUrl()
, where to change URL for simple products:To preselect a simple product in a configurable product, the address of a simple product should look like this for example:
where
/mona-pullover-hoodlie.html
- configurable product URL,143
,93
- attributes IDs,167
,53
- option IDs.Attributes IDs and option IDs can be obtained using
Magento\ConfigurableProduct\Model\Product\Type\Configurable::getConfigurableAttributesAsArray($product)
function.I made a
VCT Simple Product URL
module on Magento Marketplace that solves this problem.