Friday, May 17, 2013

Magento - Browse by Price Option in Left Navigation Using Custom Code

Here we mentioned about Browse By Price in the left nav. Price option incremented multiples of 1000

<?php
     
  

    $minPrice    =    Mage::getSingleton('catalog/layer')->setCurrentCategory(Mage::registry('current_category'))->getProductCollection()->getMinPrice();
    $maxPrice    =    Mage::getSingleton('catalog/layer')->setCurrentCategory(Mage::registry('current_category'))->getProductCollection()->getMaxPrice();
   
    if($maxPrice>0)
    {
    ?>
    <dl id="narrow-by-list">
      <dt>Browse By Price</dt>
      <dd>
        <ol class="m-filter-item-list ">
        <?php
            $incp=1;
            for($pi = $minPrice; $pi <= $maxPrice; $pi++)
            {
                if($incp>1)
                {
                    $minPrice    =    ($incp-1)*1000;
                    $pi         =    $incp*1000;
                    echo '<li><a href="'.$this->getUrl('catalogsearch/result/').'?q=a&price='.$incp.'%2C1000&search=cateinfo" title="$'.$minPrice.' - $'.$pi.'">$'.$minPrice.' - $'.$pi.'</a></li>';
                }
                else
                {       
                    $pi     =    $incp*1000;
                    echo '<li><a href="'.$this->getUrl('catalogsearch/result/').'?q=a&price=1%2C1000&search=cateinfo" title="$0 - $'.$pi.'">$0 - $'.$pi.'</a></li>';   
                }
                $incp++;
            }   
        ?>
       
         </ol>
      </dd>
    </dl>
        <hr/>
<?php }  ?>
   

By PHP with No comments

Magento - Check Current page is Category | DETERMINE IF CURRENT PAGE IS A CATEGORY

After a great deal of frustration and numerous tantrums I discovered the solution is to wrap the code in a loop that checks if it’s a category page.
//LOOP TO CHECK IF ITS A CATEGORY PAGE //INNER LOOP getId() == 5):?>
Shows this paragraph is the category id="5"
Else show this for different category pages
> //SHOW THIS CONTENT IF ITS A CMS PAGE
Else show this paragraph for all other pages
>
Posted in All, News, Tutorials and tagged eCommerce, Magento, Magento Category, Magento current_category, Mangento If Category.

========================

 Magento Determine if Current Page is Category or Not

Sometimes you find your self in the need to determine if the current page a visitor is viewing is of a category or not. The reason for this could drastically very, but one good reason is the need for a global element not be shown on any category page.
It would be much faster to simply include this element or rather custom block globally to your Magento store via your layout XML files, and have a condition in this block’s .phtml file that excluded any output on category page.
This is a very simple method that simply looks to our Mage registry and asks the question “Is the current page indeed a current category page or not?”.

<?php if(!Mage::registry('current_category')):?>
//OUTPUT THE GLOBAL ELEMENTS CODE ON NONE-CATEGORY PAGES
php endif; ?>
 
 
Simple as that, of course the correct way would be to include this block globally via your XML layout files, and then remove it via XML (Custom Layout Update) in the backend->catalog->manage categories “Custom Design” tab for each category.



============================

Get the current category in Magento

 

I’ve come across several places where I’ve needed to get the current category (or information from it) in one way or another. Some blocks have the ability to do this:
$_category = $this->getCurrentCategory();
However, if you need to get the current category in a block (or model, helper, whichever) where this method isn’t present, then we can simply do this instead:
$_category = Mage::registry('current_category');
They both return exactly the same object (unless overridden, that is Mage_Catalog_Model_Category).
Caveat: If you are in a block that doesn’t have access to ‘getCurrentCategory’ nor is Mage::registry(‘current_category’) defined (for example, the product list being applied to the home/front page) you can load by category ID if known. In this example, the Mage_Catalog_Block_Product_List has been applied to the front page, so you’d need to do this instead:
// Check for $_category's existance/status after initial block load, and grab the category ID from the object's parameters/attributes.
// Replace $this->getCategoryId() with a category ID, whether defined statically or retrieved from another method.
if (!is_object($_category))
     $_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
