Tag Archives: Controller

Magento 2 Routing Rewrite Controller

We can rewrite controller with the help of router. Magento provides the attribute before/after to config the module sort order which define what module controller will be find first.

For example, if we want to rewrite the controller customer/account/login, we will define more route in the route.xml like this: Continue reading Magento 2 Routing Rewrite Controller

Magento Override Controller

Here we are overing the function addAction in CartController.php

1) Chnage the directory to magento installtion

2) vi app/etc/modules/Phpcodez_Checkout.xml
<?xml version=”1.0″?>
<config>
<modules>
<Phpcodez_Checkout>
<active>true</active>
<codePool>local</codePool>
</Phpcodez_Checkout>
</modules>
</config>

3)vi app/code/local/Phpcodez/Checkout/etc/config.xml

<?xml version=”1.0″?>
<config>

<modules>
<Phpcodez_Checkout>
<version>0.1.0</version>
</Phpcodez_Checkout>
</modules>

<frontend>
<routers>
<checkout>
<args>
<modules>
<phpcodez_checkout before=”Mage_Checkout”>Phpcodez_Checkout</phpcodez_checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>

</config>

3) vi app/code/local/Phpcodez/Checkout/controllers/CartController.php

<?php
require_once Mage::getModuleDir(‘controllers’, ‘Mage_Checkout’).DS.’CartController.php’;

class Phpcodez_Checkout_CartController extends Mage_Checkout_CartController{

public function addAction() {
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params[‘qty’])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array(‘locale’ => Mage::app()->getLocale()->getLocaleCode())
);
$params[‘qty’] = $filter->filter($params[‘qty’]);
}

$product = $this->_initProduct();
$related = $this->getRequest()->getParam(‘related_product’);

/**
* Check product availability
*/
if (!$product) {
$this->_goBack();
return;
}

$cart->addProduct($product, $params);
if (!empty($related)) {
$cart->addProductsByIds(explode(‘,’, $related));
}

$cart->save();

$this->_getSession()->setCartWasUpdated(true);

/**
* @todo remove wishlist observer processAddToCart
*/
Mage::dispatchEvent(‘checkout_cart_add_product_complete’,
array(‘product’ => $product, ‘request’ => $this->getRequest(), ‘response’ => $this->getResponse())
);

if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()){
$message = $this->__(‘%s was added to your shopping cart.’, Mage::helper(‘core’)->escapeHtml($product->getName()));
$this->_getSession()->addSuccess($message);
}
$this->_goBack();
}
} catch (Mage_Core_Exception $e) {
if ($this->_getSession()->getUseNotice(true)) {
$this->_getSession()->addNotice(Mage::helper(‘core’)->escapeHtml($e->getMessage()));
} else {
$messages = array_unique(explode(“n”, $e->getMessage()));
foreach ($messages as $message) {
$this->_getSession()->addError(Mage::helper(‘core’)->escapeHtml($message));
}
}

$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
$this->getResponse()->setRedirect($url);
} else {
$this->_redirectReferer(Mage::helper(‘checkout/cart’)->getCartUrl());
}
} catch (Exception $e) {
$this->_getSession()->addException($e, $this->__(‘Cannot add the item to shopping cart.’));
Mage::logException($e);
$this->_goBack();
}
}

}
?>