Posts

Showing posts from 2016

Running cron in magento

Setting cron In your command line use this command sudo crontab -e //add your cron */1 * * * * /bin/sh /var/www/html/canon_2016/cron.sh >> /var/log/myjob.log 2>&1 sudo crontab -l (shows the list of cron) To test cron is running or not  try {     Mage::getConfig()->init()->loadEventObservers('crontab');     Mage::app()->addEventArea('crontab');     Mage::dispatchEvent('default'); $log = fopen(__FILE__.'.log', 'a'); fwrite($log, date("Y-m-d H:i:s").PHP_EOL); fclose($log); } catch (Exception $e) {     Mage::printException($e); } place this code at the bottom of cron.php file in magento root directory,it will create cron.php.log file,which will show when the cron job start

Magento 2 CSS and JavaScript not loading

If you are facing problem of css and js page load design after installation in magento2 please follow the following step-: open the terminal and navigate to magento web root  $ cd /var/www/html/magento2  Step 1.  $ php bin/magento setup:static-content:deploy  Step 2.  $ php bin/magento indexer:reindex Step 3. make sure apache “rewrite_module” is enable and then restart the server Step 4.  $ chown -R www-data:www-data /var/www/html/magento2  Step 5.  $ chmod -R 777 /var/www/html/magento2  Step 6. delete cache folder under var/cache The above step working. I hope this will work for you also. Command to change the mode (Developer,Production..etc) open the terminal and navigate to magento web root $ php bin/magento deploy:mode:show $ php bin/magento deploy:mode:set production

Event and Observers in Magento2

To create observer in magento2, first we need to define observer in file: app/code/Rahul/HelloWorld/etc/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">     <event name="controller_action_catalog_product_save_entity_after">         <observer name="Rahul_HelloWorld_Product_Save_After" instance="Rahul\HelloWorld\Observer\ProductSaveAfter" />     </event> </config> Note: There are different places to create files for different handlers.     To create observer for frontend you can create file under : app/code/Rahul/HelloWorld/etc/frontend/event.xml     To create observer for frontend you can create file under : app/code/Rahul/HelloWorld/etc/adminhtml/event.xml     To create observer for both end, you need to cre...

Create log file in Magento2

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Your text message'); You can also print PHP objects and arrays like below : $logger->info(print_r($yourArray, true));

Create a simple module in Magento2

I have created a module with Namespace as Rahul and Module Name as HelloWorld Step1: Create a module.xml file in app/code/Rahul/HelloWorld/etc <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">     <module name="Rahul_HelloWorld" setup_version="1.0.0">     </module> </config> Step2: Create app/code/Rahul/HelloWorld/registration.php file <?php \Magento\Framework\Component\ComponentRegistrar::register(     \Magento\Framework\Component\ComponentRegistrar::MODULE,     'Rahul_HelloWorld',     __DIR__ ); Step3: Create a frontend router in app/code/Rahul/HelloWorld/etc/frontend/routes.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:...

Magento Registry: Mage::register

Magento registry is a great way of sharing information anywhere in your Magento store as they are static function.In other words Magento registry implies creation of new global variables which can be accessed anywhere within your Magento store. I have been wondering always,where is all the registry data stored in Magento ? I know that even singleton objects are stored in the registry. And registry is just a static array variable of the Mage class. Is registry different for different users ? I mean is it created on a per-user (per HTTP request) basis? For example,     Mage::register('foo', 'Hello World'); //set a value for foo     Mage::registry('foo'); //will this return hello world Is registry data stored in sessions ? If not how will Magento identify which user invoked the registry data ? <==============================================>       Magento Registry stored in the application’s memory,when ever your scri...

Magento orders states and statuses

Image
Each state can have one or several statuses and a status can have only one state. By default, statuses and states have often the same name, that is why it is a little confusing. Here is the list of statuses and states available by default. $order = Mage::getModel('sales/order')->loadByIncrementId('100000001'); $state = 'processing'; $status = 'Payment After Cancelled'; $comment = 'Changing state to Processing and status to Payment After Cancelled '; $isCustomerNotified = false; $order->setState($state, $status, $comment, $isCustomerNotified); $order->save(); $status can also take false value in order to only set order state, or true value for setting status by taking first status associated to this state. You can now adjust as you wish your order workflow in Magento.

Output raw SQL query from Magento collection

