我知道在Magento 1.4.2.0中,父id是这样的
list( $parentId ) = Mage::getModel('catalog/product_type_configurable')
->getParentIdsByChild( $product->getId() );
我的问题是:如果我不知道父母是什么,我怎么知道使用'catalog/product_type_可配置' vs 'catalog/product_type_分组'模型来获得id?
你可以同时调用两者,并提供一个退一步,因为它应该是其中之一:
if($product->getTypeId() == "simple"){
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($product->getId());
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
if(isset($parentIds[0])){
$parent = Mage::getModel('catalog/product')->load($parentIds[0]);
// do stuff here
}
}
您可以使用:
$product->getTypeInstance();
返回产品的类型对象
然后你可以执行:
->getParentIdsByChild()
给最后:
$product->getTypeInstance()->getParentIdsByChild($child->getId());
这是magento 1.7.2的另一个解决方案
$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($mageProduct->getId());
我们可以在块文件中使用,magento 2,
protected $_catalogProductTypeConfigurable;
public function __construct(
'Magento'Catalog'Block'Product'Context $context,
//for getting parent id of simple
'Magento'ConfigurableProduct'Model'ResourceModel'Product'Type'Configurable $catalogProductTypeConfigurable,
array $data = []
) {
//for getting parent id of simple
$this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
parent::__construct($context, $data);
}
public function getProductData($id){
$parentByChild = $this->_catalogProductTypeConfigurable->getParentIdsByChild($id);
if(isset($parentByChild[0])){
//set id as parent product id...
$id = $parentByChild[0];
}
return $id;
}
您可以使用$_product->getTypeId();
检查产品类型,如果返回'可配置',则取可配置模型,如果返回'分组',则取分组模型。