Ultimate Magento 1 Developer Guide : How to get product details using rest API in Magento 1.x ?

Future is mobile apps. Especially, eCommerce Mobile Apps. Sooner or later you’ll run into a case where you need REST API for your online store and you found that Magento does not support all Magento REST API. What should you do now? How can you write REST API for your online store as per client need?

The Magento REST API allows you to use various data entities such as customer, catalog and order information to third-party applications. I’d like to share it with you a quick, simple, well written and easy solution which will explain how can you retrieve the product details with the help of REST API.

You are working on a mobile application and want to display product after customer selected a category. You are able to get the list of the product by category in REST API request but that list doesn’t have many details about the product and you want to show more product details to the users. What should you do to get more product information so that you can show the image and other things in a mobile app?

Magento Rest API for Product Details by SKU

Step 1: Get the SKU which you need to get the product details.

      $sku  =  $this->getRequest()->getParam('SKU');

Step 2: Load the product using the product SKU. We can also load product by its attributes, like SKU, Id. Assuming product SKU to be ‘1000’.

    
       // Mage main object
	$catalogMageObj = Mage::getModel('catalog/product');
	$products = $catalogMageObj->loadByAttribute('sku', $sku);

Step 3: load the product custom attributes details.

	$custom_attributes = array();
	$attributes = $products->getAttributes();
	foreach ($attributes as $attribute) {
		if ($attribute->getAttributeCode() =="category_ids"){
			$custom_attributes[] = array(
				"attribute_code"=> $attribute->getAttributeCode(),
				"value"=> $products->getCategoryIds()
			);
		}
		else{
			$custom_attributes[] = array(
				"attribute_code"=> $attribute->getAttributeCode(),
				"value"=> $attribute->getFrontend()->getValue($products)
			);
		}
	}

Step 4: Create and return the full array of the loaded product.

	
protected function getProductDetailsBySku()
{
        $sku  =  $this->getRequest()->getParam('sku');	

        // Mage main object
	$catalogMageObj = Mage::getModel('catalog/product');
	$products = $catalogMageObj->loadByAttribute('sku', $sku);

	$custom_attributes = array();
	$attributes = $products->getAttributes();
	foreach ($attributes as $attribute) {
		if ($attribute->getAttributeCode() =="category_ids"){
			$custom_attributes[] = array(
				"attribute_code"=> $attribute->getAttributeCode(),
				"value"=> $products->getCategoryIds()
			);
		}
		else{
			$custom_attributes[] = array(
				"attribute_code"=> $attribute->getAttributeCode(),
				"value"=> $attribute->getFrontend()->getValue($products)
			);
		}
	}
				
	$data = array(
		'id' => $products->getEntityId(),
		'sku' => $products->getSku(),
		'name' => $products->getName(),
		'attribute_set_id' => (int)$products->getAttributeSetId(),
		'price' => $products->getPrice(),
		'status' => $products->getStatus(),
		'visibility' => $products->getVisibility(),
		'type_id' => $products->getTypeId(),
		'created_at' => $products->getCreatedAt(),
		'updated_at' => $products->getUpdatedAt(),
		'product_links' => $productstypes, 
		'custom_attributes'  => $custom_attributes
	);
		
	//return $products;
	return $data;
}

How to get Configured Product Extension Attributes

Getting all options of a specific attribute in Magento is fairly easy but sometimes A more challenging task is to get the options and values that are applicable to a particular configurable product. This can solve using the following code:

// get product extension attributes details
	public function getProductExtensionAttributes($productObj){
		
		if (!is_object($productObj)){
			return false;
		} 
		
		$extensionAttributesFinalArray = array();
		$extensionAttributesArray = array();
		if ($productObj->isConfigurable()){
			$optionsData = $productObj->getTypeInstance(true)->getConfigurableAttributesAsArray($productObj);
			foreach ($optionsData as $option) {
				// get option values
				$values =  $option['values'];
				$tempDataItems = array();
				foreach ($values as $value) {
					$tempData = array(
							"value_index" => $value['value_index'],
							"title" => $value['label']
					);
					$product_super_attribute_id =  $value['product_super_attribute_id'];
					array_push($tempDataItems, $tempData);
				}
				
				// create temp array for extension_attributes
				$extensionAttributesTempArray = array(
						"id" => $option['id'],
						"label" => $option['label'],
						"position" => $option['position'],
						"attribute_id" => $product_super_attribute_id,
						"values" => $tempDataItems,
						"product_id" => (int)$productObj->getEntityId(),
				);
				array_push($extensionAttributesFinalArray, $extensionAttributesTempArray);
			}
			$extensionAttributesArray['configurable_product_options'] = $extensionAttributesFinalArray;
			$extensionAttributesArray['configurable_product_links'] = [];
		}
		
		// returning response
		return $extensionAttributesArray;
	}

The resulting array will consist of an associative array, indexed by the attribute label, with nested arrays containing attribute value => attribute label items.

Conclusion

This is the era of eCommerce with the Mobile app as peoples are spending time on their mobile. The user has various choices to purchase an item from the web that’s why your eCommerce website should remain competitive in your business. The mobile app is a new way, which turns your Magento website into a fully functional eCommerce store with everything user needs.

On this tutorial, we have discussed how to get all the product details Magento which you can use to develop your Magento Mobile App.

References

Further reading

Leave a Comment

Scroll to Top