Monday, October 26, 2015

PHP Remove special (non printable) characters from text

 

$mystring = " Fund BDM̢۪s were then able to engage with 19 employers, most of whom were subsequently converted to the MTAA Super Clearinghouse.
";
preg_replace("/[^\x0A\x20-\x7E]/",'', $mystring);

echo $mystring;

//The above code will result in

Fund BDMs were then able to engage with 19 employers, most of whom were subsequently converted to the MTAA Super Clearinghouse..
        

Wednesday, October 21, 2015

DISPLAY OUT OF STOCK PRODUCTS ACCORDING TO ADMIN INVENTORY SETTING

 

$isOutOfStockAllowed = Mage::getStoreConfig('cataloginventory/options/show_out_of_stock');
 
  foreach($_productCollection as $_product)
       {
        if(!$isOutOfStockAllowed){
         if(!$_product->isSaleable()){
          continue;
         }
         
        }
        //PRODUCT DISPLAY CODE HERE
        
        }
        

Saturday, October 17, 2015

Facebook page feed time issue

I was implementing facebook page feed for one of my client, the feeds were appearing correct but time was different, actually it was a timezone issue, you have to convert facebook time to local timezone, and to do this i found the perfect way.

step1.
Download and include the js file into your script.

https://gist.github.com/aredo/4153245

step2:

use below code to determine the client timezone:

<script type="text/javascript">
    jQuery(document).ready(function() {
       var timezone = jstz.determine();
var timezonename = timezone.name();  
    });
</script>

Saturday, December 20, 2014

Magento Flat category , show category tree

Below code displays dropdown of categories and subcategories upto n level.


<!----NEW CODE---->

<select id="category-changer"  name="cat" style="width:150px;">
<option value="6"> All Categories </option>
<?php echo $catlistHtml; ?>  
</select>
<?php
function str_prefix($str, $n=1, $char=" "){
    for ($x=0;$x<$n;$x++){ $str = $char . $str; }
    return $str;
}
function getTreeCategories($parentId, $isChild){
    $_cathelper = Mage::helper('catalog/category');
$allCats = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active','1')
                ->addAttributeToFilter('parent_id',array('eq' => $parentId));
    $class = ($isChild) ? "sub-cat-list" : "cat-list";
    //$children = Mage::getModel('catalog/category')->getCategories(7);
    foreach ($allCats as $category)
    {
   $prefix = '';
   $_category =  Mage::getModel('catalog/category')->load($category->getId());
   $lblval=$_category->getLevel()-3;
$catname = ($lblval==0)?strtoupper($_category->getName()):$_category->getName();
$catname =  str_prefix($catname,$lblval*2,"-");
        $html .= '<option lbl="'.$_category->getLevel().'" value="'.$_category->getId().'" caturl="'.$_cathelper->getCategoryUrl($_category).'" >'.$catname."</option>";
        $subcats = $category->getChildren();
        if($subcats != ''){
            $html .= getTreeCategories($category->getId(), true);
        }
    }
    return $html;
}
$catlistHtml = getTreeCategories(6, false);
?>

<!--NEW CODE ENDS--> 

Monday, August 12, 2013

Magento add datepicker to frontend

Code referece:

http://stackoverflow.com/questions/8335456/magento-how-do-i-add-a-frontend-date-picker-that-returns-dd-mm-yy-instead-of-m


STEPS:
1. Add js and css necessary for the datepicker in frontend
<reference name="head">
        <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
        <action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
        <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
</reference>
 
2.
 <block type="core/html_calendar" name="html_calendar" as="html_calendar" template="page/js/calendar.phtml"/> 

3.call that block in the phtml file where u want to implement datepicker.
<?php echo $this->getChildHtml('html_calendar') ?>
 
4. Last use the below code:
 
<script type="text/javascript">
  Calendar.setup({
  inputField : 'deliverydate',
  ifFormat : '%m/%e/%y',
  button : 'date_from_trig',
  align : 'Bl',
  singleClick : true
  });
</script> 

Tuesday, June 18, 2013

Magento signin signout links on any phtml file

 <ul class="links si_ac">
            <?php
            $signout =$baseurl.'customer/account/logout';
            if(!Mage::getSingleton('customer/session')->isLoggedIn()) {
            $signinUrl =$baseurl.'customer/account/login';
            $signup   =$baseurl.'customer/account/create';
             $linkks = "<li><a href='$signinUrl'>".$this->__("Sign In")."</a></li>
                <li class='last'><a  href='$signup'>".$this->__("Create an Account")."</a></li>";
            }else{
              $linkks = "<li class='last'><a  href='$signout'>".$this->__("Sign Out")."</a></li>";
            }
            echo $linkks;
            ?> 
         </ul>

Friday, May 17, 2013

magento display multi select attribute in admin grid with filter



I have created a custom module by name supplier , and displayed several supplier on the product page in admin like in the image above, Now the multiselected values were not appearing properly in the grid page.

It was appearing empty for multiselected values. although i managed to display single selected values properly by the post:

http://blog.chapagain.com.np/magento-how-to-search-or-filter-by-multiselect-attribute-in-admin-grid/

But still there was problem with multi selected values, so i defined "renderer"
$this->addColumn('supplier', array(
            'header'    => Mage::helper('catalog')->__('supplier'),
            'width'     => '180px',
            'index'     => 'supplier',
            'type'  => 'options',
            'options' => $item_types,
             'filter_condition_callback' => array($this, '_filterSupplierCallback'),
            'renderer'=>new Mage_Adminhtml_Block_Catalog_Product_Columns_Supplier()        ));

IN THE RENDERER I USED BELOW CODE TO RETURN THE LIST OF MULTI SELECTED SUPPLIER:

public function render(Varien_Object $row)
    {
        $suppliers=explode(',',$row->getSupplier());
        $suppstr="";
        if(count($suppliers)>0)
        {
            $suppstr="<ul>";
            foreach($suppliers as $value)
            {
                $productModel = Mage::getModel('catalog/product');
                $attr = $productModel->getResource()->getAttribute("supplier");
                if ($attr->usesSource()) {
                $suppstr   .= "<li>".$attr->getSource()->getOptionText("$value")."</li>";
                }

            }
            $suppstr   .= "</ul>";
  
        }
          return $suppstr;   
       
    }



And successfully displayed multiselected options in the grid.