Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Path : app\code\local\ABC\Checkout\controllers\CartController.php
- //http://magento.stackexchange.com/questions/45132/adding-a-product-to-the-cart-via-querystring-using-sku
- require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'CartController.php');
- class ABC_Checkout_CartController extends Mage_Checkout_CartController
- {
- protected function _initProduct()
- {
- $sku = $this->getRequest()->getParam('sku');
- $productId = (int) $this->getRequest()->getParam('product');
- if($sku){
- $params = $this->getRequest()->getParams();
- $productId = Mage::helper('checkout/cart')->getProductIdByParams($params, false);
- if(! $productId){
- return false;
- }
- }
- if ($productId) {
- $product = Mage::getModel('catalog/product')
- ->setStoreId(Mage::app()->getStore()->getId())
- ->load($productId);
- if ($product->getId()) {
- return $product;
- }
- }
- return false;
- }
- /**
- * Shopping cart display action
- */
- public function indexAction()
- {
- //remove last order id to prevent update order/status API again
- Mage::getModel('checkout/session')->unsLastOrderId();
- parent::indexAction();
- }
- /**
- * Custom Add product to shopping cart action
- */
- public function addAction()
- {
- $cart = $this->_getCart();
- $params = $this->getRequest()->getParams();
- if ($params["isAjax"] == 1) {
- $response = array();
- 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->loadLayout();
- $response["message"] = $message;
- if(Mage::getSingleton('core/session')->getSwitchView() == "desktop") {
- $response["right_slide"] = $this->getLayout()->getBlock('sp_help_rightslide')->toHtml();
- $response["sidebar"] = $this->getLayout()->getBlock('cart_sidebar')->toHtml();
- }else{
- $this->_getSession()->addSuccess($message);
- }
- }
- }
- } 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->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
- return;
- } else {
- return parent::addAction();
- }
- }
- /**
- * Delete shoping cart item action
- */
- public function deleteAction()
- {
- $id = (int) $this->getRequest()->getParam('id');
- if ($id) {
- try {
- $productName = "";
- foreach ($this->_getCart()->getItems() as $item) {
- if ($item->getId() == $id) {
- $productName = $item->getName();
- break;
- }
- }
- $this->_getCart()->removeItem($id)
- ->save();
- $message = $this->__('%s has already removed from your cart.', Mage::helper('core')->escapeHtml($productName));
- $this->_getSession()->addSuccess($message);
- } catch (Exception $e) {
- $this->_getSession()->addError($this->__('Cannot remove the item.'));
- Mage::logException($e);
- }
- }
- $this->_redirectReferer(Mage::getUrl('*/*'));
- }
- /**
- * Update shopping cart data action
- */
- public function updatePostAction()
- {
- $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
- $redirect_url = (string)$this->getRequest()->getParam('redirect_url');
- switch ($updateAction) {
- case 'empty_cart':
- $this->_emptyShoppingCart();
- break;
- case 'update_qty': // back to shopping cart
- //refresh stock - call connector api - get stock qty real time
- Mage::getModel('abc_stock/data')->refreshStockOnepage();
- $this->_updateShoppingCart();
- break;
- case 'continue_shopping': // go to product page
- $this->_updateShoppingCart();
- break;
- default: // go to checkout page
- //refresh stock - call connector api - get stock qty real time
- Mage::getModel('abc_stock/data')->refreshStockOnepage();
- list($stock_status, $total_items_has_stock, $total_items) = Mage::getModel('abc_stock/data')->checkStockOnepage();
- if($total_items_has_stock != $total_items){
- Mage::getSingleton('customer/session')->setIsAllStockAvailable(FALSE);
- Mage::getSingleton('checkout/session')->addError($this->__('Sorry, you can not place an order at this time. Please try again later.'));
- $this->_redirectUrl(Mage::getUrl('checkout/cart'));
- return;
- }else{
- $this->_updateShoppingCart();
- }
- }
- if($redirect_url){
- $this->_redirectUrl($redirect_url);
- }else{
- $this->_goBack();
- }
- }
- public function addMassAction() {
- $product_list = $this->getRequest()->getPost("product_list");
- $products = explode(",", $product_list);
- $cart = Mage::getModel('checkout/cart');
- $cart->init();
- /* @var $pModel Mage_Catalog_Model_Product */
- foreach ($products as $product_id) {
- if ($product_id == '') {
- continue;
- }
- $pModel = Mage::getModel('catalog/product')->load($product_id);
- try {
- $cart->addProduct($pModel, array('qty' => '1'));
- } catch (Exception $e) {
- continue;
- }
- }
- $cart->save();
- if ($this->getRequest()->isXmlHttpRequest()) {
- exit('1');
- }
- $this->_redirect('checkout/cart');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement