Archive

Archive for October, 2009

How to Detect if the Page is Secure in a Magento Template?

October 22nd, 2009
Comments Off

When you are designing a Magento template it is easy to overlook a simple fact: most links should not be hard-coded and you should always use the methods that are available for you like getSkinUrl() and getJsUrl(). In addition to pointing to the right root folder, these methods also point to the secure or unsecure URLs, ‘https’ and ‘http’ respectively. However, how can you detect if a URL needs to be secured or not?

herring-hall-marvin-safe

In a recent Magento project, I needed to do exactly that: find out if we are in secure or in a non secure page and point to the right jQuery file on Google CDN’s. Here is the code:

<?php if(Mage::app()->getStore()->isCurrentlySecure()): ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<?php else: ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<?php endif;  ?>

The key method here is the Mage::app()->getStore()->isCurrentlySecure().

Reblog this post [with Zemanta]

Magento , ,

ZenCart 1.3.8 and PHP 5.2.11 Bug: problem login to admin area

October 15th, 2009

Yes, I do have some clients that still use ZenCart. In fact, some ZenCart stores are doing very well in terms of SEO and more importantly, sales. This morning, I ran a routine yum update command on one of my clients servers. To my surprise, after rebooting the admin was inaccessible. I’ll share with you the symptoms so you can identify it and the solution that worked for me.

zencart-1.3.8-php-5.2.11-bug

The symptoms:

  1. Not able to login to Admin panel. More importantly, when you try to login it doesn’t show any error message.
  2. No admin is able to login, in other words, the problem is global.
  3. If you look in the php log files, you’ll find a similar line:
    PHP Fatal error:  Class ‘queryFactory’ not found in /var/www/html/includes/functions/sessions.php on line 54, referer: http://www.securepersonalcare.com/admin/login.php

What is happening:

While I am not sure exactly, this post in the ZenCart forums gave up a clue: it was something in PHP 5.2.11. Now, it may be a new bug in this new version or a bug that got resolved and as a result it broke something in the old ZenCart. Note that as of this writing, ZenCart 1.3.8 is about a year and nine months old. In internet time this is a significant amount of time.

The solution that

Once I had an assumption for what the problem is, I worked on reverting the PHP version to an older one. However, when I tried to revert using the Atomic repository – it did not work because while they do make old versions of php available, the php-common is not. I ended up removing the atomic repository and using CentOS repositories which provide PHP ver 5.1.6 something. Reinstalled, restarted apache, and vuala!

Now, only if the ZenCart team will get off their butts (or maybe get on their butts) and finish up what they call the next release of this eCommerce application!! What is the next version 1.4, 1.6, 2.0, 2.5? of ZenCart, well that is another discussion altogether.

ZenCart , ,

Magento’s massAction for Custom Plugins

October 8th, 2009
Comments Off

Ever wanted to allow your users to make mass action on your Magento Custom Plugin records? The good news is that it is built in to the Magento Admin Panel and is available at the community edition. The bad news is that there is absolutely no documentation on how to use it. Here are the 3 steps you will need to take in order to develop this feature in your custom plugin:

magento-mass-action

1. Prepare a Grid Container and Grid in your backend:
For this you will need a collection of records, a controller, and the basic blocks of a grid admin page. I will not cover this in this post, but you can find more about setting it up nicely on Tips for Twits Magento Blog, the article is called Howto: Repackageable custom extension development in Magento.

2. Add the _prepareMassaction() method inside the Grid class:
Add the following code inside your module class that extends Mage_Adminhtml_Block_Widget_Grid:

protected function _prepareMassaction()
 {
 $this->setMassactionIdField('entity_id');
 $this->getMassactionBlock()->setFormFieldName('product');

 $this->getMassactionBlock()->addItem('add', array(
 'label'    => Mage::helper('rma')->__('Add Products to RMA List'),
 'url'      => $this->getUrl('*/*/massAdd'),
 ));

 return $this;
 }

A few things to notice:

2.1. ‘entity_id’ is the database column that serves as the unique identifier throughout your data structure, including: db table, single product magento model, and the collection.
2.2. setFormFieldName(‘product’) – the text ‘product’ is flexible but you will need it in the next step, so remember it.
2.3. The routing string ‘*/*/massAdd’ will triger a method called massAddAction() in your controller.

3. Prepare the actual action to be taken in your controller
Here is the code that needs to be added, notes are below:

public function massAddAction()
 {
 $productIds = $this->getRequest()->getParam('product');
 if(!is_array($productIds)) {
 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('rma')->__('Please select product(s)'));
 } else {
 try {
 $product = Mage::getModel('catalog/product');

 foreach ($productIds as $productId) 
 {
 $product->reset()->load($productId);
 $rmaProduct = Mage::getModel('rma/product');
 $rmaProduct->setName($product->getName())
 ->setDescription($product->getDescription())
 ->setStatus(Activo_RMA_Model_Product::STATUS_ENABLED)
 ->setCreatedAt(now())
 ->setCatalogProductId($productId)
 ->save();
 }
 Mage::getSingleton('adminhtml/session')->addSuccess(
 Mage::helper('rma')->__(
 'Total of %d product(s) were successfully added to the RMA product list', count($productIds)
 )
 );
 } catch (Exception $e) {
 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
 }
 }

 $this->_redirect('*/*/index');

 }

