diff --git a/CHANGELOG.md b/CHANGELOG.md index 144ab9bb..45f443e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning. +### 3.11.2 +* Implement new add multiple products to cart method to fix cart products having no image + ### 3.11.1 * Fix add multiple products to cart javascript function where some products added could have no image or link diff --git a/app/code/community/Nosto/Tagging/Block/Addtocart.php b/app/code/community/Nosto/Tagging/Block/Addtocart.php index 08700c8a..6807bb13 100644 --- a/app/code/community/Nosto/Tagging/Block/Addtocart.php +++ b/app/code/community/Nosto/Tagging/Block/Addtocart.php @@ -84,4 +84,20 @@ public function getAddSkuToCartUrl() return $urlHelper->getNostoAddToCartUrl($store); } + + /** + * Returns the url for add to cart controller + * + * @return string + */ + public function getAddMultipleProductsToCartUrl() + { + /** @var Nosto_Tagging_Helper_Url $urlHelper */ + $urlHelper = Mage::helper('nosto_tagging/url'); + /** @var Nosto_Tagging_Helper_Data $helper */ + $helper = Mage::helper('nosto_tagging'); + $store = $helper->getStore(); + + return $urlHelper->getNostoAddMultipleProductsToCartUrl($store); + } } diff --git a/app/code/community/Nosto/Tagging/Helper/Url.php b/app/code/community/Nosto/Tagging/Helper/Url.php index 067da243..cc5fbc21 100644 --- a/app/code/community/Nosto/Tagging/Helper/Url.php +++ b/app/code/community/Nosto/Tagging/Helper/Url.php @@ -115,6 +115,11 @@ class Nosto_Tagging_Helper_Url extends Mage_Core_Helper_Abstract */ const NOSTO_ADD_TO_CART_PATH = 'nosto/addToCart/add'; + /** + * Nosto's add multiple products to cart controller + */ + const NOSTO_ADD_MULTIPLE_TO_CART_PATH = 'nosto/addToCart/addMultipleProductsToCart'; + /** * Path to Nosto's restore cart controller */ @@ -536,14 +541,7 @@ public function generateRestoreCartUrl($hash, Mage_Core_Model_Store $store) */ public function getAddToCartUrl(Mage_Core_Model_Store $store) { - $defaultParams = $this->getUrlOptionsWithNoSid($store); - $defaultParams[self::MAGENTO_URL_OPTION_SECURE] = 1; - $url = Mage::getUrl( - self::MAGENTO_ADD_TO_CART_PATH, - $defaultParams - ); - - return $url; + return $this->getAddToCartUrlByAction($store, self::MAGENTO_ADD_TO_CART_PATH); } /** @@ -554,17 +552,39 @@ public function getAddToCartUrl(Mage_Core_Model_Store $store) * @return string the url. */ public function getNostoAddToCartUrl(Mage_Core_Model_Store $store) + { + return $this->getAddToCartUrlByAction($store, self::NOSTO_ADD_TO_CART_PATH); + } + + /** + * Gets the absolute URL to the Nosto's add multiple to cart controller and action + * + * @param Mage_Core_Model_Store $store the store to get the url for. + * + * @return string the url. + */ + public function getNostoAddMultipleProductsToCartUrl(Mage_Core_Model_Store $store) + { + return $this->getAddToCartUrlByAction($store, self::NOSTO_ADD_MULTIPLE_TO_CART_PATH); + } + + /** + * Wrapper that returns URL with action in add to cart controller + * @param Mage_Core_Model_Store $store + * @param String $action + * @return string + */ + protected function getAddToCartUrlByAction(Mage_Core_Model_Store $store, String $action) { $defaultParams = $this->getUrlOptionsWithNoSid($store); $defaultParams[self::MAGENTO_URL_OPTION_SECURE] = 1; - $url = Mage::getUrl( - self::NOSTO_ADD_TO_CART_PATH, + return Mage::getUrl( + $action, $defaultParams ); - - return $url; } + /** * Gets the absolute URL to the Nosto configuration page * diff --git a/app/code/community/Nosto/Tagging/controllers/AddToCartController.php b/app/code/community/Nosto/Tagging/controllers/AddToCartController.php index 6a29c7da..265baccd 100644 --- a/app/code/community/Nosto/Tagging/controllers/AddToCartController.php +++ b/app/code/community/Nosto/Tagging/controllers/AddToCartController.php @@ -69,21 +69,7 @@ public function addAction() $parentType = $product->getTypeInstance(); $attributeOptions = array(); if ($parentType instanceof Mage_Catalog_Model_Product_Type_Configurable) { - $skuProduct = Mage::getModel('catalog/product')->load($skuId); - $configurableAttributes = $parentType->getConfigurableAttributesAsArray($product); - foreach ($configurableAttributes as $configurableAttribute) { - $attributeCode = $configurableAttribute['attribute_code']; - /** @var \Mage_Catalog_Model_Resource_Product $productResource */ - $productResource = $skuProduct->getResource(); - $attribute = $productResource->getAttribute($attributeCode); - if ($attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) { - $attributeId = $attribute->getId(); - $attributeValueId = $skuProduct->getData($attributeCode); - if ($attributeId && $attributeValueId) { - $attributeOptions[$attributeId] = $attributeValueId; - } - } - } + $attributeOptions = $this->getOptionAttributes($skuId, $parentType, $product); } if (empty($attributeOptions)) { @@ -125,4 +111,100 @@ public function addAction() } return $this; } + + /** + * @return Mage_Checkout_CartController|Nosto_Tagging_AddToCartController + * @throws Mage_Core_Exception + * @throws Mage_Exception + */ + public function addMultipleProductsToCartAction() + { + if (!$this->_validateFormKey()) { + /** @noinspection PhpUnhandledExceptionInspection */ + Mage::throwException('Invalid form key'); + } + $cart = $this->_getCart(); + $products = explode(',', $this->getRequest()->getParam('product')); + $skus = explode(',', $this->getRequest()->getParam('skus')); + foreach ($products as $key => $product) { + try { + /* @var Mage_Catalog_Model_Product $product */ + $this->getRequest()->setParam('product', $product); + $product = $this->_initProduct(); + if (!$product) { + return $this->_goBack(); + } + $parentType = $product->getTypeInstance(); + $params = null; + if ($parentType instanceof Mage_Catalog_Model_Product_Type_Configurable) { + $attributeOptions = $this->getOptionAttributes($skus[$key], $parentType, $product); + if (empty($attributeOptions)) { + $this->_getSession()->addError( + $this->__( + 'Cannot add %s to shopping cart.', + Mage::helper('core')->escapeHtml($product->getName()) + ) + ); + return $this->_goBack(); + } + $params = array('super_attribute' => $attributeOptions); + } + $cart->addProduct($product, $params); + /** @noinspection PhpUndefinedMethodInspection */ + if (!$this->_getSession()->getNoCartRedirect(true) && !$cart->getQuote()->getHasError()) { + $message = $this->__( + '%s was added to your shopping cart.', + Mage::helper('core')->escapeHtml($product->getName()) + ); + $this->_getSession()->addSuccess($message); + } + Mage::dispatchEvent( + 'checkout_cart_add_product_complete', + array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()) + ); + } catch (\Exception $e) { + $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.')); + Mage::logException($e); + /** @noinspection PhpUndefinedMethodInspection */ + $this->_getSession()->setCartWasUpdated(false); + return $this->_goBack(); + } + } + $cart->save(); + /** @noinspection PhpUndefinedMethodInspection */ + $this->_getSession()->setCartWasUpdated(true); + return $this->_goBack(); + } + + /** + * @param $skuId + * @param Mage_Catalog_Model_Product_Type_Configurable $parentType + * @param Mage_Catalog_Model_Product $product + * @return array + */ + protected function getOptionAttributes( + $skuId, + Mage_Catalog_Model_Product_Type_Configurable $parentType, + Mage_Catalog_Model_Product $product + ) + { + $attributeOptions = array(); + $skuProduct = Mage::getModel('catalog/product')->load($skuId); + $configurableAttributes = $parentType->getConfigurableAttributesAsArray($product); + foreach ($configurableAttributes as $configurableAttribute) { + $attributeCode = $configurableAttribute['attribute_code']; + /** @var \Mage_Catalog_Model_Resource_Product $productResource */ + $productResource = $skuProduct->getResource(); + $attribute = $productResource->getAttribute($attributeCode); + if ($attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) { + $attributeId = $attribute->getId(); + $attributeValueId = $skuProduct->getData($attributeCode); + if ($attributeId && $attributeValueId) { + $attributeOptions[$attributeId] = $attributeValueId; + } + } + } + return $attributeOptions; + } + } diff --git a/app/code/community/Nosto/Tagging/etc/config.xml b/app/code/community/Nosto/Tagging/etc/config.xml index faed2092..ece58a4e 100644 --- a/app/code/community/Nosto/Tagging/etc/config.xml +++ b/app/code/community/Nosto/Tagging/etc/config.xml @@ -28,7 +28,7 @@ - 3.11.1 + 3.11.2 diff --git a/app/design/frontend/base/default/template/nostotagging/addtocart.phtml b/app/design/frontend/base/default/template/nostotagging/addtocart.phtml index 1e16f200..eee6711d 100644 --- a/app/design/frontend/base/default/template/nostotagging/addtocart.phtml +++ b/app/design/frontend/base/default/template/nostotagging/addtocart.phtml @@ -63,9 +63,18 @@ $formKey = $session->getFormKey(); // Products must be and array of objects [{productId: "123", skuId: "321"}] Nosto.addMultipleProductsToCart = function (products, element) { + var productsArray = []; + var skus = []; products.forEach(function(product) { - Nosto.addSkuToCart(product, element, 1); + Nosto.trackAddToCartClick(product.productId, element); + productsArray.push(product.productId); + skus.push(product.skuId); }); + var fields = {}; + fields["product"] = productsArray; + fields["skus"] = skus; + fields["form_key"]= ""; + Nosto.postAddToCartForm(fields, "getAddMultipleProductsToCartUrl() ?>"); }; // Product object must have fields productId and skuId {productId: 123, skuId: 321}