PHP Storm searching for a string

If you use PHP Storm, then this will help you find something in the code such as short tags for PHP.
We had an issue that some of our server environments did not have php short tags enabled. We needed to get rid of them!
So, what you do is open up PHP Storm..then (mac version ) and hold down control, and hold down shift, then press F and in the Options check the Regular expression box. Then in the Text to Find enter:

<\?[^(php|xml)]

Kudos to Brian for this tip!

Simple Customer objects in Magento

Have you ever just needed the customers company name or first name, or some custom customer attribute?

Well, I am tired of loading the entire customer object for just 2 or 3 things.

I have 2 custom customer attributes that I need, companyname and companywebsite

This would work for regular things like first name and last name as well!

Here is how:

Version 1:

// Get the customer data and set it on the object as companyname
 $customer = Mage::getModel('customer/customer')
 ->getCollection()
 ->addFieldToFilter('entity_id', array('eq' => $customer_id))
 ->addAttributeToSelect('companyname')
 ->addAttributeToSelect('companywebsite')
 ->getItems();
$company_name = $customer[$customer_id]->getData('companyname');
$company_website = $customer[$customer_id]->getData('companywebsite');

Version #2 is slightly more easy to use

// Get the customer data and set it on the object as companyname
 $customer = Mage::getModel('customer/customer')
 ->getCollection()
 ->addFieldToFilter('entity_id', array('eq' => $customer_id))
 ->addAttributeToSelect('companyname')
 ->addAttributeToSelect('companywebsite')
 ->getLastItem();
$company_name = $customer->getData('companyname');
$company_website = $customer->getData('companywebsite');

As you can see, version 1 needs to go through the array before you can have access to the method getData.

I would recommend version 2 as it stays with the typical way of doing things ( aka the magento way ).

 

Magento – how to decode the referer part of a URL

If you have ever wondered how to figure out what that long string of letters and numbers are in a magento URL, here is how to do it.

Lets say that our URL is:

$string = 'www.whatever.com/customer/account/login/referer/aHR0cDovL2xvY2FsLmJsb29tMi5jb20vc2hvcA,,/';
Mage::helper('core')->urlDecode($string);

From what I can tell, it does the same thing as

base64_decode($string);

That will be decoded as

www.whatever.com/shop

Pretty fancy work there Magento! But I got you figured out.

Adding Custom Module with Custom Database Table

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

How to get a block in a controller

If you are in a Magento Controller and you need to get a different block and its methods….here is how:

// This function would be in a controller called something like CustomerController.php

class Bloom_BeautyBio_CustomerController extends Mage_Core_Controller_Front_Action
{
public function viewAction()
    {

/******** This is the secret sauce that gives you access to the class and its methods   ******/
        $viewingOthers = $this->getLayout()->createBlock('beautybio/beautybio')->viewingOtherMember();

        if($viewingOthers)
        {
            $this->_redirect('beautybio/customer/view/#photos');
            exit();
        }

        $this->loadLayout(array('default'));
        $this->renderLayout();
    }
}

Magento Data collection

http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

Magento: Get a rough total for all rows in all tables in mysql

$query = “SELECT table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘database_name_goes_here’”;

$result = Mage::getSingleton(‘core/resource’) ->getConnection(‘core_read’)->fetchAll($query);
$total = 0;

for( $i = 0; $i {
$total += $result[$i]['table_rows'];

}
echo ‘Grand Total = ‘ . $total;

Redhat – Sort log file by date

$ ls -l -r –sort=time

magento and ajax

http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-3-magento-controller-dispatch

Good tutorial

Magento – get shipping amount for a quote

If your on the checkout/cart page, you can get the shipping totals:
$shipping_total = $this->getQuote()->getShippingAddress()->getShippingAmount();