Category Archives: Magento
Magento get all disabled Products
Magento get all disabled Products : You can get Disabled inactive products using the Mage::getModel(‘catalog/product’) Model. You can add filters to get the inactive disabled products. Here we are going to explain the collection filter to get the disabled/inactive products.
Magento get all disabled Products
Here is syntax to get the disabled product in magento.
Magento get all disabled Products:
$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*') ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_DISABLED); |
The above Filter will return all disabled products in magento.
Magento Get Product Type
Magento Get Product Type: There are various types of products in Magento and each has some different functions. Each has its own meaning and uses so it is very important to understand them. Here in this tutorial first we are going to describe the types of products in magento and then we will cover how to get the products type using product id or sku etc.
Magento Get Product Type – Magento 2 Product Type
Here are following types of products let us have an overview –
-
Simple products in Magento :
This is most common used product in magento. Simple products are basically used for single item without any selectable variations. Example – Chair, Table
-
Grouped products In Magento :
Magnto grouped products are combination of simple products. If in the above example Chair & Tables are combined and sell together it will come in grouped products type.
-
Configurable Products in Magento :
Configurable Products are products which can be configured on the basis of various parameters. For example – Tee Shirt in Different Colors and Sizes.
-
Virtual Products In Magento :
Virtual Products in magento are products which does not exist in real ie. they are not touchable. Shipping method is not used for these products because there is nothing to ship in real. The simple example can be – Reservation for movie ticket.
-
Bundle Products in Magento :
Bundle products are basically bundle of simple products which are sold together not separately. Example – Mobile which is sold with accessories such as charger & headphone.
-
Downloadable products :
Products which can be downloaded online. These products also does not need shipping method as they are downloaded directly.
Magento 2 get product type from Product id
You check the product type in magento2 same as in below example. Here is syntax to get the product type from product id in Magento 2.
Magento 2 Check Product Type
$productId = 100; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId); $productType = $product->getTypeID(); if($productType == 'simple') { echo "Simple Product"; } if($productType == 'configurable') { echo "Configurable Product"; } |
Magento2 Get Product Type : $product->getTypeID() is used for getting the type id from product object in magento. The above example will give the product type in magento.
Magento 1.x get product type from Product id
Here is syntax to get the product type from product id in Magento 1.x.
Magento Get Product Type
$productId = 1222; $product = Mage::getModel('catalog/product')->load($productId); $productType = $product->getTypeID(); if($productType == 'simple') { echo "Simple Product"; } if($productType == 'configurable') { echo "Configurable Product"; } |
More Example
Let us have another example –
Get product type bundle / Grouped / Virtual / Downloadable
Here is syntax to get the bundle / Grouped / Virtual / Downloadable
Magento get product type bundle / Grouped / Virtual / Downloadable:
/****** for magento 1.x*****/ //$product = Mage::getModel('catalog/product')->load($productId); /****** for magento 2.x*****/ $productId = 100; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId); $productType = $product->getTypeID(); if($productType == 'bundle') { echo "Gundle Product"; } if($productType == 'grouped') { echo "Grouped Product"; } if($productType == 'Downloadable') { echo "Downloadable Product"; } if($productType == 'virtual') { echo "Virtual Product"; } |
Thus using the above example you can get product type Details In Magento Version 2.x And 1.x.
Magento get ip address
Magento get ip address : Magento Core Http Helper (Mage::helper(‘core/http’)->getRemoteAddr();) class is used to get the current Ip Address in magento. You can get visitors detail using this helper. Magento Core Http Helper class provides useful functions which can be used frequently. In this tutorial we are going to explain the Magento Core Http Helper.
Magento get ip address
Here is syntax to get the ip address of the visitors in magento –
Magento get Remote ip address
Magento get ip address of Visitor
$visitorsIpAddress = Mage::helper('core/http')->getRemoteAddr(); |
The above syntax will give you the visitors ip address in magento. Let us go with other options available in Magento Core Http Helper.
Magento get Server Ip
You Can get Server Ip in magento as below-
Magento get ip address of Visitor
$visitorsServerIp = Mage::helper('core/http')->getServerAddr(); |
Mage::helper(‘core/http’)->getServerAddr(); isused to retrieve server IP address.
Magento Validate Ip Address
You Can Validate Ip Address in Magento as below –
Magento Validate Ip Address
Mage::helper('core/http')->validateIpAddr($ipAddress); |
Mage::helper(‘core/http’)->validateIpAddr(); is used to validate ip address. It returns false if ip address is not valid.
Magento get http referrer
You Can get http referrer in Magento as below –
Magento get http referrer
Mage::helper('core/http')->getHttpReferer($clean = true); |
Mage::helper(‘core/http’)->getHttpReferer(); is used to get http referer in magento.
More Functions In Core Http Helper
Here list of other functions available in Core Http Helper.
Magento Core Http Functions
Mage::helper('core/http')->getHttpHost($clean = true); Mage::helper('core/http')->getHttpUserAgent($clean = true); Mage::helper('core/http')->getHttpAcceptLanguage($clean = true); Mage::helper('core/http')->getHttpAcceptCharset($clean = true); |
You can use the above core http helper to get the details listed above.
Magento get best selling products Collection
Magento get best selling products Collection : Here simple and quick way to get the best selling products in magento. Mage::getResourceModel(‘reports/product_collection’) is used to get the most ordered items on the basis of ordered quantities. In this tutorial we are going to create simple collection which will give the most ordered ie. best selling products.
Magento get best selling products Collection
Here is syntax to get the collection of best selling products-
Magento get best selling products Collection
$storeId = Mage::app()->getStore()->getId(); $productsCollection = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->setStoreId($storeId) ->addStoreFilter($storeId) ->setOrder('ordered_qty', 'desc')//This tells the most ordered products ->setPageSize(50); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($productsCollection); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($productsCollection); foreach($productsCollection as $product){ $productIds[] = $product->getId(); } |
The above example will give you the top 50 most selling products in magento. You can add your own filter and page size to get the collection of the product. Hope this will help you!
Magento get Most Viewed Products
Magento get Most Viewed Products : You can get most viewed products from the resource model reports/product_collection. Using this model you can get the product ids which has been viewed most among the all products. Using the product ids you can load other details of the product using the catalog product model. Here we are going to explain the simple collection to get most viewed products in magento.
Magento get Most Viewed Products
Mage::getResourceModel(‘reports/product_collection’)->addViewsCount() Collection is used to get the most viewed items. Here is example to get the ids of these products-
Magento get Most Viewed Products:
$mostViewedProd = Mage::getResourceModel('reports/product_collection') ->addViewsCount() ->setPageSize(10); $productIds = array(); foreach ($mostViewedProd as $product) { $productIds[] = $product->getId(); } |
You can set the page size and other filters you want. The above example will give you top 10 most viewed products. For more details you can load product details using Mage::getModel(‘catalog/product’). and ids of products
Magento get discounted Price of Product
Magento get discounted Price of Product : You can get Discounted price of product in magento from product object. You can load product by catalog product model. Here is simple syntax to get the discounted price of product.
Magento get discounted Price of Product
Syntax to get discounted price of an item is given below-
Magento get discounted Price of Product
//load product by product id $product=Mage::getModel('catalog/product')->load($productId); $discountedPrice = number_format($product->getFinalPrice(), 2); |
Magento Get Attribute Code By Id
Magento Get Attribute Code By Id : You can get Attribute code by using attribute id. First load the product by product id and then get the attribute object by product resource. $product->getResource()->getAttribute(‘my_attribute’);. Here we are going to explain this with simple example.
Magento Get Attribute Code By Id
Here is syntax to get the attribute code by id
Magento Get Attribute Code By Id
// use this to get attrId //$product=Mage::getModel('catalog/product')->load($productId); //$attr = $product->getResource()->getAttribute('my_attribute'); //$attrId = $attr->getAttributeId(); /***get attribute code if you know attribute id. $attrCode = Mage::getModel('eav/entity_attribute')->load($attrId)->getAttributeCode(); |
The above example will give the attribute code.
Get custom attribute label in Magento
Get custom attribute label in Magento : You can get custom attribute label in magento by loading the product by product id and using the syntax – $product->getResource()->getAttribute(‘my_attribute’)->getStoreLabel(); Here we are going to explain this with simple example and demo.
Get custom attribute label in Magento
You can get the custom attribute in magento simply using the catalog/product model and its resource. Here is simple example how to get custom attribute label in magento –
Get custom attribute label in Magento
$product=Mage::getModel('catalog/product')->load($productId); $attrLabel = $product->getResource()->getAttribute('my_attribute')->getStoreLabel(); |
Get list all bundle products in Magento
Get list all bundle products in Magento : In magento there can be many types of products such as simple, configurable and bundled etc. You can user magento model catalog product model and filters to get the bundle products. You can get other types of products in the same way. Here we are going to explain how to get bundle products in magento.
Get list all bundle products in Magento
Here is syntax for getting bundle products-
Get list all bundled products in Magento Syntax
$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*') ->addAttributeToSelect('type') ->addAttributeToFilter('type_id', 'bundle'); |
Which will give you the bundle products.
Get bundle product items from Bundle Product in Magento
You can get the bundle product items simple using the following syntax –
Get bundle product items from Bundle Product in Magento Syntax
//first load bundle product $bundleProduct = Mage::getModel('catalog/product')->load($bundleProductId); $collection = $product->getTypeInstance(true) ->getSelectionsCollection( $product->getTypeInstance(true) ->getOptionsIds($bundleProduct), $bundleProduct); foreach($collection as $item) { $itemId = $item->product_id; } |
The above example will give you items of the bundle product.