Magento:如何将两个产品集合合并成一个?

编程入门 行业动态 更新时间:2024-10-25 16:29:25
本文介绍了Magento:如何将两个产品集合合并成一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有两个产品集合是有办法将它们合并成一个?

if i have two product collections is there a way to merge them into one?

例如(我最终的目的不是只是得到一个集合2 cats,这只是为了说明问题):

for example (my final intent isn't to actually just get a collection of 2 cats, this is just to illustrate the issue):

$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection(); $collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection(); $merged_collection = merge_collections($collection1,$collection2);

任何帮助将不胜感激。

推荐答案

假设您希望组合在一起的项目属于相同类型并且存在于数据库中,那么您可以这样做:

Assuming the items you wish to group together are of the same type and exist in the database then you can do this:

$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection(); $collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection(); $merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds()); // can sometimes use "getLoadedIds()" as well $merged_collection = Mage::getResourceModel('catalog/product_collection') ->addFieldToFilter('entity_id', array('in' => $merged_ids)) ->addAttributeToSelect('*');

这里我知道要过滤 entity_id 即产品的关键字段,就像它是对大多数实体类型,一些平面表有不同的主键。通常你可以使用集合的 getIdFieldName()方法推广它。在这种情况下,产品是一个坏的例子,因为它的ID字段名称没有正确填写。

Here I know to filter by entity_id because that is products' key field, like it is for most entity types, some flat tables have a different primary key. Often you can generalise that with one of the collection's getIdFieldName() method. Products are a bad example in this case because it's ID field name isn't filled out correctly.

更多推荐

Magento:如何将两个产品集合合并成一个?

本文发布于:2023-11-29 00:16:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   两个   产品   Magento

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!