从子SKU获取父SKU(可配置或捆绑) [英] Get parent SKU (configurable or bundle) from child SKU

查看:215
本文介绍了从子SKU获取父SKU(可配置或捆绑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在购物车页面上,我需要能够使用子SKU获得父SKU.

On the cart page I need to be able to obtain the parent SKU using a child SKU.

我已经尝试过从Magento论坛和此处类似的问题(在StackOverflow上)摘录的几个代码都没有成功.

I've tried several code snipped from both the Magento forums and similar questions here on StackOverflow without success.

我可以通过使用getTypeId()来确定某个产品是否只是没有父项的简单产品,但是在此之后,我尝试执行的所有操作均无法获得父项SKU.

I'm able to determine if a product is just a simple product without a parent by using getTypeId() but after that everything I try fails to result in getting at the parent SKU.

Magento版本:1.4.2.0

Magento Version: 1.4.2.0

推荐答案

看看Mage_Catalog_Model_Product_Type_Configurable和Mage_Bundle_Model_Product_Type类.他们有获取父母和孩子产品的有用方法.您需要getParentIdsByChild():

Take a look at the Mage_Catalog_Model_Product_Type_Configurable and Mage_Bundle_Model_Product_Type classes. They have useful methods for getting parent and child products. You want getParentIdsByChild():

对于可配置产品:

$parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childId);

对于捆绑产品:

$parent_ids = Mage::getModel('bundle/product_type')->getParentIdsByChild($childId);

这些仅适用于ID.您需要将子SKU转换为ID,然后将父ID转换回SKU.从SKU获取ID的一种简单方法是:

These only work with ids. You'll need to convert the child SKU to an id and then the parent id back to a SKU. A simple way to get the id from the SKU is:

Mage::getModel('catalog/product')->getIdBySku($sku);

此外,您可以有多个父ID,因此您必须要意识到这一点.这是一个示例:

Also, you can have multiple parent ids, so you'll have to be aware of that. Here's an example:

$child_id = Mage::getModel('catalog/product')->getIdBySku($child_sku);
$parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($child_id);
$parent_collection = Mage::getResourceModel('catalog/product_collection')
    ->addFieldToFilter('entity_id', array('in'=>$parent_ids))
    ->addAttributeToSelect('sku');
$parent_skus = $parent_collection->getColumnValues('sku');

这篇关于从子SKU获取父SKU(可配置或捆绑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