|
<?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); |
|
} |
|
} |
Nice!
Actually you updates have been included but the current development version is in a beta stage, we had several requests for updates and new features, in the end it was decided that the extension should be reconstructed completely and incorporate its own tables for tracking orders etc.
I thought I had included this in the official release as well though but maybe I'm wrong, I will look it over and get it published right away, thank you and my apologies if this got neglected.
I will try to include as much of the above as possible in the upcoming version as well, as long as you are fine with me doing so? I've linked this on my todo list so I will come back here as soon as there is time to finish the beta version of the extension, which will most likely be during the second quarter of 2014.