$collection = Mage::getResourceModel('rp/organisation_collection')                 ->searchByPostcodeLastname($postcode, Slastname)                 ->addFieldToSelect(array('organisation_id'))                 ->setPageSize(1);                                 Mage::log($collection->getSelect()->__toString(),true,'sqlquery.log')

Redirect on Observer file Magento

                $url = Mage::getUrl('checkout/onepage');                 Mage::getSingleton('core/session')->addError('Please Log In..');                 Mage::app()->getFrontController()->getResponse()->setRedirect($url);                 Mage::app()->getResponse()->sendResponse();                 exit ;

To apply the patch using SSH in Magento

Step 1: Place patches files into Magento Root directory,Upload your files into Magento root directory. It is important to place patch files directly into Magento root directory and execute it also directly in Magento root directory. $ ls -1 PATCH_SUPEE-7405_CE_1.9.2.2_v1-2016-01-20-04-35-33.sh app cron.php downloader errors favicon.ico index.php js lib mage media pkginfo robots.txt shell skin var Step 2:Run the patches $ bash ./PATCH_SUPEE-7405_CE_1.9.2.2_v1-2016-01-20-04-35-33.sh Checking if patch can be applied/reverted successfully... Patch was applied/reverted successfully. Note: If you get this error:- Hunk #1 FAILED at 695 (different line endings). 1 out of 1 hunk FAILED checking file app/code/core/Mage/Api/Model/Server/Adapter/Soap.php Hunk #1 FAILED at 208 (different line endings). Run this comand on your terminal find . -type f -exec dos2unix {} \; This will recursively look for files from the current directory and invoke dos2unix command on them and then run the patches $ ba...

How to get protected property of object in Magento (PHP)

2016-04-25T06:52:32+00:00 ALERT (1): Mage_Sales_Model_Order Object (     [_eventPrefix:protected] => sales_order     [_eventObject:protected] => order     [_addresses:protected] =>     [_items:protected] =>     [_payments:protected] =>     [_statusHistory:protected] =>     [_invoices:protected] =>     [_tracks:protected] =>     [_shipments:protected] =>     [_creditmemos:protected] =>     [_relatedObjects:protected] => Array         (         )     [_orderCurrency:protected] =>     [_baseCurrency:protected] =>     [_actionFlag:protected] => Array         (         )   ...

To find the layout name in Magento

<?php    $oRequest        = Mage::app()->getRequest();     $sModuleName     = $oRequest->getModuleName();     $sControllerName = $oRequest->getControllerName();     $sAtionName      = $oRequest->getActionName();     echo 'Current Page Layout Name:<br/>';     echo '"'.$sModuleName.'_'.$sControllerName.'_'.$sAtionName.'"';    ?> Now you can use the call the layout name on your xml file <?xml version="1.0"?>     <layout version="0.1.0">      <catalog_product_view>         <reference name="content">              ....yourcode.....          </reference>     </catalog_product_view>  ...

Magento – How to Add color picker in Magento Admin Configuration Page

1. In adminhtml/default/default/layout directory, create the layout XML file for your module. Lets name it as yourmodule.xml .Write following content in yourmodule..xml:   <?xml version="1.0"?> <layout version="0.1.1"> <default>         <reference name="head">             <action method="addJs"><file>YOURMODULE/jscolor.js</file></action>         </reference>     </default> </layout> 2. In your module's system.xml, add the color picker field as: <background translate="label tooltip comment"> <label>Background Color</label>    <frontend_type>text</frontend_type>    <validate>color</validate>    <sort_order>2</sort_order>    <show_in_default>1</show_in_default>  ...

How to remove Price from Options in Magento

Place this script in view.phtml file <script>     jQuery(document).ready(function() {        checkoptions();     jQuery("#product-options-wrapper select").change(function(){         checkoptions();     });     function checkoptions(){         jQuery("#product-options-wrapper select option").each(function(){             var optiontext = jQuery(this).text();             var addsignpos = optiontext.indexOf('+');             var subtractsignpos = optiontext.indexOf('-');             if(addsignpos>0){                 var result = optiontext.substring(0,addsignpos-1);  ...

Magento Get Add to Cart URL of any Product

$_productId = '33' ; $_product = Mage:: getModel ( 'catalog/product' ) -> load ( $_productId ) ; $_url = Mage:: helper ( 'checkout/cart' ) -> getAddUrl ( $_product ) ;