Tag Archives: Magento tutorials online

Magento 2 get Product Collection


Magento 2 get Product Collection : In magento 1.x we get product collection using the Mage::getModel(‘catalog/product’)->getCollection(); but in magento 2.0 it is different to get the product collection. Here in this tutorial we are going to explain how to get product collection in magento 2.


Magento 2 get Product Collection

You can get product collection in magento 2.0 as below-

Magento 2 get Product Collection:

create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $prodCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('sku',$sku);
             // or ->load($productId);
foreach ($collection as $product){
     echo 'Name =  '.$product->getName().'
'; echo 'Id = '.$product->getId().'
'; echo 'Sku = '.$product->getSku().'
'; } ?>

The above example will give the product collection of by the sku or you can simply load the collection by product id simply using the ->load($productId) method.

Magento2 – Magento 2 load product by id


Magento2 – Magento 2 load product by id : In magento 1.x we use Mage::getModel(‘catalog/product’) to load the product but in magento 2.0 it is different than this. Here in this tutorial we are going to explain how you can load the product by product id.


Magento2 – Magento 2 load product by id

You can load the product by product id simply as below –

Magento2 – Magento 2 load product by id :

$productId = 100;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);

The example loads the product object from product id.

Magento category ID from product ID


Magento category ID from product ID : Sometimes we need to get the category id from product id. Mage::registry(‘current_product’); is used to get the category of current product ie. selected product. Here in this tutorial we are going to explain the methods how you can get category id from product id.


Magento category ID from product ID

First load the product using the catalog product model then check if product is loaded and get category ids as below –

:

