Magento
Magento – Upgrading Magento Core and Modules Via SSH
[shell] chmod 550 ./mage ./mage mage-setup . ./mage sync ./mage install http://connect20.magentocommerce.com/community Mage_All_Latest –force ./mage upgrade-all –force rm -rf downloader/.cache/* downloader/pearlib/cache/* downloader/pearlib/download/* var/cache/* php shell/indexer.php reindexall [/shell]
Magento – Programmatically Load and Delete Products
[php] <?php // Register a secure admin environment Mage::register(‘isSecureArea’, 1); // Load the Magento product by entity_id $product = Mage::getModel(‘catalog/product’)->load($productId); // Load the Magento product by sku $product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’, $productSku); // Load the Magento product by name $product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘name’, $productName); // Delete the product $product->delete(); ?> [/php]
Magento – Accessing, Getting and Using Custom Variables in PHTML
[php] // To get the TEXT value of the custom variable: Mage::getModel(‘core/variable’)->setStoreId(Mage::app()->getStore()->getId())->loadByCode(‘custom_variable_code’)->getValue(‘text’); // To get the HTML value of the custom variable: Mage::getModel(‘core/variable’)->setStoreId(Mage::app()->getStore()->getId())->loadByCode(‘custom_variable_code’)->getValue(‘html’); // The store id is set as Custom Variables can be edited for multiple stores [/php]
Magento Compilation – Compile, Clear, Enable and Disable from Command Line
[shell] $ php shell/compiler.php Usage: php -f compiler.php — [options] state Show Compilation State compile Run Compilation Process clear Disable Compiler include path and Remove compiled files enable Enable Compiler include path disable Disable Compiler include path help This help [/shell]
Magento – Inserting the Customer’s Email Address into the Email Templates
If you wish to see your customer’s email address in the order confirmation emails sent out by Magento, you will have to edit (or create a new template and assign it to new order emails) the new order confirmation email template and add this shortcode to it. [php] {{htmlescape var=$order.getCustomerEmail()}} [/php] This will display the…
Magento – Including OneStepCheckout’s Order Comments in the Email Templates
If you have installed the OneStepCheckout extension for your Magento store and wondering why you are not seeing the order comments (OneStepCheckout feature) in your order confirmation emails, your email template needs to be edited. You will have to edit (or create a new template and assign it to new order emails) the new order…
Magento – Get Logged In Customer’s Full Name, First Name, Last Name and Email Address
[php] // Check if any customer is logged in or not if (Mage::getSingleton(‘customer/session’)->isLoggedIn()) { // Load the customer’s data $customer = Mage::getSingleton(‘customer/session’)->getCustomer(); $customer->getPrefix(); $customer->getName(); // Full Name $customer->getFirstname(); // First Name $customer->getMiddlename(); // Middle Name $customer->getLastname(); // Last Name $customer->getSuffix(); // All other customer data $customer->getWebsiteId(); // ID $customer->getEntityId(); // ID $customer->getEntityTypeId(); // ID $customer->getAttributeSetId();…
How to Get the Magento CMS Page Identifier of the Current Page
To get the URL key / identifier of any CMS page in Magento, use the following bit of code. [php] <?php $cmsPageUrlKey = Mage::getSingleton(‘cms/page’)->getIdentifier(); ?> [/php] This will return the path that comes after the website’s URL. For example, the URL identifier for the About Us page might be about-us and not the full URL…
Magento – Easy Method to Set the Number of Columns in Product List.phtml Without XML
[php] <?php /* Edit the catalog/product/list.phtml template to include the following */ /* Get the layout’s page template */ $pageLayoutRootTemplate = $this->getLayout()->getBlock(‘root’)->getTemplate(); /* Set the column count based on the layout template used */ switch ($pageLayoutRootTemplate) { case ‘page/1column.phtml’: $_columnCount = 4; break; case ‘page/2columns-left.phtml’: $_columnCount = 3; break; case ‘page/2columns-right.phtml’: $_columnCount = 3; break;…
Magento – Returning JSON (for AJAX and API Calls) Response From Controller Action
Within your Magento Controller > Action you can use the below code to send a JSON response. [php] $this->getResponse()->setHeader(‘Content-type’, ‘application/json’); $this->getResponse()->setBody($jsonData); [/php] Read this post on how to use Magento’s core helper to encode and decode JSON.
Magento’s Core JSON Encoding and Decoding Functions
To JSON encode an array [php] $jsonData = Mage::helper(‘core’)->jsonEncode($array); [/php] To JSON decode an array [php] $array = Mage::helper(‘core’)->jsonDecode($jsonData); [/php]
Magento – How to Check if a Module is Installed and is Active
While developing Magento websites, you might wish to check to find out if a module is installed before proceeding to use that module’s functions. You can do so by using the following bit of code. [php] /* Returns boolean true or false */ Mage::getConfig()->getModuleConfig(‘Namespace_Modulename’)->is(‘active’, ‘true’); [/php]