Notes:

3.1. The method name is routed from the previously provided $this->getUrl(‘*/*/massAdd’) method.
3.2. Yes, the variable $productIds contains a simple array of entity_id numbers. This is not a collection by any means, so you should treat it this way.
3.3. Inside the main foreach loop, you can pretty much do whatever action you wanted.

In conclusion, with about 20 lines of code and updates to two files we are able to leverage a powerful built in feature provided by Magento which allows for mass actions on our records. Obviously, this little excerpt is not for the beginners out there. However, I hope this will make some developers’ lives a bit easier.

Let me know how what other advanced Magento features you use the most.

Magento , ,

Updating iNove to work with KeywordLuv plugin

October 5th, 2009

Last week I discussed a new wordpress plugin that got me excited: KeywordLuv. I also promised to show you how I fixed the issue with iNove theme. Here it goes:

fixing-inove-toolbox

The main issue is that the iNove theme uses the get_comment_author_url() and the KeywordLuv works on a different method, the get_comment_author_link(). So, here are the steps to update your iNove theme in a way that the author will show the Author name and the keywords properly:

  1. Go to your WordPress admin area
  2. Install the DoFollow plugin
  3. Install the KeywordLuv plugin
  4. Go to the Appearance tab
  5. Click on the Editor (Themes Editor for wordpress)
  6. Click on the ‘Theme Functions‘ file (functions.php)
  7. Look for these lines of code around line 230:

    <?php if (get_comment_author_url()) : ?>
    <a id="commentauthor-<?php comment_ID() ?>" href="<?php comment_author_url() ?>" rel="external nofollow">
    <?php else : ?>
    <span id="commentauthor-<?php comment_ID() ?>">
    <?php endif; ?>
    <?php comment_author(); ?>
    <?php if(get_comment_author_url()) : ?>
    </a>
    <?php else : ?>
    </span>
    <?php endif; ?>
  8. Erase the above lines and instead insert the following code:

    <?php echo get_comment_author_link(); ?>
  9. Save by clicking the ‘update file’ & check your work

That should do it. Notice that this site already has it installed and working properly. Disclaimer: the steps above are provided with the intention that it may fix an issue – there is by no means a guarantee. Each blog is slightly different.

Let me know how this worked out for you.

Web Development ,

KeywordLuv: a must have wordpress plugin

October 2nd, 2009

KeywordLuv is the latest hot wordpress plugin. It did not officially catch on fire yet, but it will. I won’t be surprised to find it built in next versions of wordpress. So what is it anyway? From the publisher’s website:

Description: Reward your commentators by separating their name from their keywords in the link to their website, giving them improved anchor text.

- Stephan Cronin

keywordluv-google-wonder-wheelIn other words it lets commentators leave a comment and allow area for their name and keywords while showing clear separation.  Instead of having Dedicated Advanced Hosting or Magento Plugins post comments on your site, the author of the comment can leave their name and get the credit they want with the keywords they selected. Three immediate benefits arise from this plugin/feature:

  1. Clear separation between author name and the keyword.
  2. Since the plugin needs a DoFollow plugin installed and the keywords entice authors to comment on your posts, more people will potentially join the discussion.
  3. Ability to separate between spammers and non spammers easily (for now).

There was a tiny issue with inove and the usage of this plugin. Next week, I will share with you how to address the issue in the iNove theme.

Let me know if you find this plugin useful or not.

Web Development

Coming Book Review for Magento: Beginner’s Guide

October 1st, 2009

Magento-for-Beginners-GuideI’ve recently been approach by a UK publisher, Packt Publishing, to review a new books about Magento. The book is called Magento: Beginner’s Guide and covers many aspects of the Magento eCommerce platform. In fact, it almost seem like an intermediate or even advanced from a merchant’s point of view. Here are a few pros and cons for this book which I drafted from a quick 10 minutes glance:

Pros:

  • From the table of content, the book seem to be comprehensive and covers many parts of the Magento eCommerce.
  • The book has a duo setup of Author(William Rice) + Technical Reviewer(Jose Argudo) – which is promising.
  • Installation chapter includes FTP instructions and folder/file permissions instructions which is more than I have seen in similar books.

Cons:

  • The book claims to target everyone. Realistically, this book seem to target smaller merchants who will be installing, setting up, and running the store themselves.
  • The book refers to a bit outdated version of Magento: 1.2.x.x. Not a big issue since Magento’s main features have not changed much.
  • Seem to be a bit technical for the average smaller merchants. Still remains to be seen how technical.

If you have any thoughts about this book or if you read it and have something to say about it, let me know.

Magento