¿Cómo obtener el id de producto principal en magento?


Sé que en Magento 1.4.2.0 uno obtiene la identificación del padre así

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

Mi pregunta es: si no sé cuál es el padre, ¿cómo sé usar el modelo 'catalog/product_type_configurable' vs 'catalog/product_type_agrupado' para obtener el id?

Author: YakovL, 2011-08-10

5 answers

Puede utilizar:

$product->getTypeInstance();

Que devolverá el objeto tipo de su producto

Entonces usted puede realizar su:

->getParentIdsByChild()

Dando finalmente:

$product->getTypeInstance()->getParentIdsByChild($child->getId());
 9
Author: eric ramahatra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-30 17:10:52

Puedes llamar a ambos y ofrecer una alternativa, ya que debería ser una u otra:

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
    }
}
 30
Author: Kus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-07 01:34:36

Aquí hay otra solución para magento 1.7.2

$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($mageProduct->getId());
 6
Author: lvillarino,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-12 09:42:31

Podemos usar en 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;     
        }   
 1
Author: Rakesh Jesadiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-04 07:03:42

Puedes comprobar el tipo de producto con $_product->getTypeId(); y si devuelve 'configurable', toma el modelo configurable y si devuelve 'agrupado' toma el modelo agrupado.

Espero que esto ayude.

 0
Author: Simon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-10 16:52:07