Skip to content

Commit

Permalink
MAGETWO-58057: [Backport] - [GitHub] Unable to add more than 1 produc…
Browse files Browse the repository at this point in the history
…t to a cart from Wishlist #5282 - for 2.1
  • Loading branch information
Sergey Semenov committed Sep 8, 2016
1 parent fdb3182 commit f553379
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
8 changes: 7 additions & 1 deletion app/code/Magento/Wishlist/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,13 @@ public function getSharedAddAllToCartUrl()
*/
protected function _getCartUrlParameters($item)
{
return ['item' => is_string($item) ? $item : $item->getWishlistItemId()];
$params = [
'item' => is_string($item) ? $item : $item->getWishlistItemId(),
];
if ($item instanceof \Magento\Wishlist\Model\Item) {
$params['qty'] = $item->getQty();
}
return $params;
}

/**
Expand Down
32 changes: 29 additions & 3 deletions app/code/Magento/Wishlist/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ protected function setUp()
->setMethods([
'getProduct',
'getWishlistItemId',
'getQty',
])
->getMock();

Expand Down Expand Up @@ -217,13 +218,17 @@ public function testGetAddToCartParams()
$url = 'result url';
$storeId = 1;
$wishlistItemId = 1;
$wishlistItemQty = 1;

$this->wishlistItem->expects($this->once())
->method('getProduct')
->willReturn($this->product);
$this->wishlistItem->expects($this->once())
->method('getWishlistItemId')
->willReturn($wishlistItemId);
$this->wishlistItem->expects($this->once())
->method('getQty')
->willReturn($wishlistItemQty);

$this->product->expects($this->once())
->method('isVisibleInSiteVisibility')
Expand All @@ -243,9 +248,13 @@ public function testGetAddToCartParams()
->with('wishlist/index/cart')
->willReturn($url);

$expected = [
'item' => $wishlistItemId,
'qty' => $wishlistItemQty,
];
$this->postDataHelper->expects($this->once())
->method('getPostData')
->with($url, ['item' => $wishlistItemId])
->with($url, $expected)
->willReturn($url);

$this->assertEquals($url, $this->model->getAddToCartParams($this->wishlistItem));
Expand All @@ -256,6 +265,7 @@ public function testGetAddToCartParamsWithReferer()
$url = 'result url';
$storeId = 1;
$wishlistItemId = 1;
$wishlistItemQty = 1;
$referer = 'referer';
$refererEncoded = 'referer_encoded';

Expand All @@ -265,6 +275,9 @@ public function testGetAddToCartParamsWithReferer()
$this->wishlistItem->expects($this->once())
->method('getWishlistItemId')
->willReturn($wishlistItemId);
$this->wishlistItem->expects($this->once())
->method('getQty')
->willReturn($wishlistItemQty);

$this->product->expects($this->once())
->method('isVisibleInSiteVisibility')
Expand All @@ -288,9 +301,14 @@ public function testGetAddToCartParamsWithReferer()
->with('wishlist/index/cart')
->willReturn($url);

$expected = [
'item' => $wishlistItemId,
ActionInterface::PARAM_NAME_URL_ENCODED => $refererEncoded,
'qty' => $wishlistItemQty,
];
$this->postDataHelper->expects($this->once())
->method('getPostData')
->with($url, ['item' => $wishlistItemId, ActionInterface::PARAM_NAME_URL_ENCODED => $refererEncoded])
->with($url, $expected)
->willReturn($url);

$this->assertEquals($url, $this->model->getAddToCartParams($this->wishlistItem, true));
Expand Down Expand Up @@ -363,13 +381,17 @@ public function testGetSharedAddToCartUrl()
$url = 'result url';
$storeId = 1;
$wishlistItemId = 1;
$wishlistItemQty = 1;

$this->wishlistItem->expects($this->once())
->method('getProduct')
->willReturn($this->product);
$this->wishlistItem->expects($this->once())
->method('getWishlistItemId')
->willReturn($wishlistItemId);
$this->wishlistItem->expects($this->once())
->method('getQty')
->willReturn($wishlistItemQty);

$this->product->expects($this->once())
->method('isVisibleInSiteVisibility')
Expand All @@ -383,9 +405,13 @@ public function testGetSharedAddToCartUrl()
->with('wishlist/shared/cart')
->willReturn($url);

$exptected = [
'item' => $wishlistItemId,
'qty' => $wishlistItemQty,
];
$this->postDataHelper->expects($this->once())
->method('getPostData')
->with($url, ['item' => $wishlistItemId])
->with($url, $exptected)
->willReturn($url);

$this->assertEquals($url, $this->model->getSharedAddToCartUrl($this->wishlistItem));
Expand Down
22 changes: 22 additions & 0 deletions app/code/Magento/Wishlist/view/frontend/web/wishlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ define([
event.preventDefault();
$.mage.dataPost().postData($(event.currentTarget).data('post-remove'));
}, this))
.on('click', this.options.addToCartSelector, $.proxy(this._beforeAddToCart, this))
.on('click', this.options.addAllToCartSelector, $.proxy(this._addAllWItemsToCart, this))
.on('focusin focusout', this.options.commentInputType, $.proxy(this._focusComment, this));
}
Expand All @@ -59,6 +60,27 @@ define([
});
},

/**
* Process data before add to cart
*
* - update item's qty value.
*
* @param {Event} event
* @private
*/
_beforeAddToCart: function(event) {
var elem = $(event.currentTarget),
itemId = elem.data(this.options.dataAttribute),
qtyName = $.validator.format(this.options.nameFormat, itemId),
qtyValue = elem.parents().find('[name="' + qtyName + '"]').val(),
params = elem.data('post');

if (params) {
params.data = $.extend({}, params.data, {'qty': qtyValue});
elem.data('post', params);
}
},

/**
* Add wish list items to cart.
* @private
Expand Down

0 comments on commit f553379

Please sign in to comment.