Example #1
0
 /**
  * Update wishlist Item and set data from request
  *
  * $params sets how current item configuration must be taken into account and additional options.
  * It's passed to Mage_Catalog_Helper_Product->addParamsToBuyRequest() to compose resulting buyRequest.
  *
  * Basically it can hold
  * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs), so they won't
  *   intersect with other submitted options
  *
  * For more options see Mage_Catalog_Helper_Product->addParamsToBuyRequest()
  *
  * @param int $itemId
  * @param Varien_Object $buyRequest
  * @param null|array|Varien_Object $params
  * @return Mage_Wishlist_Model_Wishlist
  *
  * @see Mage_Catalog_Helper_Product::addParamsToBuyRequest()
  */
 public function updateItem($itemId, $buyRequest, $params = null)
 {
     $item = $this->getItem((int) $itemId);
     if (!$item) {
         Mage::throwException(Mage::helper('wishlist')->__('Cannot specify wishlist item.'));
     }
     $product = $item->getProduct();
     $productId = $product->getId();
     if ($productId) {
         if (!$params) {
             $params = new Varien_Object();
         } else {
             if (is_array($params)) {
                 $params = new Varien_Object($params);
             }
         }
         $params->setCurrentConfig($item->getBuyRequest());
         $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($buyRequest, $params);
         $product->setWishlistStoreId($item->getStoreId());
         $resultItem = $this->addNewItem($product, $buyRequest, true);
         /**
          * Error message
          */
         if (is_string($resultItem)) {
             Mage::throwException(Mage::helper('checkout')->__($resultItem));
         }
         if ($resultItem->getId() != $itemId) {
             if ($resultItem->getDescription() != $item->getDescription()) {
                 $resultItem->setDescription($item->getDescription())->save();
             }
             $item->isDeleted(true);
             $this->setDataChanges(true);
             $items = $this->getItemCollection();
             foreach ($items as $_item) {
                 if ($_item->getProductId() == $productId && $_item->getId() != $resultItem->getId()) {
                     if ($resultItem->compareOptions($resultItem->getOptions(), $_item->getOptions())) {
                         $resultItem->setQty($resultItem->getQty() + $_item->getQty());
                         $_item->isDeleted(true);
                     }
                 }
             }
         } else {
             $resultItem->setQty($buyRequest->getQty() * 1);
         }
     } else {
         Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
     }
     return $this;
 }
Example #2
0
 /**
  * Update wishlist Item and set data from request
  *
  * $params sets how current item configuration must be taken into account and additional options.
  * It's passed to Mage_Catalog_Helper_Product->addParamsToBuyRequest() to compose resulting buyRequest.
  *
  * Basically it can hold
  * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs), so they won't
  *   intersect with other submitted options
  *
  * For more options see Mage_Catalog_Helper_Product->addParamsToBuyRequest()
  *
  * @param int|Mage_Wishlist_Model_Item $itemId
  * @param Varien_Object $buyRequest
  * @param null|array|Varien_Object $params
  * @return Mage_Wishlist_Model_Wishlist
  *
  * @see Mage_Catalog_Helper_Product::addParamsToBuyRequest()
  */
 public function updateItem($itemId, $buyRequest, $params = null)
 {
     $item = null;
     if ($itemId instanceof Mage_Wishlist_Model_Item) {
         $item = $itemId;
     } else {
         $item = $this->getItem((int) $itemId);
     }
     if (!$item) {
         Mage::throwException(Mage::helper('wishlist')->__('Cannot specify wishlist item.'));
     }
     $product = $item->getProduct();
     $productId = $product->getId();
     if ($productId) {
         if (!$params) {
             $params = new Varien_Object();
         } else {
             if (is_array($params)) {
                 $params = new Varien_Object($params);
             }
         }
         $params->setCurrentConfig($item->getBuyRequest());
         $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($buyRequest, $params);
         $product->setWishlistStoreId($item->getStoreId());
         $items = $this->getItemCollection();
         $isForceSetQuantity = true;
         foreach ($items as $_item) {
             /* @var $_item Mage_Wishlist_Model_Item */
             if ($_item->getProductId() == $product->getId() && $_item->representProduct($product) && $_item->getId() != $item->getId()) {
                 // We do not add new wishlist item, but updating the existing one
                 $isForceSetQuantity = false;
             }
         }
         $resultItem = $this->addNewItem($product, $buyRequest, $isForceSetQuantity);
         /**
          * Error message
          */
         if (is_string($resultItem)) {
             Mage::throwException(Mage::helper('checkout')->__($resultItem));
         }
         if ($resultItem->getId() != $itemId) {
             if ($resultItem->getDescription() != $item->getDescription()) {
                 $resultItem->setDescription($item->getDescription())->save();
             }
             $item->isDeleted(true);
             $this->setDataChanges(true);
         } else {
             $resultItem->setQty($buyRequest->getQty() * 1);
             $resultItem->setOrigData('qty', 0);
         }
     } else {
         Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
     }
     return $this;
 }
