Skip to content

Commit

Permalink
[BUGFIX][6244] Fix Issue with code label display in cart checkout.
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainraye committed May 21, 2017
1 parent bdb0464 commit f133390
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 7 deletions.
18 changes: 12 additions & 6 deletions app/code/Magento/Checkout/view/frontend/web/js/model/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ define([
], function (ko, _) {
'use strict';

var proceedTotalsData = function (data) {
if (_.isObject(data) && _.isObject(data['extension_attributes'])) {
_.each(data['extension_attributes'], function (element, index) {
data[index] = element;
});
}

return data;
};

var billingAddress = ko.observable(null),
shippingAddress = ko.observable(null),
shippingMethod = ko.observable(null),
Expand All @@ -19,7 +29,7 @@ define([
basePriceFormat = window.checkoutConfig.basePriceFormat,
priceFormat = window.checkoutConfig.priceFormat,
storeCode = window.checkoutConfig.storeCode,
totalsData = window.checkoutConfig.totalsData,
totalsData = proceedTotalsData(window.checkoutConfig.totalsData),
totals = ko.observable(totalsData),
collectedTotals = ko.observable({});

Expand Down Expand Up @@ -78,11 +88,7 @@ define([
* @param {Object} data
*/
setTotals: function (data) {
if (_.isObject(data) && _.isObject(data['extension_attributes'])) {
_.each(data['extension_attributes'], function (element, index) {
data[index] = element;
});
}
data = proceedTotalsData(data);
totals(data);
this.setCollectedTotals('subtotal_with_discount', parseFloat(data['subtotal_with_discount']));
},
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Quote/etc/extension_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<extension_attributes for="Magento\Quote\Api\Data\CartInterface">
<attribute code="shipping_assignments" type="Magento\Quote\Api\Data\ShippingAssignmentInterface[]" />
</extension_attributes>

<extension_attributes for="Magento\Quote\Api\Data\TotalsInterface">
<attribute code="coupon_label" type="string" />
</extension_attributes>
</config>
106 changes: 106 additions & 0 deletions app/code/Magento/SalesRule/Plugin/CartTotalRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesRule\Plugin;

use Magento\Store\Model\StoreManagerInterface;

/**
* Class CartTotalRepository
* @package Magento\SalesRule\Plugin
*/
class CartTotalRepository
{
/**
* @var \Magento\Quote\Api\Data\TotalsExtensionFactory
*/
private $extensionFactory;

/**
* @var \Magento\SalesRule\Api\RuleRepositoryInterface
*/
private $ruleRepository;

/**
* @var \Magento\SalesRule\Model\Coupon
*/
private $coupon;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* CartTotalRepository constructor.
* @param \Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory
* @param \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository
* @param \Magento\SalesRule\Model\Coupon $coupon
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory,
\Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository,
\Magento\SalesRule\Model\Coupon $coupon,
StoreManagerInterface $storeManager
) {
$this->extensionFactory = $extensionFactory;
$this->ruleRepository = $ruleRepository;
$this->coupon = $coupon;
$this->storeManager = $storeManager;
}

/**
* @param \Magento\Quote\Model\Cart\CartTotalRepository $subject
* @param \Magento\Quote\Api\Data\TotalsInterface $result
* @return \Magento\Quote\Api\Data\TotalsInterface
*/
public function afterGet(
\Magento\Quote\Model\Cart\CartTotalRepository $subject,
\Magento\Quote\Api\Data\TotalsInterface $result
) {
if ($result->getExtensionAttributes() === null) {
$extensionAttributes = $this->extensionFactory->create();
$result->setExtensionAttributes($extensionAttributes);
}

$extensionAttributes = $result->getExtensionAttributes();
$couponCode = $result->getCouponCode();

if (empty($couponCode)) {
return $result;
}

$this->coupon->loadByCode($couponCode);
$ruleId = $this->coupon->getRuleId();

if (empty($ruleId)) {
return $result;
}

$storeId = $this->storeManager->getStore()->getId();
$rule = $this->ruleRepository->getById($ruleId);

$storeLabel = $storeLabelFallback = null;

/* @var $label \Magento\SalesRule\Model\Data\RuleLabel */
foreach ($rule->getStoreLabels() as $label) {

if ($label->getStoreId() === 0) {
$storeLabelFallback = $label->getStoreLabel();
}

if ($label->getStoreId() == $storeId) {
$storeLabel = $label->getStoreLabel();
break;
}
}

$extensionAttributes->setCouponLabel(($storeLabel) ? $storeLabel : $storeLabelFallback);

$result->setExtensionAttributes($extensionAttributes);

return $result;
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/SalesRule/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,8 @@
</argument>
</arguments>
</type>

<type name="\Magento\Quote\Model\Cart\CartTotalRepository">
<plugin name="coupon_label_plugin" type="Magento\SalesRule\Plugin\CartTotalRepository" />
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ define([
return this.totals()['coupon_code'];
},

/**
* @return {*}
*/
getCouponLabel: function () {
if (!this.totals()) {
return null;
}

return this.totals()['coupon_label'];
},

/**
* @return {Number}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<tr class="totals">
<th colspan="1" style="" class="mark" scope="row">
<span class="title" data-bind="text: title"></span>
<span class="discount coupon" data-bind="text: getCouponCode()"></span>
<span class="discount coupon" data-bind="text: getCouponLabel()"></span>
</th>
<td class="amount" data-bind="attr: {'data-th': title}">
<span><span class="price" data-bind="text: getValue()"></span></span>
Expand Down

0 comments on commit f133390

Please sign in to comment.