cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the array of size and quantity as a value in magento from product collection?

How to get the array of size and quantity as a value in magento from product collection?

I am quite a new to magento, but trying hard to learn it and know all it models and collections.
I had a requirement to get the size of all the simple products and its quantity. We do have the size attribute set and many product are of type configurable and we are displaying that on the list page as well.

Configurable product do have the quantity and size as it is associates all the simple products.

Now i am really curious to know the stock of the product with their sizes in this format.

stock ={
"size" => "quantity"
}

where size is the attribute and it can be x,xl,xs,xxl,xxxl and so on. and quantity is the quantity which is in stock for that particular size.

What i have tried uptill now is to get the name,id,sku,product url, images url and category id as well.

 

<?php
require __DIR__ . '/app/Mage.php';
umask(0);
Mage::app();

$collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*'); // set the offset (useful for pagination) // we iterate through the list of products to get attribute values foreach ($collection as $product) { echo $product->getName(); //get name of the product echo "<br/>"; echo (float) $product->getPrice(); //get price as cast to float echo "<br/>"; echo $product->getDescription(); //get description echo "<br/>"; echo $product->getSku(); //get sku echo "<br/>"; echo (int)Mage::getModel('cataloginventory/stock_item') ->loadByProduct($product)->getQty(); //get the qty of the product echo "<br/>"; // getCategoryIds(); returns an array of category IDs associated with the product foreach ($product->getCategoryIds() as $category_id) { $category = Mage::getModel('catalog/category')->load($category_id); echo "<br/>"; echo $category->getName(); //get name of the category && sub-category echo "<br/>"; } echo '<br/>'; //gets the image url of the product //var_dump($product->getMediaGalleryImages()); echo "<br/>"; echo $product->getProductUrl(); //gets the product url echo '<br />'; echo $product->getSize(); echo '<br />'; $product1 = Mage::getModel('catalog/product')->load($product->getID())->getMediaGalleryImages();//product $items = array(); foreach($product1 as $gimage) { $items[] = $gimage['url']; } $t = implode(',', $items); echo $t; echo "</br>"; }

 

Now, how will i be able to fetch the quantity and size in the format mentioned above foreach product using the above collection ??

 

Please ,anyone?

1 REPLY

Re: How to get the array of size and quantity as a value in magento from product collection?

To my understanding, You would like to retrieve the size attribute with quantity of simple products associated to the configurable products. Check the below code for retrieving size with stock informations.

 

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();

// Loading all configurable products
$_configurablecollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));

foreach ($_configurablecollection as $_configurableproduct) {

	echo $product->getName(); //get name of the product
	echo "<br/>";
	echo (float) $product->getPrice(); //get price as cast to float
	echo "<br/>";
	echo $product->getDescription(); //get description
	echo "<br/>";

	echo $product->getSku(); //get sku
	echo "<br/>";

	// Similarly print category names and image urls

	$stock = array();
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

    // Get Simple products associated to the configurable product
    $ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($_configurableproduct->getId()); 
    foreach ($ids as $_id) {  	
		$_subproduct = Mage::getModel('catalog/product')->load($_id);
		// Get Size Label
		$_sizelabel = $_subproduct->getSize();
		// Get Quantity
		$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
                ->loadByProduct($_subproduct)->getQty();
        $stock[$_sizelabel] = $stocklevel;
    }
    print_r($stock);

}

 

Hope it helps Smiley Happy