Tuesday 12 May 2015

Magento Previous-Next Buttons on Product Details Page

<?php
// Prev-Next links $_product = $this->getProduct(); if(!$_product->getCategoryIds()) return; // if product is not in any category, do not display prev-next :)
$cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
$cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category, you should enhance this, it works for me
$order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
$direction = 'asc'; // asc or desc
$category_products = $cat->getProductCollection()->addAttributeToSort($order, $direction);
$category_products->addAttributeToFilter('status',1); // 1 or 2
$category_products->addAttributeToFilter('visibility',4); // 1.2.3.4
$cat_prod_ids = $category_products->getAllIds(); // get all products from the category
$_product_id = $_product->getId();
$_pos = array_search($_product_id, $cat_prod_ids); // get position of current product
$_next_pos = $_pos+1;
$_prev_pos = $_pos-1; // get the next product url
if( isset($cat_prod_ids[$_next_pos]) ) {
$_next_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_next_pos] );
} else {
$_next_prod = Mage::getModel('catalog/product')->load( reset($cat_prod_ids) ); } // get the prev product url
if( isset($cat_prod_ids[$_prev_pos]) )
{
$_prev_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_prev_pos] );
} else {
$_prev_prod = Mage::getModel('catalog/product')->load( end($cat_prod_ids) ); } ?>
<div>
    <?php if($_prev_prod != NULL): ?>
    <a href="<?php print $_prev_prod->getUrlPath();
    if($search_parameter):?>?search=1<?php endif;?>">
    <span><?php echo $this->__('Previous') ?></span></a>
    <?php endif; ?> <?php if($_next_prod != NULL): ?> <a href="<?php print $_next_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>">
    <span><?php echo $this->__('Next') ?></span></a> <?php endif; ?>
</div> 

Magento - using getPriceHtml on custom page template

<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
      $productBlock = $this->getLayout()->createBlock('catalog/product_price');
      echo $productBlock->getPriceHtml($_product);
 ?>

Loop through each character in a string in PHP

<?php
$str = "String to loop through"
$strlen = strlen( $str );
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
// $char contains the current character, so do your processing here
}
?>
Once I had the loop set up I simply added an is_ numeric() check and inserted a break command the first time the check returned false – here is my use case:
<?php
$str = "123?param=value"
$strlen = strlen( $str );
$id = "";
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
if( ! is_numeric( $char ) ) { break; }
$id .= $char;
}
// $id now contains the ID I need, in this case: 123
?>

Filter Product Collection for Multi Select Attribute Option value in Magento

$brandLabel = $_product->getAttributeText('manufacturer');
           
            $option_id = $attr->getSource()->getOptionId($brandLabel);

            $attrcode="manufacturer";

$product_collection=Mage::getModel("catalog/product")->getCollection();
   $product_collection->addAttributeToFilter($attrcode,$option_id);
            foreach($product_collection as $_product){
                $_product = Mage::getModel('catalog/product')->load($_product->getId());
            }