php 检查 Magento 产品是否是可配置产品的子项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1706097/
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
Checking if a Magento product is a child of a configurable product
提问by chris.rickard
I have the following code to grab a list of Products
我有以下代码来获取产品列表
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name')
->addAttributeToFilter("category_ids", array('finset'=>$this->category_id));
foreach($collection as $product) {
echo $product->getName();
}
My question is, how can I NOT echo products that are 'simple' but belong to a parent 'configurable' product. (for example don't show "Red Shirt Medium" as it belongs to "Red Shirt")
我的问题是,我怎么能不回显“简单”但属于“可配置”父产品的产品。(例如不要显示“Red Shirt Medium”,因为它属于“Red Shirt”)
I have worked out that this association lives in 'catalog_product_super_link' but I have only just started with Magento and unfortuantely don't know how to do the filtering :)
我已经发现这个关联存在于 ' catalog_product_super_link' 但我才刚刚开始使用 Magento,不幸的是不知道如何进行过滤:)
Cheers guys,
干杯,伙计们,
Chris.
克里斯。
回答by michaelk
I don't know a direct way to add this condition to the collection, I'd be interested in such a solution too. But you can always check inside the loop for each product:
我不知道将此条件添加到集合中的直接方法,我也对这样的解决方案感兴趣。但是您始终可以检查每个产品的循环内部:
if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId())))
{
echo $product->getName();
}
回答by deepmark
I've done something similar for our google feed. This excerpt of code is what I use to check the products inheritance:
我为我们的谷歌提要做了类似的事情。这段代码摘录是我用来检查产品继承的:
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('price', array('gt' => 0) );//price not 0
//$products->addAttributeToFilter('visibility', 4); //catalog, search - comment out to show all items (configurable products simple product breakdowns)
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
$prodIds=$products->getAllIds();
try {
foreach($prodIds as $productId) {
$product = Mage::getModel('catalog/product');
$product->load($productId);
// SIMPLE PRODUCTS
if($product->getTypeId() == 'simple' ) {
$prodName = trim($product->getName());
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
if($parentIds) {
$parentProd = Mage::getModel('catalog/product')->load($parentIds[0]);
/*
* do something if this product has a parent or do some checks against $parentProd
*/
} // end parent check
}//if SIMPLE
} // foreach
} catch(Exception $e) {
die($e->getMessage());
}
回答by John Cuthbert
There's a function called isConfigurablein the product class.
isConfigurable在产品类中调用了一个函数。
That may be of help to you.
那可能对你有帮助。
$product->isConfigurable();
// if its the parent object it'll be true, if its the child it'll be false.
回答by cfx
The quickest way might be to check if the product's visibility is set to "Not Visible Individually", since simple products associated with configurable products are typically set to this. Unfortunately I don't know the precise syntax but hopefully someone else who's willing to chime in does!
最快的方法可能是检查产品的可见性是否设置为“单独不可见”,因为与可配置产品关联的简单产品通常设置为“不可见”。不幸的是,我不知道确切的语法,但希望其他愿意插话的人知道!
回答by Aad Mathijssen
Since simple products that are part of configurable products usually have a visibility value of Not Visible Individually, it probably suffices to add a visibility filter to the collection that checks for visibility of the products in the catalog:
由于作为可配置产品一部分的简单产品的可见性值通常为Not Visible Individually,因此向集合添加可见性过滤器可能就足够了,以检查目录中产品的可见性:
$collection->setVisibility(Mage::getModel('catalog/product_visibility')->getVisibleInCatalogIds());
In the unlikely circumstance that the resulting products are part of a configurable product, you can use the method Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChildto check if a product is used as part of a configurable product.
在生成的产品是可配置产品的一部分的不太可能的情况下,您可以使用该方法Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChild检查产品是否用作可配置产品的一部分。

![当 mysql 命令行工作时,为什么 PHP PDO 会收到“SQLSTATE[42000] [1044] 用户拒绝访问”?](/res/img/loading.gif)