Engine23

Magento Ecommerce Strategy, Design & Development

Magento - Get the sql for a collection

If you ever were curious about what the SQL is being built for a Magento Collection, its really, really easy

$collection = Mage::getModel('poll/poll')->getResourceCollection()
->addFieldToFilter('date_posted', array('from' =>'2014-02-01 00:00:00', 'to'=>'2014-02-28 23:59:59'));
$collection->load(true);

The result will be something similar to this:

SELECT `main_table`.* FROM `poll` AS `main_table` WHERE (date_posted >= '2014-02-01 00:00:00' AND date_posted <= '2014-02-28 23:59:59')

Magento - How to fix error message: MediabrowserUtility is not defined

 error: error in [unknown object].fireEvent():

event name: open_browser_callback

error message: MediabrowserUtility is not defined 

Step 1: Add this to the adminhtml xml for the custom extension

<?xml version="1.0"?>
<layout>
<default>
<reference name="head">
<action method="setCanLoadExtJs"><flag>1</flag></action>
<action method="addJs"><script>mage/adminhtml/variables.js</script></action>
<action method="addJs"><script>mage/adminhtml/wysiwyg/widget.js</script></action>
<action method="addJs"><script>lib/flex.js</script></action>
<action method="addJs"><script>lib/FABridge.js</script></action>
<action method="addJs"><script>mage/adminhtml/flexuploader.js</script></action>
<action method="addJs"><script>mage/adminhtml/browser.js</script></action>
<action method="addJs"><script>prototype/window.js</script></action>
<action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
</reference>
</default>
</layout>

Read more

Masterlock - combination recovery helper

One day, I was at work and a friend of mine was trying to recall what his Master lock combination was.  He knew the first and last number.  I had done a bit of googling and found that if you know the last number of the combination, there is a defined set of possible options!  This was promising.  After figuring out the algorythm, I set forth on a quest to provide a nice template for him to use when doing the recovery.  It turns out that there are only 100 possible combination should you know that last number. 

If you want to use it, please click here.

Magento - Concat part of a magento collection

When building admin area modules, you need to sometimes use the admin users. When doing so, you may want to combine the firstname and lastname into one. This is where concat comes in handy. It will combine them, into one string to be used as the visual for the dropdown. The following function will also show you how to get all the admin users using a collection.

    /*
    *  
    */
    public function getAdminUsersAsOptions()
    {

        $collection = Mage::getModel('admin/user')->getCollection()
                            ->addFieldToSelect('firstname')
                            ->addFieldToSelect('lastname')
                            ->addFieldToSelect('user_id');
        $collection->setOrder('lastname', 'ASC');
        $fields      = array('lastname', 'firstname');
        $fieldConcat = $collection->getConnection()->getConcatSql($fields, ', ');

        $collection->getSelect()->columns(array('value'=> 'user_id', 'name' => $fieldConcat));
        $items = $collection->getItems();
        $user_array = array();
        foreach($items as $_user)
        {
            $user_array[$_user->getData('user_id')] = $_user->getData('name');
        }
        return $user_array;
    }

Setting new Mac 10.9 for PHP development

  1. Download MySQL DMG  http://dev.mysql.com/downloads/mysql/
  2. Install both the MySQL and the startup packages ( this may require approval in your  Mac OS X Privacy & Security settings )
  3. Start mysql via command line  
    sudo /usr/local/mysql/support-files/mysql.server start
  4. Start apache via command line  
    sudo apachectl start
  5. Create a folder for your virtual host files
    sudo mkdir  /etc/apache2/extra/vhosts 

Read more

GIT Log - Dump to a txt file

I recently needed to gather just a handful of recent commits for a code review.

This actually is very easy to do, with the appropriate flags.

git --no-pager log --stat --since='2014-03-07 04:08:28' > git-log.txt

Now, you can only get the logs since that date/time.

Here is what it would look like:

commit 3551e428e9c214517165c180c306223c53cbdd01
Author: ralbin <russell@russellalbin.com>
Date: Thu Mar 13 11:37:37 2014 -0500 making it so the reduced price is not so dark skin/frontend/default/theme589/css/styles.css | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-) commit e07fd40bc9985164980f16f74864f57ce14ea3d7
Author: ralbin <russell@russellalbin.com>
Date: Thu Mar 13 11:32:51 2014 -0500 applying css fix app/etc/modules/Engine23_Customer.xml | 2 +-
skin/frontend/default/theme589/css/styles.css | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)

Display Bundle Product Weight

We had a store where we disabled user configuration of bundle products and set them up the way we wanted as defaults.  That made this much easier, but something similar could be done to update the weight via an ajax call as the configuration is changes.

 

Override the getAdditionalData function in Mage_Catalog_Block_Product_View_Attributes

 

About line 10 you will see where we look for when a bundle product's weight is being set and override it.  Once we catch the right piece of logic, we the weight of the products up and also catch the logic to do the proper formatting logic.  

public function getAdditionalData(array $excludeAttr = array())
    {
        $data = array();
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {        

Read more

Remembering Apache Restart Command

First off the command to restart Apache2 is:

sudo apachectl -k restart

You can only lookup the command so many times before figuring out a way to remember it. I decided to try to figure out what apachectl was. I found that apachectl is not even Apache2, it's actually a controller for it, or apache controller, or apache ctl .... apachectl. Hopefully it's a bit easier to remember to call the apache controller than apache gobbly goop of letters.

Magento - How to add captcha to the contact us form

Step 1: create the module instantiation xml: app/etc/modules/Engine23_Contactform.xml

<?xml version="1.0"?>
<config>
<modules>
<Engine23_Contactform>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Captcha/>
</depends>
</Engine23_Contactform>
</modules>
</config>

Step 2: Create the folder for our controller and config.xml: app/code/local/Engine23/Contactform

Step 3: Create the IndexController: app/code/local/Engine23/Contactform/controllers/IndexController.php

 <?php 
/*
 * Adding Captcha to Contacts form
 */
require_once(Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php');

class Engine23_Contactform_IndexController extends Mage_Contacts_IndexController
{        

Read more