Yearly Archives: 2013


Check if a customer is logged in on a magento controller 1

As Alan Storm wrote about here http://alanstorm.com/magento_create_customer_page, there are a variety of different ways to check if a customer has an active session in Magento.

I want to check if the user/customer is logged in before all action methods. This seems to be the most common solution suggested

if(Mage::getSingleton('customer/session')->isLoggedIn()){
}

but in a controller’s preDispatch() method it does not work. It does work inside a specific action method however.

This solution that Alan found actually works. It’ll redirect to the login page and then redirect back to the referring page after successful login.

public function preDispatch()
{
    parent::preDispatch();
    $action = $this->getRequest()->getActionName();
    $loginUrl = Mage::helper('customer')->getLoginUrl();
 
    if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
        $this->setFlag('', self::FLAG_NO_DISPATCH, true);
    }
}

Magento: how to display all product attributes

$attributes = $_product->getAttributes ();
foreach ( $attributes as $attribute ) {
	if ($attribute->getIsVisibleOnFront ()) {
		$value = $attribute->getFrontend ()->getValue ( $_product );		
		if (is_string ( $value ) && strlen ( $value )) {
		?>
		<p class="no-space">
			<span class="text-blue bold-text"><?php echo $attribute->getStoreLabel()?>: </span>
			<?php echo $value ?>
		</p>
		<?php
		}
	}
}

How to run Yii Migrations on Linux

After developing an application on a windows box using “php yiic migrate”, I found this did not work on Linux. I received an error “yiic command not found” and also “php command not found”. Both of these executable’s need to be added to your linux user’s PATH, which is a bunch of directories that linux will search through to find executable files. Here is a great resource on working with the PATH http://www.troubleshooters.com/linux/prepostpath.htm. To make “php yiic migrate” work I added the below line to the /etc/profile file, which provides the directory to the php executable on the machine along with the location of your migrations directory. The connection/session may need to be closed and reopened for it to take effect.

export PATH=$PATH:/usr/local/bin:/var/www/youryiisitedirectory/protected

Now when you navigate to the migrations directory, you can run “php yiic migrate” as usual.


Difference between functional specs and technical specs

A functional specification describes how a product will work entirely from the user’s perspective. It doesn’t care how the thing is implemented. It talks about features. It specifies screens, menus, dialogs, and so on.

A technical specification describes the internal implementation of the program. It talks about data structures, relational database models, choice of programming languages and tools, algorithms, etc.


How to add buttons to CGridView when using CArrayDataProvider 1

By default, the button’s used by CGridView expect an array of objects having an “id” property that it can use with the edit and delete buttons. If you’re using CArrayDataProvider, this won’t work. I was able to get the below code working for a delete button. Note that you have to specify the button you want in the “template” attribute, otherwise it’ll try to build all the buttons using the non existent id property.

'dataProvider'=>$dataProvider,
	'columns'=>array(
			'name',
			array(
				'class'=>'CButtonColumn',
				'template' => '{delete}',
				'buttons' => array(
					'delete' => array(
							'url' => 'Yii::app()->controller->createUrl("mycontroller/myaction", array("name"=>$data["name"]))',
					),
				),
			),
	),