php 如何在magento中获取父产品ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7014530/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:48:41  来源:igfitidea点击:

How to get parent product id in magento?

phpmagentomagento-1.4

提问by veilig

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

我知道在 Magento 1.4.2.0 中,父 ID 是这样的

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?

我的问题是:如果我不知道父母是什么,我怎么知道使用“目录/ product_type_配置” VS“目录/ product_type_分组”的模式,以获得ID?

采纳答案by eric ramahatra

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());

回答by Kus

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

回答by lavb

Here is another solution for magento 1.7.2

这是 magento 1.7.2 的另一个解决方案

$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($mageProduct->getId());

回答by Rakesh Jesadiya

we can use in block file,magento 2,

我们可以在块文件中使用,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;     
        }   

回答by Simon

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.

您可以检查产品的类型,$_product->getTypeId();如果返回“可配置”,则采用可配置模型,如果返回“分组”,则采用分组模型。

Hope this helps.

希望这可以帮助。