Issue
I am doing a magento customization site I need to add certain product details to database so i have used the function addAction() in Mage checkout controller, inorder to make it a module I have to override the cartController's Addaction()
I have referenced the stackoverflow's overriding controller questions but no use it is using the coding in Mage checkout controllers
I have used below code,
code\local\SmartGrowth\CompatibleWith\etc
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<SmartGrowth_CompatibleWith>
<version>0.1.0</version>
</SmartGrowth_CompatibleWith>
</modules>
<!-- Configure our module's behavior in the global scope -->
<global>
<blocks>
<catalog>
<rewrite>
<product_view>SmartGrowth_CompatibleWith_Block_CompatibleWith</product_view>
</rewrite>
</catalog>
</blocks>
</global>
<frontend>
<routers>
<checkout>
<use>standard</use>
<modules>
<SmartGrowth_CompatibleWith_Checkout before="Mage_Checkout">SmartGrowth_CompatibleWith_Checkout</SmartGrowth_CompatibleWith_Checkout>
</modules>
</checkout>
</routers>
</frontend>
</config>
app\code\local\SmartGrowth\CompatibleWith\controllers\Checkout
require_once 'Mage/Checkout/controllers/CartController.php';
class SmartGrowth_CompatibleWith_Checkout_CartController extends Mage_Core_Controller_Front_Action
{
public function addAction()
{
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
$postData = Mage::app()->getRequest()->getPost();
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();
$cart_id = $cart->getQuote()->getId();
$product_id=Mage::helper('core')->escapeHtml($product->getId());
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT item_id FROM mage_sales_flat_quote_item WHERE quote_id='$cart_id' AND product_id='$product_id'");
$row = $readresult->fetch() ;
$item_id=$row['item_id'];
$brand = Mage::getSingleton('core/session')->getMyModelbrandData();
if($postData['check_value']==1)
{
$myData = Mage::getSingleton('core/session')->getMyModelTypeData();
$myDataver = Mage::getSingleton('core/session')->getMyModelVersionData();
}else if($postData['check_value']==2)
{
$model_type = $postData['model_type'];
$myDataver = Mage::getSingleton('core/session')->getMyModelVersionData();
}else if($postData['check_value']==3)
{
$model_version =$postData['model_version'];
$myData = Mage::getSingleton('core/session')->getMyModelTypeData();
}else if($postData['check_value']==4)
{
$model_type =$postData['model_type'];
$model_version =$postData['model_version'];
}
/*if($postData['model_type']!=''&& $postData['model_version']!='')
{
$model_type =$postData['model_type'];
$model_version =$postData['model_version'];
}else
{
$myData = Mage::getSingleton('core/session')->getMyModelTypeData();
$myDataver = Mage::getSingleton('core/session')->getMyModelVersionData();
}*/
if($myData !=''):
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT * FROM mage_eav_attribute_option_value WHERE option_id='$myData'");
$row = $readresult->fetch() ;
$model_type=$row['value'];
endif;
if($myDataver !=''):
$readresult1=$write->query("SELECT * FROM mage_eav_attribute_option_value WHERE option_id='$myDataver'");
$row1 = $readresult1->fetch() ;
$model_version=$row1['value'];
endif;
$write->beginTransaction();
$fields = array();
$fields['model_type'] = $model_type;
$fields['model_version'] = $model_version;
$fields['brand'] = $brand;
$where = $write->quoteInto('item_id =?', $item_id);
$write->update('mage_sales_flat_quote_item', $fields, $where);
$write->commit();
/**
* @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();
}
}
}
Solution
instead of using overriding method You can write observer for magento events .
code\local\SmartGrowth\CompatibleWith\etc
<frontend>
<events>
<checkout_cart_add_product_complete>
<observers>
<SmartGrowth_CompatibleWith_Model_Observer>
<type>singleton</type>
<class>SmartGrowth_CompatibleWith_Model_Observer</class>
<method>cartproductcomplete</method>
</SmartGrowth_CompatibleWith_Model_Observer>
</observers>
</checkout_cart_add_product_complete>
</events>
</frontend>
and write the observer code in code\local\SmartGrowth\CompatibleWith\Model\Observer.php
class SmartGrowth_CompatibleWith_Model_Observer {
public function cartproductcomplete() {
//Database operation
}
}
Is better to use events than overriding method
Answered By - Soumya Mohanan Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.