Of course, this ends up being slightly different depending on where you need to get the category object.

By PHP with No comments

Thursday, May 16, 2013

Magento - Get tier price with mini price display / Tier Lowest Price Display

Magento get tier price with minimum price display


<?php
$product_tier_prices = $this->getTierPrices($_product);
if(count($product_tier_prices) > 0){
$product_tier_prices = (object)$product_tier_prices;
$product_price = array();
foreach($product_tier_prices as $key=>$value){
$value = (object)$value;
$product_price[] = $value->price;
}
echo 'As low as: '.Mage::helper('core')->currency(min($product_price),true,false);
}
?>


  Method 2

 

 

A few weeks ago we were scratching our heads in confusion on how to get the lowest tier-price from magento. If you take a look at your catalog>product>view>tierprices.phtml you will see why – why does such a complicated section of code even exist?
Anyway after a few failed attempts we were contacted this morning by Christiaan who asked us this question a few weeks ago on the magento forums. We took this as a challenge and set to work this very morning sorting it out.
After a few black coffees we finally figured it out…or at least got rid of the rubbish that was concealing it. We now have a little method to bring back the lowest tier price on a magento product view page!
Here is how you do it:
1. Create a file in your product>view folder called getlowest.phtml
Copy & paste the following into it:
<?php
/**
* @E-Commercewebdesign.co.uk
*/
$_product = $this->getProduct();
$_tierPrices = $this->getTierPrices();
$_finalPriceInclTax = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice(), true);

$_weeeTaxAmount = Mage::helper('weee')->getAmountForDisplay($_product);
if (Mage::helper('weee')->typeOfDisplay($_product, array(1,2,4))) {
$_weeeTaxAttributes = Mage::helper('weee')->getProductWeeeAttributesForDisplay($_product);
}

?>
<?php if (count($_tierPrices) > 0): ?>
<?php if ($this->getInGrouped()): ?>
<?php $_tierPrices = $this->getTierPrices($_product); ?>
<?php endif; ?>
<?php Mage::helper('weee')->processTierPrices($_product, $_tierPrices); ?>
<?php $i = 0; ?>
<?php $_tierPrices = array_reverse($_tierPrices); ?>
<?php foreach ($_tierPrices as $_price): ?>
<?php if($i==0){ ?>
<p style="font-weight:bold; font-size: 1em;">After tier pricing the lowest price you can have a Cowboy Hat is...
<span style="font-size:150%; text-decoration:underline;">
<?php echo $_price['formated_price']; ?>
</span>
<p>
<?php $i++; ?>
<?php } ?>
<?php endforeach ?>
<?php endif;?>
As you can see you can edit some words above to output whatever you like around this lowest price – could be on top of an image or simply surrounded by text like above.
2. Open your themes catalog.xml and paste in the following code:
<block type="catalog/product_view" name="getlowest" as="getlowest" template="catalog/product/view/getlowest.phtml" />
3. Call this function on your product>view.phtml
<?php echo $this->getChildHtml('getlowest') ?>
There you have it! This code will output the lowest price of your tier prices dynamically – and will also not show anything if tier-prices are not activated for the product.

 

By PHP with 4 comments

Magento - Get Min and Max product price in current category / Display lowest / highest price from current product collection

Display minium / lowest product price from current category in magento. get min price from current product collection. Fine the below code to get min price value.


echo $minPrice=Mage::getSingleton('catalog/layer')
->setCurrentCategory(Mage::registry('current_category'))
->getProductCollection()
->getMinPrice();


Display maximum / Highest product price from current category in magento. get max price from current product collection. Fine the below code to get max price value.

echo $maxPrice=Mage::getSingleton('catalog/layer')
->setCurrentCategory(Mage::registry('current_category'))
->getProductCollection()
->getMaxPrice();

By PHP with No comments

Monday, May 13, 2013

Magento - Advanced Search Category Lists In Select Box

How to Place the Category Lists in advanced Search page., Search by category in magento.

n the advanced search page, “search by category” is not an option by default, but can be very helpful. To add this, you will need to edit the following files:
  • app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
  • app/code/core/Mage/CatalogSearch/Model/Advanced.php
  • app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml
