About Kien Wiliam
Magento Ecommerce Developer
Posts by Kien Wiliam
Magento Ecommerce Developer
NameSpace : MyPackage and Module: MyModule Module configuration location : app/etc/modules/MyPackage_MyModule.xml
1 2 3 4 5 6 7 8 |
<config> <modules> <MyPackage_MyModule> <active>true</active> <codePool>local</codePool> </MyPackage_MyModule> </modules> </config> |
Create config file for this module location : app/code/local/MyPackage/MyModule/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<config> <modules> <MyPackage_MyModule> <version>0.0.0.1</version> </MyPackage_MyModule> </modules> <global> <models> <mymodule> <class>MyPackage_MyModule_Model</class> </mymodule> </models> <events> <controller_action_predispatch_contacts_index_post> <observers> <mymodule> <class>mymodule/observer</class> <method>checkContacts</method> </mymodule> </observers> </controller_action_predispatch_contacts_index_post> </events> </global> <default> <captcha> <frontend> <areas> <contacts> <label>Contacts Page</label> </contacts> </areas> </frontend> </captcha> <customer> <captcha> <always_for> <contacts>1</contacts> </always_for> </captcha> </customer> </default> </config> |
Create a observer for that location: app/code/local/MyPackage/MyModule/Model/Observer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class MyPackage_MyModule_Model_Observer { public function checkContacts($observer){ $formId = 'contacts'; $captchaModel = Mage::helper('captcha')->getCaptcha($formId); if ($captchaModel->isRequired()) { $controller = $observer->getControllerAction(); $word = $this->_getCaptchaString($controller->getRequest(), $formId); if (!$captchaModel->isCorrect($word)) { Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.')); $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true); $url = Mage::getUrl('contacts'); $controller->getResponse()->setRedirect($url); } } return $this; } /** * Get Captcha String * * @param Varien_Object $request * @param string $formId * @return string */ protected function _getCaptchaString($request, $formId) { $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE); return $captchaParams[$formId]; } } |
Create a local.xml to your active theme inside layout folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<layout version="0.1.0"> <contacts_index_index> <reference name="contactForm"> <action method="setTemplate"><template>mymodule/contacts/form.phtml</template></action> <block type="core/text_list" name="form.additional.info"> <block type="captcha/captcha" name="captcha"> <reference name="head"> <action method="addJs"><file>mage/captcha.js</file></action> </reference> <action method="setFormId"><formId>contacts</formId></action> <action method="setImgWidth"><width>230</width></action> <action method="setImgHeight"><width>50</width></action> </block> </block> </reference> </contacts_index_index> |
Now copy contacts/form.phtml to mymodule/contacts/form.phtml, add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php echo $this->getChildHtml('form.additional.info'); ?> to <form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="b_default_layout"> <ul class="form-list"> <li class="fields row"> <div class=" col-lg-6 col-md-6 col-sm-6 m_bottom_15"> <label for="name" class="second_font required d_inline_b m_bottom_5 clickable"><?php echo Mage::helper('contacts')->__('First Name') ?></label> <div class="input-box"> <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry tr_all w_full fw_light" type="text" /> </div> </div> <div class=" col-lg-6 col-md-6 col-sm-6 m_bottom_15"> <label for="email" class="second_font required d_inline_b m_bottom_5 clickable"><?php echo Mage::helper('contacts')->__('Email Address') ?></label> <div class="input-box"> <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email tr_all w_full fw_light" type="text" /> </div> </div> </li> <li> <label for="telephone" class="second_font d_inline_b m_bottom_5 clickable"><?php echo Mage::helper('contacts')->__('Telephone') ?></label> <div class="input-box"> <input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text tr_all w_full fw_light" type="text" /> </div> </li> <li class="wide"> <label for="comment" class=" second_font d_inline_b m_bottom_5 clickable required"><?php echo Mage::helper('contacts')->__('Message') ?></label> <div class="input-box"> <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text tr_all w_full fw_light" cols="5" rows="3"></textarea> </div> </li> </ul> <?php echo $this->getChildHtml('form.additional.info'); ?> <div class="buttons-set"> <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" /> <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button button_type_2 black state_2 tr_all second_font fs_medium tt_uppercase d_inline_b"><span><span class="m_left_10 m_right_10 d_inline_b"><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button> </div> </form> |
Clear cache. Now Go to System -> Configuration […]
To add translation csv file in magento2, you just need to add a folder named: “i18n” inside your module’s folder, i.e. app/code/Magebay/TestModule/i18n Then create a csv file with name of language code. forexample: for english (United state) => en_US.csv french (canada)=>fr_CA.csv Inside csv file you just need to add words which you want to translate, […]
You can always use CLI command for managing modules. Disable module:
1 |
$ bin/magento module:disable Vendor_ModuleName |
Enable module:
1 |
$ bin/magento module:enable Vendor_ModuleName |
And you can check status of all modules:
1 |
$ bin/magento module:status |
It is useful to build a habit of controlling your installation via bin/magento. Also it will help to avoid any new features introduced for module commands. Hope it helps !
Magento 1 :
1 2 3 4 5 6 |
//get shipping method Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod() //get shipping descriptions Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription(); //get shipping data Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData(); |
Magento 2 : You can do this by calling the \Magento\Checkout\Model\Session. Add it to your constructor and get it by using this code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protected $_checkoutSession; public function __construct( \Magento\Checkout\Model\Session $checkoutSession ){ $this->_checkoutSession = $checkoutSession; } //call you code in the methos somewehre like this : //get shipping method $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod(); //get shipping descriptions $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingDescription(); //get shipping data $this->_checkoutSession->getQuote()->getShippingAddress()->getData(); |
Or you can get by Object Manager Instance by using this code :
1 2 3 4 5 6 7 8 9 10 |
//Get Object Manager Instance $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //Get checkout Session by Object Manager Instance $checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session'); //get shipping method $checkoutSession->getQuote()->getShippingAddress()->getShippingMethod(); //get shipping descriptions $checkoutSession->getQuote()->getShippingAddress()->getShippingDescription(); //get shipping data $checkoutSession->getQuote()->getShippingAddress()->getData(); |
Thank for reading this post, Hope it helps.
Magento 1 : The code for change price of product, you can put on controller, model, helper or any where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//load product by id $product = Mage::getModel('catalog/product')->load($productId); //change base price if($product->getId()){ //increase base price + 5 $newPrice = $product->getPrice()+5; $product->setPrice($newPrice); $product->save(); } //change special price if($product->getSpecialPrice()){ //increase special price + 5 $newPrice = $product->getSpecialPrice()+5; $product->setSpecialPrice($newPrice); $product->save(); } |
Magento 2 : To change price of product you need to follow these functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//instance of object manager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //create product model via object manager $product = $objectManager->create('Magento\Catalog\Model\Product'); //load product by id $product->load($productId) //change base price if($product->getId()){ //increase base price + 5 $newPrice = $product->getPrice()+5; $product->setPrice($newPrice); $product->save(); } //change special price if($product->getSpecialPrice()){ //increase special price + 5 $newPrice = $product->getSpecialPrice()+5; $product->setSpecialPrice($newPrice); $product->save(); } |
Thank for reading this post, Hope it helps.
Magento 1 : The code for delete shopping cart items for logged in customer:
1 2 3 4 5 6 7 8 9 |
$cart = Mage::getSingleton('checkout/cart'); $quoteItems = Mage::getSingleton('checkout/session') ->getQuote() ->getItemsCollection(); foreach( $quoteItems as $item ){ $cart->removeItem( $item->getId() ); } $cart->save(); |
The code for delete shopping cart items for all customers:
1 2 3 4 5 6 7 |
$quoteCollection = Mage::getModel('sales/quote') ->getCollection() ->addFieldToFilter('is_active', 1); foreach ($quoteCollection as $item) { $item->delete(); } |
If you have a large number of customers quotes then deleting them by using loop might be time and resource consuming. You can clear/delete all customers cart items (all […]