Example #3
0
 /**
  * Updates quote item with new configuration
  *
  * $params sets how current item configuration must be taken into account and additional options.
  * It's passed to Mage_Catalog_Helper_Product->addParamsToBuyRequest() to compose resulting buyRequest.
  *
  * Basically it can hold
  * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs), so they won't
  *   intersect with other submitted options
  *
  * For more options see Mage_Catalog_Helper_Product->addParamsToBuyRequest()
  *
  * @param int $itemId
  * @param Varien_Object $buyRequest
  * @param null|array|Varien_Object $params
  * @return Mage_Sales_Model_Quote_Item
  *
  * @see Mage_Catalog_Helper_Product::addParamsToBuyRequest()
  */
 public function updateItem($itemId, $buyRequest, $params = null)
 {
     $item = $this->getItemById($itemId);
     if (!$item) {
         Mage::throwException(Mage::helper('sales')->__('Wrong quote item id to update configuration.'));
     }
     $productId = $item->getProduct()->getId();
     //We need to create new clear product instance with same $productId
     //to set new option values from $buyRequest
     $product = Mage::getModel('catalog/product')->setStoreId($this->getStore()->getId())->load($productId);
     if (!$params) {
         $params = new Varien_Object();
     } else {
         if (is_array($params)) {
             $params = new Varien_Object($params);
         }
     }
     $params->setCurrentConfig($item->getBuyRequest());
     $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($buyRequest, $params);
     $buyRequest->setResetCount(true);
     $resultItem = $this->addProduct($product, $buyRequest);
     if (is_string($resultItem)) {
         Mage::throwException($resultItem);
     }
     if ($resultItem->getParentItem()) {
         $resultItem = $resultItem->getParentItem();
     }
     if ($resultItem->getId() != $itemId) {
         /*
          * Product configuration didn't stick to original quote item
          * It either has same configuration as some other quote item's product or completely new configuration
          */
         $this->removeItem($itemId);
         $items = $this->getAllItems();
         foreach ($items as $item) {
             if ($item->getProductId() == $productId && $item->getId() != $resultItem->getId()) {
                 if ($resultItem->compare($item)) {
                     // Product configuration is same as in other quote item
                     $resultItem->setQty($resultItem->getQty() + $item->getQty());
                     $this->removeItem($item->getId());
                     break;
                 }
             }
         }
     } else {
         $resultItem->setQty($buyRequest->getQty());
     }
     return $resultItem;
 }
Example #4
0
 /**
  * Process $buyRequest and sets its options before saving configuration to some product item.
  * This method is used to attach additional parameters to processed buyRequest.
  *
  * $params holds parameters of what operation must be performed:
  * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file inputs,
  *   so they won't intersect with other submitted options
  *
  * @param Varien_Object|array $buyRequest
  * @param Varien_Object|array $params
  * @return Varien_Object
  */
 public function addParamsToBuyRequest($buyRequest, $params)
 {
     if (is_array($buyRequest)) {
         $buyRequest = new Varien_Object($buyRequest);
     }
     if (is_array($params)) {
         $params = new Varien_Object($params);
     }
     // Ensure that currentConfig goes as Varien_Object - for easier work with it later
     $currentConfig = $params->getCurrentConfig();
     if ($currentConfig) {
         if (is_array($currentConfig)) {
             $params->setCurrentConfig(new Varien_Object($currentConfig));
         } else {
             if (!$currentConfig instanceof Varien_Object) {
                 $params->unsCurrentConfig();
             }
         }
     }
     /*
      * Notice that '_processing_params' must always be object to protect processing forged requests
      * where '_processing_params' comes in $buyRequest as array from user input
      */
     $processingParams = $buyRequest->getData('_processing_params');
     if (!$processingParams || !$processingParams instanceof Varien_Object) {
         $processingParams = new Varien_Object();
         $buyRequest->setData('_processing_params', $processingParams);
     }
     $processingParams->addData($params->getData());
     return $buyRequest;
 }