At the very end of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php (before the closing brace), add:
  1.     public function getStoreCategories()
  2.     {
  3.         $helper = Mage::helper('catalog/category');
  4.         return $helper->getStoreCategories();
  5.     }
In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() function (line 157) with the code below:
  1. public function getSearchCriterias()
  2.     {
  3.         $search = $this->_searchCriterias;
  4.         /* display category filtering criteria */
  5.         if(isset($_GET['category']) && is_numeric($_GET['category'])) {
  6.             $category = Mage::getModel('catalog/category')->load($_GET['category']);
  7.             $search[] = array('name'=>'Category','value'=>$category->getName());
  8.         }
  9.         return $search;
  10.     }
replace the next function, getProductCollection(), with:
  1.   public function getProductCollection(){
  2.         if (is_null($this->_productCollection)) {
  3.             $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
  4.                 ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
  5.                 ->addMinimalPrice()
  6.                 ->addStoreFilter();
  7.                 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
  8.                 Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
  9.             /* include category filtering */
  10.             if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
  11.         }
  12.  
  13.         return $this->_productCollection;
  14.     }
In app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, after this code:
  1.         <?php foreach ($this->getSearchableAttributes() as $_attribute): ?>
  2.         <?php $_code = $_attribute->getAttributeCode() ?>
  3.         <li>
  4.             <label for="<?php echo $_code ?>"><?php echo $this->getAttributeLabel($_attribute) ?></label>
  5.             <?php switch($this->getAttributeInputType($_attribute)):
  6.                 case 'number': ?>
  7.                 <div class="range field-row">
  8.                     <input name="<?php echo $_code ?>[from]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" />
  9.                     <input name="<?php echo $_code ?>[to]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text"/>
  10.                 </div>
  11.                 <?php break;
  12.                 case 'select': ?>
  13.                     <?php echo $this->getAttributeSelectElement($_attribute) ?>
  14.                 <?php break;
  15.                 case 'yesno': ?>
  16.                     <?php echo $this->getAttributeYesNoElement($_attribute) ?>
  17.                 <?php break;
  18.                 case 'date': ?>
  19.                     <?php echo $this->getDateInput($_attribute, 'from') ?>
  20.                     -
  21.                     <?php echo $this->getDateInput($_attribute, 'to') ?>
  22.                 <?php break;
  23.                 default: ?>
  24.                 <input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
  25.             <?php endswitch; ?>
  26.         </li>
  27.         <?php endforeach; ?>
add:
  1.  
  2.         <li>
  3.             <label for="category_search_field">Search by Category:</label>
  4.             <select name="category" id="category_search_field">
  5.                 <option value="">-- Any Category --</option>
  6.                 <?php foreach ($this->getStoreCategories() as $_category): ?>
  7.                 <?php if($_category->hasChildren()): ?>
  8.                 <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
  9.                 <?php foreach ($_category->getChildren() as $subcategory):
  10.                 if($subcategory->getIsActive()) : ?>
  11.                     <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
  12.                 <?php endif; endforeach; ?>
  13.                 <?php elseif($_category->getIsActive()): ?>
  14.                 <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
  15.                 <?php endif; ?>
  16.                 <?php endforeach ?>
  17.  
  18.             </select>
  19.         </li>
Now, If you are going to search only by Category then you will face an error message like “You have to specify at least one search term”. To solve it you need to modify following file:
  • app/code/core/Mage/CatalogSearch/Model/Advanced.php
Open the file and search this function addFilters. In this function you can see following codes, replace this
  1. if ($allConditions) {
  2.             $this->getProductCollection()->addFieldsToFilter($allConditions);
  3.         } else if (!count($filteredAttributes)) {
  4.             Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
  5.         }
with following codes:
  1. if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
  2.             $this->getProductCollection()->addFieldsToFilter($allConditions);
  3.         } else if (!count($filteredAttributes)) {
  4.             Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
  5.         }
Now you won’t face any problem in advanced search only by Category or any single attribute search.

By PHP with No comments

    • Popular
    • Categories
    • Archives