Tuesday 1 July 2014

Magento Get the parent of a simple product

This tutorial will guide you on how to get parent product id for simple product in magento. There are several ways we can achieve this.

$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
->getParentIdsByChild($product->getId());
if (isset($parentIds[0])) {
    // If we find a parent product then we display it's ID
    $parentProduct = Mage::getModel('catalog/product')->load($parentIds[0]);
    echo $parentProduct->getId();
}


OR

$simpleProductId = 102;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
                  ->getParentIdsByChild($simpleProductId);
$product = Mage::getModel('catalog/product')->load($parentIds[0]);
echo $product->getId();


Best of luck

Magento Get dropdown attribute’s option Id or Lable or Value

This tutorial will guide you on how to get drop down attribute value or label or id

Get attribute id using label.
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color_group");
if ($attr->usesSource()) {
    echo $attr->getSource()->getOptionId("Green");
}


Get attribute label using id.
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color_group");
if ($attr->usesSource()) {
    echo $attr->getSource()->getOptionText("723");
}



Get all options by attribute Code.
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product','color_group');

if ($attributeId->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}


Get all options by attribute Id.
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load(723);
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}


Best of luck

Magento How to get child product collection of configurable product

A configurable product can have multiple other products associated to it.This tutorial describe How can get all the simple products associated with a configurable product.

Get all product id of all simple products associated with a configurable product.
$AssociatedProducts = Mage::getModel('catalog/product_type_configurable')
->getChildrenIds($_product->getId());

Get collection of all the simple products associated with a configurable product.
$product = Mage::getModel('catalog/product')->load($product_id);
$childProducts = Mage::getModel('catalog/product_type_configurable')->

getUsedProducts(null,$product);

Best of luck

Magento Get a product collection by specific ids

This tutorial describe How can get product collection using specific product id.
Here is the code to fetch AND filter with custom option using specific product id also you can use to get simple products associated with a configurable product.

$productIds= Mage::getModel('catalog/product_type_configurable')
->getChildrenIds($_product->getId());

$productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('sceneseven_thumbnail_image')
                ->addAttributeToFilter('entity_id', array('in' => $productIds))
                ->addAttributeToFilter('color_group',$colorFilter)
                ->addAttributeToSort('color', 'ASC');


Best of luck