|
<?php |
|
|
|
# app/code/local/MYMOD_ReorderProduct/controllers/IndexController.php |
|
|
|
/** |
|
* Simple module to extend/customize the MageParts_ReorderProduct module |
|
* |
|
* @category MYMOD |
|
* @package MYMOD_ReorderProduct |
|
* @copyright Copyright (c) 2012 August Ash, Inc. (http://www.augustash.com) |
|
* @author Josh Johnson (August Ash) |
|
*/ |
|
|
|
require_once('MageParts/ReorderProduct/controllers/IndexController.php'); |
|
|
|
class MYMOD_ReorderProduct_IndexController extends MageParts_ReorderProduct_IndexController |
|
{ |
|
|
|
/** |
|
* placeholder for our MYMOD_ReorderProduct_Helper_Data instance |
|
* |
|
* @var MYMOD_ReorderProduct_Helper_Data |
|
*/ |
|
protected $_helper; |
|
|
|
/** |
|
* Actions to be dispatched before running this controller |
|
*/ |
|
public function preDispatch() |
|
{ |
|
parent::preDispatch(); |
|
|
|
$this->_helper = Mage::helper('mymod_reorderproduct'); |
|
} |
|
|
|
/** |
|
* == indexAction |
|
* |
|
* Display list of products available for reordering |
|
* |
|
* ------------------------------------------------------------------------- |
|
* OVER RIDES: |
|
* + use our MYMOD_ReorderProduct_Helper methods for comparing magento |
|
* versions |
|
* ------------------------------------------------------------------------- |
|
* @return Zend_View |
|
*/ |
|
public function indexAction() |
|
{ |
|
$this->loadLayout(); |
|
|
|
$session = Mage::getSingleton('customer/session'); |
|
$block = $this->getLayout()->getBlock('customer.reorderproduct'); |
|
$referer = $session->getAddActionReferer(true); |
|
|
|
if ($block) { |
|
$block->setRefererUrl($this->_getRefererUrl()); |
|
if ($referer) { |
|
$block->setRefererUrl($referer); |
|
} |
|
} |
|
|
|
$this->_initLayoutMessages('customer/session'); |
|
$this->_initLayoutMessages('checkout/session'); |
|
$this->_initLayoutMessages('catalog/session'); |
|
|
|
/** |
|
* if the Magento version is less than ('lt') 1.4 than do it the old way |
|
*/ |
|
if ($this->_helper->currentVersionBelow('1.4')) { |
|
$this->getLayout()->getBlock('content')->append( |
|
$this->getLayout()->createBlock($block) |
|
); |
|
} |
|
|
|
$this->renderLayout(); |
|
} |
|
|
|
/** |
|
* Add single product to shopping cart |
|
* |
|
* ------------------------------------------------------------------------- |
|
* OVER RIDES: |
|
* + use our MYMOD_ReorderProduct_Helper methods for comparing magento |
|
* versions |
|
* ------------------------------------------------------------------------- |
|
* @return Zend_View |
|
*/ |
|
public function addAction() |
|
{ |
|
// get helper object (using our custom Hock_ReorderProduct_Helper) |
|
$helper = $this->_helper; |
|
|
|
// setup default redirect url |
|
$redirectUrl = Mage::getUrl('*/*'); |
|
|
|
/* @var $session Mage_Customer_Model_Session */ |
|
$session = Mage::getSingleton('customer/session'); |
|
|
|
// make sure that the extension is enabled before we continue |
|
if (!$helper->getIsEnabled()) { |
|
return $this->_redirect('*/*'); |
|
} |
|
|
|
// get Magento version (moved this logic to our helper methods) |
|
// $magentoVersion = $helper->getMagentoVersion(); |
|
|
|
try { |
|
// get product id from parameter |
|
$productId = (int) $this->getRequest()->getParam('product'); |
|
|
|
/* @var $product Mage_Catalog_Model_Product */ |
|
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId); |
|
|
|
// make sure that the requested product exists |
|
if (!$product->getId()) { |
|
return $this->_redirect('*/*'); |
|
} |
|
|
|
// shopping cart parameters |
|
$cartParams = array(); |
|
|
|
// get requested qty |
|
$qty = $this->getRequest()->getParam('qty'); |
|
|
|
if ($helper->currentVersionBelow('1.4')) { |
|
$cartParams['qty'] = (!is_null($qty) && !empty($qty)) |
|
? floatval($qty) |
|
: ($product->getStockItem()->getMinSaleQty() * 1); |
|
} |
|
else { |
|
$filter = new Zend_Filter_LocalizedToNormalized( |
|
array('locale' => Mage::app()->getLocale()->getLocaleCode()) |
|
); |
|
|
|
$cartParams['qty'] = (!is_null($qty) && !empty($qty)) |
|
? $filter->filter(floatval($qty)) |
|
: $filter->filter(($product->getStockItem()->getMinSaleQty() * 1)); |
|
} |
|
|
|
// get previous buy request information |
|
$prevBuyRequestInfo = $this->getRequest()->getParam('productPrevBuyInfo'); |
|
|
|
if (!is_null($prevBuyRequestInfo) && !empty($prevBuyRequestInfo)) { |
|
$cartParams = array_merge($cartParams, unserialize($prevBuyRequestInfo)); |
|
} |
|
|
|
/* @var $cart Mage_Checkout_Model_Cart */ |
|
$cart = Mage::getSingleton('checkout/cart'); |
|
|
|
// add product to cart |
|
$cart->addProduct($product, $cartParams); |
|
$cart->save(); |
|
|
|
// get correct redirect url |
|
if (($helper->currentVersionBelow('1.4')) || Mage::helper('checkout/cart')->getShouldRedirectToCart()) { |
|
$redirectUrl = Mage::helper('checkout/cart')->getCartUrl(); |
|
} else if ($this->_getRefererUrl()) { |
|
$redirectUrl = $this->_getRefererUrl(); |
|
} |
|
} catch (Mage_Core_Exception $e) { |
|
// NOT SALEABLE |
|
if ($e->getCode() == MageParts_ReorderProduct_Model_Product::EXCEPTION_CODE_NOT_SALABLE) { |
|
$session->addError($helper->__('This product(s) is currently out of stock')); |
|
} else { |
|
$session->addError($helper->__('There was an error while adding the item(s) to the shopping cart: %s', $e->getMessage())); |
|
} |
|
} catch (Exception $e) { |
|
if ($helper->currentVersionBelow('1.4')) { |
|
$session->addError($helper->__('There was an error while adding the item(s) to the shopping cart.')); |
|
} |
|
else { |
|
$session->addException($e, $helper->__('There was an error while adding the item(s) to the shopping cart.')); |
|
} |
|
} |
|
|
|
return $this->_redirectUrl($redirectUrl); |
|
} |
|
|
|
/** |
|
* Add multiple products to shopping cart |
|
* |
|
* ------------------------------------------------------------------------- |
|
* OVER RIDES: |
|
* + use our MYMOD_ReorderProduct_Helper methods for comparing magento |
|
* versions |
|
* ------------------------------------------------------------------------- |
|
* @return Zend_View |
|
*/ |
|
public function cartAction() |
|
{ |
|
// get helper object |
|
$helper = $this->_helper; |
|
|
|
// setup default redirect url |
|
$redirectUrl = Mage::getUrl('*/*'); |
|
|
|
/* @var $session Mage_Customer_Model_Session */ |
|
$session = Mage::getSingleton('customer/session'); |
|
|
|
// make sure that the extension is enabled before we continue |
|
if (!$helper->getIsEnabled()) { |
|
return $this->_redirect('*/*'); |
|
} |
|
|
|
// get Magento version (moved this logic to our helper) |
|
// $magentoVersion = $helper->getMagentoVersion(); |
|
|
|
try { |
|
/* @var $cart Mage_Checkout_Model_Cart */ |
|
$cart = Mage::getSingleton('checkout/cart'); |
|
|
|
// get products to add to shopping cart |
|
$products = $this->getRequest()->getParam('product'); |
|
|
|
// get product ids |
|
$productIds = $this->getRequest()->getParam('productId'); |
|
|
|
// get previous buy request information |
|
$prevBuyRequestInfo = $this->getRequest()->getParam('productPrevBuyInfo'); |
|
|
|
if (!count($products) || !count($productIds)) { |
|
return $this->_redirect('*/*'); |
|
} |
|
|
|
$filter = null; |
|
|
|
if ($helper->currentVersionAbove('1.4', '>=')) { |
|
$filter = new Zend_Filter_LocalizedToNormalized( |
|
array('locale' => Mage::app()->getLocale()->getLocaleCode()) |
|
); |
|
} |
|
|
|
foreach (array_keys($products) as $itemId) { |
|
$productId = isset($productIds[$itemId]) ? $productIds[$itemId] : null; |
|
|
|
/* @var $product Mage_Catalog_Model_Product */ |
|
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId); |
|
|
|
// make sure that the requested product exists |
|
if (!$product->getId()) { |
|
$session->addError($helper->__('Product with id %s no longer exist', $productId)); |
|
continue; |
|
} |
|
|
|
// make sure that the product is saleable |
|
if (!$product->isSaleable()) { |
|
$session->addError($helper->__('Product %s is currently out of stock', $product->getName())); |
|
continue; |
|
} |
|
|
|
// shopping cart parameters |
|
$cartParams = array(); |
|
|
|
// get requested qty |
|
$qtys = $this->getRequest()->getParam('qty'); |
|
|
|
if (is_null($filter)) { |
|
if (isset($qtys[$itemId])) { |
|
$cartParams['qty'] = floatval($qtys[$itemId]); |
|
} |
|
else { |
|
$cartParams['qty'] = ($product->getStockItem()->getMinSaleQty() * 1); |
|
} |
|
} |
|
else { |
|
if (isset($qtys[$itemId])) { |
|
$cartParams['qty'] = $filter->filter(floatval($qtys[$itemId])); |
|
} |
|
else { |
|
$cartParams['qty'] = $filter->filter(($product->getStockItem()->getMinSaleQty() * 1)); |
|
} |
|
} |
|
|
|
if (isset($prevBuyRequestInfo[$itemId])) { |
|
$cartParams = array_merge($cartParams, unserialize($prevBuyRequestInfo[$itemId])); |
|
} |
|
|
|
// add product to cart |
|
$cart->addProduct($product, $cartParams); |
|
} |
|
|
|
// save shopping cart |
|
$cart->save(); |
|
|
|
// get correct redirect url |
|
if (($helper->currentVersionBelow('1.4')) || Mage::helper('checkout/cart')->getShouldRedirectToCart()) { |
|
$redirectUrl = Mage::helper('checkout/cart')->getCartUrl(); |
|
} else if ($this->_getRefererUrl()) { |
|
$redirectUrl = $this->_getRefererUrl(); |
|
} |
|
} catch (Mage_Core_Exception $e) { |
|
$session->addError($helper->__('There was an error while adding the item(s) to the shopping cart: %s', $e->getMessage())); |
|
} catch (Exception $e) { |
|
if ($helper->currentVersionBelow('1.4')) { |
|
$session->addError($helper->__('There was an error while adding the item(s) to the shopping cart.')); |
|
} |
|
else { |
|
$session->addException($e, $helper->__('There was an error while adding the item(s) to the shopping cart.')); |
|
} |
|
} |
|
|
|
return $this->_redirectUrl($redirectUrl); |
|
} |
|
} |
No problem at all. We use this module fairly often so I was just documenting it to make it repeatable for the other developers in our company or others who may benefit. Use as much or as little of it as you want.