Magento : Custom Stock Status

In Magento, once I was working Product Detail page, and used to see, status of a Product “In Stock” or “Out of Stock” What I want was to modify these Stock status messages, and go through all of the admin but don’t get any column there to modify these messages, but later on I find out solution in below manner, Please check it out –

You should have some basic knowledge of using FTP –

Open your FTP, and go through the below given path –

/app/design/frontend/{your_package}/{your_theme}/template/catalog/product/view/type/availability/default.phtml

Here your_package is package used by your Magento setup, and your_theme is a theme assigned to the magento.

open file, here you will see that “In Stock” and “Out of Stock” are written in a different conditions, Just change these string according to you –

$_product = $this->getProduct(); ?>
<?php if ($this->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
        <p class="availability in-stock">
            <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span>
            <span class="value"><?php echo $this->helper('catalog')->__('In stock') ?></span>
        </p>
    <?php else: ?>
        <p class="availability out-of-stock">
            <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span>
            <span class="value"><?php echo $this->helper('catalog')->__('Out of stock') ?></span>
        </p>
    <?php endif; ?>
<?php endif; ?>

Magento : Product stock quantity

From here you can get any product’s stock  quantity information like quantity (qty), minimum quantity (min_qty), stock availability (is_in_stock), minimum and maximum sale quantity (min_sale_qty and max_sale_qty).

Load Product

For that you need to load product. There are two ways to load product

1. Load by product id

$id = 12; // put custom product_id
$_product = Mage::getModel('catalog/product')->load($id);

2. Load by Product Sku

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

 

Get Product stock information

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

You can check stock data in following way:-

echo "<pre>"; print_r($stock->getData()); echo "</pre>";

or

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Top selling product collection programmatically in magento

Top selling products :

Here is way to get best selling product collection.

<?php 
/* Best Seller*/

$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$storeId = Mage::app()->getStore()->getId(); // get default store id

$_productCollection = Mage::getResourceModel('reports/product_collection')
     ->addAttributeToSelect('*')
     ->addOrderedQty()
     ->addAttributeToFilter('visibility', $visibility)
     ->setOrder('ordered_qty', 'desc');

 print_r($_productCollection);
?>

Product price update globally in magento using direct sql query

Update product price globally
<?php

$priceToAdd = 10; // custom product price

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $priceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
");

// where eav.entity_type_id=4 is default attribute id

 

Update all prices from a certain store

This technique is useful when working in a multi-store Magento environment.

<?php

$priceToAdd = 6;
$storeId = 4;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $priceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
    AND val.store_id = $storeId
");

 

Update all product prices with a certain attribute set
<?php

$customPriceToAdd = 10;
$attributeSetId = 4;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $customPriceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
AND entity_id = ( 
   SELECT p.entity_id FROM catalog_product_entity p
   WHERE p.attribute_set_id = $attributeSetId
)
");

 

Update the Special Price
<?php

$custompriceToAdd = 10;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $custompriceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'special_price'
    )
");

 

Direct SQL Queries In Magento

Magento uses data models which provide a great way to access and modify data. By using competently named methods and clever abstraction, Varien hide away the complex SQL needed to perform data operations. While this makes learning models easier, it often impacts the speed of the operation and so the responsiveness of your website. This is true when saving models that use the EAV architecture.

Mostly, this cannot be avoided, however there are some situations where executing direct SQL queries would be simpler and much quicker leading to a more optimised Magento installation.

Example : Product price update globally in magento.

 

Database Connections In Magento

Magento will automatically connect to it’s database and provide two separate resources which you can use to access data: core_read and core_write.

<?php 
/** 
* Get the resource model 
*/ 

$resource = Mage::getSingleton('core/resource'); 

/** 
* Retrieve the read connection 
*/ 

$readConnection = $resource->getConnection('core_read');
	
/**
* Retrieve the write connection
*/
$writeConnection = $resource->getConnection('core_write');

?>

 

Table names and prefixes

A table prefix is a string of characters that is added to the start of every table name in your database. These are useful to installing multiple system into one database. It helps to distinguish each application’s data.

Magento has a simple built in function which allows you to add the prefix to a given table name.

Get a table name from a string
<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Get the table name
*/

$tableName = $resource->getTableName('catalog_product_entity');
	
/**
* if prefix was 'mage_' then the below statement
* would print out mage_catalog_product_entity
*/

echo $tableName;

?>

 

Get a table name from a string
<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Get the table name
*/

$tableName = $resource->getTableName('catalog/product');
	
/**
* if prefix was 'mage_' then the below statement
* would print out mage_catalog_product_entity
*/

echo $tableName;
?>

 

Reading From The Database

It would be much quicker to run a single query to obtain this value than to load in a whole product model .

Varien_Db_Select::fetchAll

This method takes a query as it’s parameter, executes it and then returns all of the results as an array.

<?php
	
/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');
	
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
	
/**
* Execute the query and store the results in $results
*/

$results = $readConnection->fetchAll($query);
	
/**
* Print out the results
*/

var_dump($results);

 

Varien_Db_Select::fetchCol

This method is similar to fetchAll except that instead of returning all of the results, it returns the first column from each result row.

<?php
/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');
	
/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');

/**
* Execute the query and store the results in $results
*/

$sku = $readConnection->fetchCol('SELECT sku FROM ' . $table . ');
	
/**
* Print out the results
*/

var_dump($results);

 

Varien_Db_Select::fetchOne

Varien_Db_Select::fetchOne returns one value from the first row only.

<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the read connection
*/

$readConnection = $resource->getConnection('core_read');

/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');
	
/**
* Set the product ID
*/

$productId = 44;
	
$query = 'SELECT sku FROM ' . $table . ' WHERE entity_id = '. (int)$productId . ' LIMIT 1';
	
/**
* Execute the query and store the result in $sku
*/

$sku = $readConnection->fetchOne($query);
	
/**
* Print the SKU to the screen
*/

echo 'SKU: ' . $sku . '<br/>';

 

Writing To The Database

<?php

/**
* Get the resource model
*/

$resource = Mage::getSingleton('core/resource');
	
/**
* Retrieve the write connection
*/

$writeConnection = $resource->getConnection('core_write');

/**
* Retrieve our table name
*/

$table = $resource->getTableName('catalog/product');
	
/**
* Set the product ID
*/

$productId = 44;
	
/**
* Set the new SKU
* It is assumed that you are hard coding the new SKU in
* If the input is not dynamic, consider using the
* Varien_Db_Select object to insert data
*/

$newSku = 'new-sku';
	
$query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = ". (int)$productId;
	
/**
* Execute the query
*/

$writeConnection->query($query);