Archive for the ‘Magento 1.x’ Category

Pass information from Observer via session

Step 1: Mage::dispatchEvent(’my_event’); Step 2: link method MyEvent to event “my_event” in your module’s config.xml file Observer.php public function MyEvent(Varien_Event_Observer $observer) { Mage::getSingleton(’customer/session’)->setData(’my_event’, ‘results’); } } Step 3: $result = Mage::getSingleton(’customer/session’)->getData(’my_event’));

Using date and time

echo Mage::getModel(’core/date’)->date(’d/m/Y’); echo Mage::getModel(’core/date’)->timestamp(time()); Mage::getSingleton(’core/date’)->date(Varien_Date::DATETIME_PHP_FORMAT)); Mage::getModel(’core/date’)->gmtDate();

Country list

$countryList = Mage::getModel(’directory/country’) ->getCollection() ->loadData() ->toOptionArray(false);   echo ‘<pre>’; print_r($countryList);

Change layout

yourlayout.xml <my_template_page> <reference name="root"> <action method="setTemplate"><template>page/my-layout-type-2.phtml</template></action> </reference> <reference name="content"> <block type="my/template" name="text_message" template="my/template.phtml"/> </reference> </mutual_template_page>

Get Customer Data

<?php $customer = Mage::getModel(’customer/customer’)->load($customerId); if ($customer->getId()) { var_dump($customer->getFirstname()); var_dump($customer->getEmail()); var_dump($customer->getData()); }

Send Transactional Email

  // SELECT template_id FROM core_email_template WHERE template_code=’Your template code’; $templateId = 495;   $sender = array(’name’ => ‘Imran Aghan’, ’email’ => ’email@address.co.uk’);     // Set recepient information $recepientEmail = ‘someone@someaddress.co.uk’; $recepientName = ‘Name Surname’;   // Get Store ID $storeId = Mage::app()->getStore()->getId();   // Send Transactional Email Mage::getModel(’core/email_template’) ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, array( […]

Get Order Data

<?php … $order = Mage::getModel(’sales/order’)->load($orderId); …

What does in XML mean?

CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be. Example: etc/system.xml … <comment><![CDATA[In kilometers]]></comment> …

Dump SQL Query

Magento objects dump SQL on __toString() magic method call. Official PHP Documentation describes aforementioned magic method as follows: The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR […]

SQL Query IN and NOT IN

Magento uses keywords in for IN and nin for NOT IN …   $productIds = array(1251,1253,1256);   if ($column->getFilter()->getValue()) { $this->getCollection()->addFieldToFilter(’entity_id’, array(’in’=>$productIds)); } else { $this->getCollection()->addFieldToFilter(’entity_id’, array(’nin’=>$productIds)); } …