Just another WordPress.com weblog

Archive for the ‘Version 1.4.*’ Category

How to Create Custom Magento Module Log

Wrapping it up in a class:

class Nick_Module_Model_Log {
  protected $_adapter;

  public __construct() {
    $this->_adapter = Mage::getModel('core/log_adapter', 'my_module.log');
  }

  // It's public incase we want to use ->debug(), ->info(), and friends
  public function getAdapter() {
    return $this->_adapter;
  }

  public function log($message) {
    $this->getAdapter()->log($message);
  }

}
Set up your own log adapter with:
$my_log = Mage::getModel('core/log_adapter', 'my_module.log');

and log away!

$my_log = Mage::getSingleton('nick/log');
$my_log->log("Logging away!");

Thanks,

Vatsal Patadia

Advertisements

How to add custom field/attribute in magento category?

Add custom Field/attribute to category using script in magento
$setup = new Mage_Eav_Model_Entity_Setup(‘core_setup’);
// below code will add text attribute
$setup->addAttribute(‘catalog_category’, ‘attribute_code’, array(
‘group’         => ‘General’,
‘input’         => ‘text’,
‘type’          => ‘varchar’,
‘label’         => ‘Attribute label’,
‘backend’       => ”,
‘visible’       => 1,
‘required’      => 0,
‘user_defined’ => 1,
‘global’        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

// below code will add yes/no attribute

$setup->addAttribute(‘catalog_category’, ‘attribute_code’,  array(
‘type’     => ‘int’,
‘label’    => ‘Attribute label’,
‘input’    => ‘select’,
‘source’   => ‘eav/entity_attribute_source_boolean’,
‘global’   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
‘required’ => false,
‘default’  => 1,
‘user_defined’  => 1,
‘default’  => 0
));

// below code will use to add image attribute

$setup->addAttribute(‘catalog_category’, ‘attribute_code’,  array(
‘group’         => ‘General’,
‘type’     => ‘varchar’,
‘label’    => ‘Attribute Label’,
‘input’    => ‘image’,
‘source’   => ‘eav/entity_attribute_source_boolean’,
‘global’        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
‘required’ => false,
‘backend’ => ‘catalog/category_attribute_backend_image’,
‘frontend_input’ =>”,
‘default’  => 1,
‘visible’       => 1,
‘user_defined’  => 1,
));

 

‘attribute_code’ will be replace with your own attribute code and same way ‘Attribute Label’ will be replace with attribute label.

Once you execute above script, it will automatically creates attribute.

Thanks,

Bijal Bhavsar 🙂

 

client denied by server configuration: app/etc/local.xml

client denied by server configuration: PROJECT_DIR/app/etc/local.xml

It seems that magento is testing to see if the file can be accessed each time a page is loaded and alerts 
you to the fact that this file contains your MySQL database password is readable. 

So it is correct but seems odd that every page should check this filling the log with error messages 
instead of just doing it once at the login page.

To resolve the above error not shown in error_log at cpanel

To get rid of this security check, do not attempt to hack the Magento core (as some forum threads have
 suggested in the past). Instead, you can just modify the admin-theme to remove this check. 
Open up the following file or create it if it does not exist:
"app/design/adminhtml/default/default/layout/local.xml"

Add below code in local.xml of adminhtml/layout

<layout>
 <default>
 <remove name="notification_security" />
 <remove name="notification_survey" />
 </default>
</layout> 

This removes the blocks notification_security and notification_survey from the backend-pages entirely, 
skipping therefor the security check. No core hacks involved. 

Thanks,
Bijal Bhavsar :) 

how to get order object by increment id?

Hello,

 

We can get order object by increment id using following code:

$orders = Mage::getModel('sales/order')
  ->getCollection() 
  ->addAttributeToFilter('increment_id', 100000001)
  ->getFirstItem();

 

Thanks,

Bijal Bhavsar 🙂

‘Mage not defined’ error in magento

js error Mage.Cookies.path var Mage not defined

To resolve this issues you need to be sure that in head.phtml (app/design/frontend/default/default/template/page/html/head.phtml ) you have
getChildHtml() ?> after getCssJsHtml() ?>.
So it should look like:
getCssJsHtml() ?>
getChildHtml() ?>

How to add different toolbar template for bottom toolbar in listing page?

Hello Everyone,

<?php $this->getToolbarBlock()-> setTemplate('catalog/product/list/toolbar_bottom.phtml')->toHtml(); ?>

The above code will display bottom toolbar from toolbar_bottom.phtml file.

Thanks,
Bijal Bhavsar 🙂

How to encode product image path in magento to image blocking

Hello All,

Below is the steps to change encoded path of image and display in listing page of magento

1) Add below funciton in Image.php project/app/Local/Mage/Catalog/Helper/Image.php
Copy the core file and place in local folder and than add the code in local file.

/*********Function added to encrypt image path**********/

public function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {

$changeFilePath = explode(‘cache’,$filename);
$changeFilePath = Mage::getBaseDir(). DS. ‘media’. DS. ‘catalog’.DS.’products’.DS.’cache’.$changeFilePath[1];

$imgbinary = fread(fopen($changeFilePath, “r”), filesize($changeFilePath));
return ‘data:image/’ . $filetype . ‘;base64,’ . base64_encode($imgbinary);
}
}

Now we will call above function in list.phtml file of project/app/design/catalog/product/list.phtml

Add below inside foreach loop

<?php
$this->helper('catalog/image')->init($_product, 'small_image')->resize(193,250);
$forFiletype = explode('.',$product_image);
$forFiletype = array_reverse($forFiletype);
$filetype = $forFiletype[0];
if(strtoupper($filetype) == 'JPG')
{
$filetype = 'jpeg';
}
$base64image = '';
$base64image = $this->helper('catalog/image')->base64_encode_image($product_image,$filetype);

?>

And change Image tag of product same as Below:


<img src="<?php echo $this->getSkinUrl('images/spacer.png'); ?>" width="193" height="250" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" style="background:url(<?php echo $base64image;?>) no-repeat;"/>

When user save image it will save spacer image and not the original image.

I hope this post is helpful to you. If you have any question leave the comment.

Thanks,
Bijal Bhavsar 🙂