In our previous article, we have seen how to display all the Magento orders externally (Not within Magento itself) in php.
Now, let's see a piece of code which applies same logic and show all products. Let's check out the code.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
// SEARCH MODE
if($_GET['q'] && trim($_GET['q']) !="")
{
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array('attribute'=> 'name',
'like' => '%'.trim($_GET['q']).'%'
),
array('attribute'=> 'sku',
'like' => '%'.trim($_GET['q']).'%'
),
))
->load();
}
else
/// Normal Procedure
{
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort('name', 'ASC')
->load();
}
?>
The above code includes the Search handling part also. The condition if($_GET['q'] && trim($_GET['q']) !="") evaluates to true when user enters some keyword and hits Search button.
->addAttributeToFilter( array(
array('attribute'=> 'name',
'like' => '%'.trim($_GET['q']).'%'
),
array('attribute'=> 'sku',
'like' => '%'.trim($_GET['q']).'%'
),
))
Here, the addAttributeToFilter() generates an SQL like this
WHERE 'name' LIKE '%search%' OR 'sku' LIKE '%search%'
Next, we just need to iterate through the product collection $collection.
<table width="100%" border="0">
<tr>
<td colspan="5" align="center">
<div>
<form method="get">
<input type="text" value="<?php echo $_GET['q'];?>" name="q">
<button title="Search" type="submit">Search</button>
</form>
</div>
</td>
</tr>
<tr class="header">
<th width=""><strong>ID</strong></th>
<th width=""><strong>Name</strong></th>
<th width=""><strong>SKU</strong></th>
<th width=""><strong>Price</strong></th>
<th width=""><strong>Final Price</strong></th>
</tr>
<?php
// LOOP the Collection
foreach ($collection as $_product)
{
$productID = $_product->getId();
$_product = Mage::getModel('catalog/product')
->load($productID);
$productPosition = 0;
// Product is Enabled and VISIBILITY = Search, Catalog
if($_product->getStatus()==1 && $_product->getVisibility()==4)
{
// GET FINAL PRICE aftyer applying RULE
$finalPrice = Mage::getModel('catalogrule/rule')
->calcProductPriceRule( $_product,
$_product->getPrice() );
$_specialPrice = $_product->getFinalPrice();
if($_specialPrice<=$finalPrice)
{
$finalPrice = $_specialPrice;
}
if($finalPrice)
{
$finalPrice = Mage::helper('core')->currency(
$finalPrice, true, false);
}
else
{
$finalPrice = Mage::helper('core')->currency(
$_product->getFinalPrice(), true, false);
}
echo "<tr>";
echo "<td>".$productID."</td>";
echo "<td><a href='" .
$_product->getProductUrl() .
"'>" . $_product->getName() . "</a></td>";
echo "<td>".$_product->getSku()."</td>";
echo "<td>".Mage::helper('core')->currency(
$_product->getPrice(), true, false). "</td>";
echo "<td>".$finalPrice."</td>";
echo "</tr>";
}
}
?>
</table>
We are showing all the products which have Visibility 4 (i.e "Catalog, Search"). Also the product's final price is the price we get after applying any Rules.
Check the screenshot below.
Hope this helps.
Now, let's see a piece of code which applies same logic and show all products. Let's check out the code.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
// SEARCH MODE
if($_GET['q'] && trim($_GET['q']) !="")
{
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array('attribute'=> 'name',
'like' => '%'.trim($_GET['q']).'%'
),
array('attribute'=> 'sku',
'like' => '%'.trim($_GET['q']).'%'
),
))
->load();
}
else
/// Normal Procedure
{
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort('name', 'ASC')
->load();
}
?>
The above code includes the Search handling part also. The condition if($_GET['q'] && trim($_GET['q']) !="") evaluates to true when user enters some keyword and hits Search button.
->addAttributeToFilter( array(
array('attribute'=> 'name',
'like' => '%'.trim($_GET['q']).'%'
),
array('attribute'=> 'sku',
'like' => '%'.trim($_GET['q']).'%'
),
))
Here, the addAttributeToFilter() generates an SQL like this
WHERE 'name' LIKE '%search%' OR 'sku' LIKE '%search%'
Next, we just need to iterate through the product collection $collection.
<table width="100%" border="0">
<tr>
<td colspan="5" align="center">
<div>
<form method="get">
<input type="text" value="<?php echo $_GET['q'];?>" name="q">
<button title="Search" type="submit">Search</button>
</form>
</div>
</td>
</tr>
<tr class="header">
<th width=""><strong>ID</strong></th>
<th width=""><strong>Name</strong></th>
<th width=""><strong>SKU</strong></th>
<th width=""><strong>Price</strong></th>
<th width=""><strong>Final Price</strong></th>
</tr>
<?php
// LOOP the Collection
foreach ($collection as $_product)
{
$productID = $_product->getId();
$_product = Mage::getModel('catalog/product')
->load($productID);
$productPosition = 0;
// Product is Enabled and VISIBILITY = Search, Catalog
if($_product->getStatus()==1 && $_product->getVisibility()==4)
{
// GET FINAL PRICE aftyer applying RULE
$finalPrice = Mage::getModel('catalogrule/rule')
->calcProductPriceRule( $_product,
$_product->getPrice() );
$_specialPrice = $_product->getFinalPrice();
if($_specialPrice<=$finalPrice)
{
$finalPrice = $_specialPrice;
}
if($finalPrice)
{
$finalPrice = Mage::helper('core')->currency(
$finalPrice, true, false);
}
else
{
$finalPrice = Mage::helper('core')->currency(
$_product->getFinalPrice(), true, false);
}
echo "<tr>";
echo "<td>".$productID."</td>";
echo "<td><a href='" .
$_product->getProductUrl() .
"'>" . $_product->getName() . "</a></td>";
echo "<td>".$_product->getSku()."</td>";
echo "<td>".Mage::helper('core')->currency(
$_product->getPrice(), true, false). "</td>";
echo "<td>".$finalPrice."</td>";
echo "</tr>";
}
}
?>
</table>
We are showing all the products which have Visibility 4 (i.e "Catalog, Search"). Also the product's final price is the price we get after applying any Rules.
Check the screenshot below.
Hope this helps.