How to get parent product id in magento?

后端 未结 5 1808
旧巷少年郎
旧巷少年郎 2021-02-07 01:55

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

list( $parentId ) = Mage::getModel(\'catalog/product_type_configurable\')
                                   


        
5条回答
  •  星月不相逢
    2021-02-07 02:33

    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;     
            }   
    

提交回复
热议问题