我刚开始使用Magento CE 1.9.1.0
在这里,我想覆盖Magento addAction(),因为我已经在我的自定义模块中添加了一些代码。
config.xml:
<frontend>
<routers>
<checkout>
<args>
<modules>
<Naresh_Customcheckout before="Mage_Checkout">Naresh_Customcheckout</Naresh_Customcheckout>
</modules>
</args>
</checkout>
</routers>
</frontend>
CartController.php:
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Naresh_Customcheckout_CartController extends Mage_Checkout_CartController {
public function addAction(){
if (!$this->_validateFormKey()) {
$this->_goBack();
return;
}
$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);
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
if ($params['new_cusom_options'] == 'add_new_gift') {
$quote_id1 = Mage::getSingleton('checkout/session')->getQuote()->getId();
$custom_weight = 0.600; // Its dynamic Value from DB
$query2 = "UPDATE sales_flat_quote_item SET `weight` = `weight` + ".$custom_weight.", `row_weight` = `row_weight` + ".$custom_weight." WHERE `quote_id` = ".$quote_id1." AND `product_id` = ".$params['product'];
$write->query($query2);
}
/**
* @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();
}
}
}
?>
在Cart控制器中,我想为产品增加额外的重量和row_weight。
我试图在更新查询之前和之后看到数据库值,结果是这样的。
$query1 = "SELECT `weight`, `row_weight` FROM `sales_flat_quote_item` WHERE `quote_id` = ".$cart_id." AND `product_id` = ".$_product->getId();
Mage::log($query1);
$results = $read->fetchAll($query1);
Mage::log($results[0]);
$query2 = "UPDATE sales_flat_quote_item SET `weight` = `weight` + ".$custom_weight.", `row_weight` = `row_weight` + ".$custom_weight." WHERE `quote_id` = ".$cart_id." AND `product_id` = ".$_product->getId();
Mage::log($query2);
$write->query($query2);
$query3 = "SELECT `weight`, `row_weight` FROM `sales_flat_quote_item` WHERE `quote_id` = ".$cart_id." AND `product_id` = ".$_product->getId();
Mage::log($query3);
$results1 = $read->fetchAll($query3);
Mage::log($results1[0]);
SYSTEM.LOG
2015-07-23T06:00:35+00:00 DEBUG (7): SELECT `weight`, `row_weight` FROM `sales_flat_quote_item` WHERE `quote_id` = 21 AND `product_id` = 1
2015-07-23T06:00:35+00:00 DEBUG (7): Array
(
[weight] => 0.5600
[row_weight] => 0.5600
)
2015-07-23T06:00:35+00:00 DEBUG (7): UPDATE sales_flat_quote_item SET `weight` = `weight` + 0.6, `row_weight` = `row_weight` + 0.6 WHERE `quote_id` = 21 AND `product_id` = 1
2015-07-23T06:00:35+00:00 DEBUG (7): SELECT `weight`, `row_weight` FROM `sales_flat_quote_item` WHERE `quote_id` = 21 AND `product_id` = 1
2015-07-23T06:00:35+00:00 DEBUG (7): Array
(
[weight] => 1.1600
[row_weight] => 1.1600
)
根据Query3,我可以看到值已成功更新,但在数据库中,值就像这样
由于该查询,我的数据库值没有得到更新,为什么会发生?
我希望以上几行没有错。
我不明白为什么会这样发生。
任何想法?
答案 0 :(得分:0)
试试这个。您没有正确包含$quote_id
变量
$query1 = 'SELECT * FROM sales_flat_quote_item WHERE quote_id = "'.$quote_id.'"';