$productId = 100;
$product = Mage::getModel('catalog/product')->load($productId);
if ($product->getId()) {
        $categoryIds = $product->getCategoryIds();
        if (is_array($categoryIds) and count($categoryIds) >= 1) {
            $cat = Mage::getModel('catalog/category')->load($categoryIds[0]);
			echo $cat->getId()."
"; }else{ echo "No Category Found."; } }

The above example will give the category ids of the selected product as above.

Magento check if admin user logged in or not


Magento check if admin user logged in or not : While working with magento we sometimes need to decide whether user is logged from front end or admin. Mage::getSingleton(‘core/session’, array(‘name’ => ‘adminhtml’)); is used to get the admin end logged in user session. Here in this tutorial we are going to explain how you can check the customer login on magento backend ie admin.


Magento check if admin user logged in or not

You can check the customer is logged in simply as below –

Magento check if admin user logged in or not:


Mage::getSingleton('core/session', array('name' => 'adminhtml'));

$userSession = Mage::getSingleton("customer/session");

if($userSession->isLoggedIn()) {
  echo "Is Logged In from Admin";
} else {
   echo "User Not Logged on Admin";
}

Using the above method you can check that the user is logged in from admin or not.

Magento check if customer is logged in


Magento check if customer is logged in : While working with magento we sometimes need to decide whether user is logged from front end or not. Mage::getSingleton(‘core/session’, array(‘name’ => ‘frontend’)); is used to get the front end logged in user session in Magento 1.x but In magento 2.x object manager $objectManager->get(‘Magento\Customer\Model\Session’) is used to get the customer session. Here in this tutorial we are going to explain how you can check the customer login on magento 1 and magento 2 front end.


Magento check if customer is logged in | Magento 2

It is very simple to check the looged in customer in magento 2, using the below method you can check whether the customer is logged in or not anywhere in the application. We have used object manager to get the customer session.

Magento 2 check if frontend user is logged in:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer logged in
}else{
// customer not logged in.
}

Magento 1.x


In magento 1 you can check the customer is logged in simply as below –

Magento check if frontend user is logged in:


Mage::getSingleton('core/session', array('name' => 'frontend'));

$customerSession = Mage::getSingleton("customer/session");

if($customerSession->isLoggedIn()) {
  echo "Is Logged In";
} else {
   echo "User Not Logged";
}

Using the above method you can check that the user is logged in from front end or not.


Magento Get Payment Information


Magento Get Payment Information : You can use order model Mage::getModel(‘sales/order’) to fetch the payment information. Using this model you can get the details about the payment method which was used while creating the order. Here in this tutorial we are going to explain how you can get the payment detail using the sales order model.


Magento Get Payment Information

You can get the payment instance from the order object as below –

Magento Get Payment Information:

 $orderId = 111;
 $order = Mage::getModel('sales/order')->load($orderId);
$payment = $order->getPayment();

Which will give you the payment instance of the object.

More Details About Payment

Let us have more details about the payment.

Magento get credit card type : Payment

You can get the credit card type as below-

Magento get credit card type : Payment:

$orderId = 111;
$order = Mage::getModel('sales/order')->load($orderId);
$payment = $order->getPayment();
$creditCardType = $payment->getData('cc_type');

This will give you the credit card type used in order.

Magento get payment method code from Order

You can get the payment method code as below-

Magento get payment method code from Order

$orderId = 111;
$order = Mage::getModel('sales/order')->load($orderId);
$payment = $order->getPayment();
$paymentMethodCode = $payment->getMethodInstance()->getCode();

This will give you the payment method code used in order.

Magento get payment method Title from Order

You can get the payment method title as below-

Magento get payment method title from Order

$orderId = 111;
$order = Mage::getModel('sales/order')->load($orderId);
$payment = $order->getPayment();
$paymentMethodTitle = $payment->getMethodInstance()->getTitle();

This will give you the payment method title used in order.

Magento get credit card information

You can get credit card information as below-

Magento get credit card information

$orderId = 111;
$order = Mage::getModel('sales/order')->load($orderId);
$payment = $order->getPayment();
$payment->getMethodInstance()->getCardsStorage();
$payment->getMethodInstance()->getCardsStorage()->getCards();

This will give you the credit card information.

Magento Collection Filter


Magento Collection Filter : Magento collection is basically model type which contains the others models. Collections are used for loading data from data. You can join tables, add filter to them. You can perform joinInner(), joinLeft(), joinRight(), joinFull(), joinCross(),joinNatural() using collections.Varien_Data_Collection is used for collections in magento. Here in this tutorial we are going to explain how to use collection for various purpose efficiently. We will cover collection with syntax, example and uses.


Magento Collection Filter

Here are the details about the magento collection-

Magento get Collection

Magento Get Custom Collection Example:

$collection = Mage::getModel('ModuleName/ModelName')->getCollection();

Where ModuleName – is name of module and ModelName is name of model you want to load.

Note : We are going to explain each method with model Mage::getModel(‘catalog/product’).

Magento collection addfieldtofilter

Here a simple example to add the filter to the field –

Magento collection addfieldtofilter:

$sku = 'testsku';
$collection = Mage::getModel('catalog/product')->getCollection()
                                               ->addFieldToFilter('sku', $sku);
$data = $collection->getData();
print_r($data);

When you will run the above example it will give you the product detail. The above example will produce the following query –

SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` = ‘testsku’)

Magento collection addAttributeToFilter

Here a simple example to add the filter to the field –

magento collection add attribute to filter:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status', 1); 
foreach($collection as $product){
 echo $product->getId()."
"; echo $product->getSku()."
"; }

The example will give you all products where status is 1 ie. they are enabled.

Magento addAttributeToFilter Conditions

Let have look over other conditions of addAttributeToFilter in magento.

Magento addAttributeToFilter Equal : eq (=)

Here is an example to add the equal condition in magento collection.

Magento addAttributeToFilter Equal Condition:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status', array('eq' => 1)); // With operator
$collection->addAttributeToFilter('status', 1); // Without operator

The above example will give the results where status = 1;

Magento addAttributeToFilter Not Equal : neq (!=)

Here is an example to add the not equal condition in magento collection.

Magento add Attribute To Filter Not Equal Condition:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status', array('neq' => 1)); // With operator

The above example will give the results where status != 1;

Magento addAttributeToFilter Like : like(%%)

Here is an example to add the like filter in magento collection.

Magento add Attribute To Filter like condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('sku', array('like' => '%abc%')); // With operator

The above example is used for magento collection like filter query. This example will give the results which fulfills the like condition.

Magento addAttributeToFilter Not Like : nlike(%%)

Here is an example to add the not like filter in magento collection.

Magento add Attribute To Filter Not like condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('sku', array('nlike' => '%abc%')); // With operator

The above example is used for magento collection Not like filter query. This example will give the results which fulfills the not like condition.

Magento addAttributeToFilter In : in

Magento collection in clause can be generated simply as below-

Magento add Attribute To Filter in condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('in' => array('1','2','3'))); // With operator

The above magento collection in clause will give you the results where id is equal to 1, 2 and 3.

Magento addAttributeToFilter Not In : nin

Magento collection not in clause can be generated simply as below-

Magento add Attribute To Filter not in condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('nin' => array('1','2','3'))); // With operator

The above magento collection not in clause will give you the results where id is not equal to 1, 2 and 3.

Magento addAttributeToFilter NUll : null

Magento collection null clause can be generated simply as below-

Magento add Attribute To Filter null condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('short_description', array('null' => true)); // With operator

The above magento collection null clause will give you the results short_description is null.

Magento addAttributeToFilter NOT NUll : notnull

Magento collection not null clause can be generated simply as below-

Magento add Attribute To Filter not null condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('short_description', array('notnull' => true)); // With operator

The above magento collection not null clause will give you the results short_description is not null.

Magento addAttributeToFilter Less Than : lt(<)

Magento collection less than clause can be generated simply as below-

Magento addAttributeToFilter Less Than : lt condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('lt' => 100)); // With operator

The above magento collection less than clause will give you the results where id < 100.

Magento addAttributeToFilter Less Than Or Equals To : lteg(<=)

Magento collection less than or equals to clause can be generated simply as below-

Magento addAttributeToFilter Less Than Or Equals to : lteq condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('lteq' => 100)); // With operator

The above magento collection less than or equals to clause will give you the results where id <= 100.

Magento addAttributeToFilter Greater Than : gt(>)

Magento collection greater than clause can be generated simply as below-

Magento addAttributeToFilter greater Than : gt condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('gt' => 100)); // With operator

The above magento collection greater than clause will give you the results where id > 100.

Magento addAttributeToFilter greater Than Or Equals To : gteg(>=)

Magento collection greater than or equals to clause can be generated simply as below-

Magento addAttributeToFilter greater Than equals to : gteq condition example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('id', array('gteq' => 100)); // With operator

The above magento collection greater than or equals to clause will give you the results where id >= 100.

Magento set frontend session time out limit


Magento set frontend session time out limit : To set session time is very simple. You can set the session timeout from magento admin configuration settings. You do not need to add anything at code level. Here we are going to explain the steps how you can set the session time out in magento.


Magento set frontend session time out limit

You can change the magento session simply by changing the following configuration –

Go to – System->Configuration->Advanced->Admin->Security

You will see the following settings –

Magento Set Session TimeOut for front end

Android Connect to Magento


Android Connect to Magento : While creating android application of magento we first need to understand how to connect with magento and how to pull the data from the magento using magento api. Here in this tutorial we are going to give the brief introduction how to connection with magento from andoid or ios using the soap or rest api.


Android Connect to Magento

ios

Magento provides the SOAP and REST api for application development. Here are main points which needs to start with android or ios app of magento.

1.SOAP API – http://www.magentocommerce.com/api/soap/introduction.html

2.REST API – http://www.magentocommerce.com/api/rest/introduction.html

3. Now create a New User in your Magento Store with roles. Login to magento Admin and go to system->WebServices->User&Roles.

4. Here create role and a user. Then assign the roles to this User.
Magento connect to android soap rest example

Now you can call the soap method as in example below –

Android Connect to Magento Soap Example:

$client = new SoapClient('http://mydomain.com/soap/api/?wsdl');

$session = $client->login('apiUser', 'apiKey');
// get both from apiUser detail in magento.
$params = array(array(
    'status'=>array('eq'=>'pending')
));
$result = $client->call($session, 'sales_order.list', $params);

The above example will give you the sales order data.