Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Hotfix/add to cart js function (#532)
Browse files Browse the repository at this point in the history
* Add extra route to add multiple products to cart controller

* WIP

* WIP - Multiple Configurable products Add to cart

* Extract getOptionAttributes into method

* Bump version && Update Changelog

* Fix Phan Warnings
  • Loading branch information
supercid authored Jul 24, 2019
1 parent 049f3b0 commit 07a5c5d
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
16 changes: 16 additions & 0 deletions app/code/community/Nosto/Tagging/Block/Addtocart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
44 changes: 32 additions & 12 deletions app/code/community/Nosto/Tagging/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
*
Expand Down
112 changes: 97 additions & 15 deletions app/code/community/Nosto/Tagging/controllers/AddToCartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion app/code/community/Nosto/Tagging/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<config>
<modules>
<Nosto_Tagging>
<version>3.11.1</version>
<version>3.11.2</version>
</Nosto_Tagging>
</modules>
<global>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]= "<?php echo $formKey; ?>";
Nosto.postAddToCartForm(fields, "<?php echo $this->getAddMultipleProductsToCartUrl() ?>");
};

// Product object must have fields productId and skuId {productId: 123, skuId: 321}
Expand Down

0 comments on commit 07a5c5d

Please sign in to comment.