Magento: Save shipment information of order programatically
After creating invoice and shipment, it is necessary to add tracking information to shipment. Here is how to write a observer which will invoke as shipment save method is called and save tracking information programatically.
config.xml – under global -> events node
<sales_order_shipment_save_before> | |
<observers> | |
<namespace_modulename_ship_before> | |
<type>singleton</type> | |
<class>Namespace_Modulename_Model_Observer</class> | |
<method>salesOrderShipmentSaveBefore</method> | |
</namespace_modulename_ship_before> | |
</observers> | |
</sales_order_shipment_save_before> |
Observer.php -> under Model directory of module
public function salesOrderShipmentSaveBefore($observer) | |
{ | |
$shipment = $observer->getEvent()->getShipment(); | |
$track = Mage::getModel('sales/order_shipment_track') | |
->setNumber('824343454454') //tracking number / awb number | |
->setCarrierCode('aramex') //carrier code | |
->setTitle('Aramex'); //carrier title | |
$shipment->addTrack($track); | |
} |
Magento: Mysql records with NULL values are not fetched in query
After banging my head on my desk trying to get the records with NULL values with Magento ORM, it was found that writing
$collection->addAttributeToFilter('somefield', 'null') | |
$collection->addAttributeToFilter('somefield', array('is' => 'null')) |
will check for any blank values like this: WHERE somefield = ”
So if you want to fetch records that have NULL values in Magento style, you need to write as following:
$collection->addAttributeToFilter('somefield', array('null'=>'null') |
will check like WHERE somefield = null
Hope this saves someone’s time!
Magento: Wrong count in admin Grid when using GROUP BY clause, overriding lib module
If you have noticed or not, in version 1.5 of Magento when you use GROUP BY clause in any Grid.php file in admin, the count always display wrong. Many times it displays 1 even if there are hundreds of records. Due to this your pagination also doesn’t work. This is a bug in Magento. Your getSize() always returns wrong count whereas total records in grid are proper.
To fix this, you need to edit one of your core file. As it’s not a good practice to edit core file, we will here override the core file.
Overriding LIB module
Copy Db.php file from magento / lib / Varien / Data / Collection / Db.php
Paste it to your local directory so the resultant folder structure would look like this:
magento / app / code / local / Varien / Data / Collection / Db.php
Continue reading »
Magento: Show maintenance mode page (website under construction)
If you want your Magento website to show in maintenance mode, you will have to do two things.
1. Create a file name maintenance.flag in your magento root directory. Contents under this file doesn’t matter, you can keep it empty.
2. Change the maintenance file (located in magento root -> errors -> default directory) to show proper message when user visits your website.
Magento: Register guest user to website if email provided
If your Magento website have feature where guest user can place an order without registering themself in your site, you can add them as a customer in your database. It helps in grouping the orders w.r.t email address in backend sales order. The only condition is that he should provide his email address while placing an order, which is quite obvious.
So here is a snippet of code you can insert where you are asking user to enter his email address. Just check if the user is already not registered in your site.
$e = $email; //provided by guest user | |
$store = Mage::app()->getStore(); | |
$websiteId = Mage::app()->getWebsite()->getId(); | |
$customerObj = Mage::getModel('customer/customer'); | |
$customerObj->website_id = $websiteId; | |
$customerObj->setStore($store); | |
$prefix = "mag"; | |
$pwd = uniqid($prefix); | |
$session = Mage::getSingleton('checkout/session'); | |
$fname = $session->getFirstname(); | |
$lname = $session->getLastname(); | |
$customerObj->setEmail($e); | |
$customerObj->setFirstname('Guest'); | |
$customerObj->setLastname('Guest'); | |
$customerObj->setPassword($pwd); | |
$customerObj->save(); | |
$customerObj->sendNewAccountEmail('confirmed'); //auto confirmed |
UPDATE: It depends where you want to keep this functionality on your site. I have put it in saveShippingMethodAction() function of Mage/Checkout/controllers/OnepageController.php file after extending. Don’t forget, this code should only be run if customer is guest.
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)