23

I know that in Magento 1.4.2.0 one gets parent id's like so

list( $parentId ) = Mage::getModel('catalog/product_type_configurable')
                            ->getParentIdsByChild( $product->getId() );

My question is: if I don't know what the parent is, how do I know to use the 'catalog/product_type_configurable' vs 'catalog/product_type_grouped' model to get the id?

YakovL
  • 7,557
  • 12
  • 62
  • 102
veilig
  • 5,085
  • 10
  • 48
  • 86

5 Answers5

36

You can just call both and offer a fall-back as it should be one or the other:

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
    }
}
Kus
  • 2,529
  • 27
  • 27
  • 4
    Just a note to others, depending on your catalog you may want to check configurable products first if they occur more frequently. – KTastrophy Jan 23 '14 at 01:30
  • 9
    You could use the constant Mage_Catalog_Model_Product_Type::TYPE_SIMPLE instead of "simple". – magic_al Jan 24 '14 at 11:33
10

You may use:

$product->getTypeInstance();

Which will return the type object of your product

Then you can perform your:

->getParentIdsByChild()

Giving finally:

$product->getTypeInstance()->getParentIdsByChild($child->getId());
alanaktion
  • 1,326
  • 1
  • 11
  • 18
eric ramahatra
  • 165
  • 1
  • 4
  • 6
    How is this going to work ? $product->getTypeInstance() will return simple product instance. Still we do not know what type is the parent product. – Hashid Hameed May 30 '15 at 11:24
8

Here is another solution for magento 1.7.2

$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($mageProduct->getId());
WonderLand
  • 5,494
  • 7
  • 57
  • 76
lavb
  • 618
  • 7
  • 20
1

we can use in block file,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;     
        }   
Rakesh Jesadiya
  • 436
  • 1
  • 11
  • 25
0

You could check the type of the product with $_product->getTypeId(); and if this returns 'configurable', take the configurable model and if it returns 'grouped' take the grouped model.

Hope this helps.

Simon
  • 4,103
  • 7
  • 28
  • 53
  • 2
    not sure it does :( if you have a simple product and trying to get its parent (grouped or config) the getTypeId method will only return 'simple'...and you still don't know if you need to use the config or grouped model. – veilig Aug 10 '11 at 17:38
  • I am sorry, you are absolutely right. So I do not know anything better than use both, the configurable and grouped model and then merge the results. You could also check which one returns an empty array and ignore this one. – Simon Aug 10 '11 at 21:47
  • how do you "take the configurable model"? – ahnbizcad Feb 17 '16 at 19:49
  • 1
    @ahnbizcad `Mage::getModel('catalog/product_type_configurable')` – Simon Feb 17 '16 at 20:04