From a820713003304976a22cacaec1c74398f832cb96 Mon Sep 17 00:00:00 2001 From: Martijn Date: Wed, 27 Dec 2023 13:52:27 +0100 Subject: [PATCH 1/5] Update price-adjustments.md * Fixed overriding of existing discount component * Added sortOrder parameter for flexibility * Adjust example of JS component so gathering of custom total is dynamic * Minor grammar adjustments and clarifications --- .../components/price-adjustments.md | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/pages/development/components/price-adjustments.md b/src/pages/development/components/price-adjustments.md index 4df6186aa..0f33e4e83 100644 --- a/src/pages/development/components/price-adjustments.md +++ b/src/pages/development/components/price-adjustments.md @@ -128,9 +128,9 @@ class Adjustment implements AdjustmentInterface } ``` -The `ADJUSTMENT_CODE` constant is a unique code for the adjustment which it gets added to the `Magento\Framework\Pricing\Adjustment\Collection` collection. +The `ADJUSTMENT_CODE` constant is a unique code for the adjustment which is added to the `Magento\Framework\Pricing\Adjustment\Collection` collection. -The adjustment logic is defined in `extractAdjustment` and `applyAdjustment` functions. +The adjustment logic is defined in the `extractAdjustment` and `applyAdjustment` functions. Price adjustments affect storefront product prices. @@ -138,7 +138,7 @@ Price adjustments **will not** affect quote item prices, so when a product is ad ## Add price adjustments for quote items -To adjustments prices for quote items, a custom quote total is added: +To add price adjustments for quote items, a custom quote total is added: Add the following to `VENDOR/MODULE/etc/sales.xml`: @@ -240,7 +240,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal return [ 'code' => $this->getCode(), - 'title' => __('Custom Total'), + 'title' => __('Custom Surcharge Total'), 'value' => $amount ]; } @@ -255,7 +255,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal } ``` -The `COLLECTOR_TYPE_CODE` constant is a unique name of the custom total. It can be accessed with `Magento\Quote\Model\Quote\Address\Total::getTotalAmount`, and set with `Magento\Quote\Model\Quote\Address\Total::setTotalAmount`. +The `COLLECTOR_TYPE_CODE` constant is a unique name of the custom total. The custom total can be retrieved with `Magento\Quote\Model\Quote\Address\Total::getTotalAmount`, and set with `Magento\Quote\Model\Quote\Address\Total::setTotalAmount`. ### Display price-adjusted totals on the cart page @@ -277,6 +277,7 @@ First, add the new total: VENDOR_MODULE/js/view/cart/totals/surcharge + 25 Custom Surcharge @@ -298,8 +299,10 @@ Then, define a new component: `VENDOR_MODULE/js/view/cart/totals/surcharge`: ```javascript define([ 'Magento_Checkout/js/view/summary/abstract-total', - 'Magento_Checkout/js/model/quote' -], function (Component, quote) { + 'Magento_Checkout/js/model/quote', + 'Magento_Checkout/js/model/totals', + 'mage/translate' +], function (Component, quote, totals, $t) { 'use strict'; return Component.extend({ @@ -325,19 +328,25 @@ define([ return null; } - return 'Custom Surcharge'; + return $t('Custom Surcharge'); }, /** * @return {Number} */ getPureValue: function () { - var price = 0; - for (var i=0; i < window.checkoutConfig.quoteItemData.length; i++) { - price += window.checkoutConfig.quoteItemData[i].qty * 1.79; + let amount = 0, + customSurchargeTotal; + + if (this.totals()) { + customSurchargeTotal = totals.getSegment('custom-surcharge'); + + if (customSurchargeTotal) { + amount = customSurchargeTotal.value; + } } - return price; + return amount; }, /** @@ -352,6 +361,8 @@ define([ Then create the template `VENDOR_MODULE/summary/surcharge`: +`VENDOR/MODULE/view/frontend/web/template/summary/surcharge.html`: + ```html @@ -387,8 +398,9 @@ To display the price-adjusted total on the checkout page, add it to the `totals` - + VENDOR_MODULE/js/view/cart/totals/surcharge + 25 Custom Surcharge From af09618196d8f67435647f1b0baacb7d31aa5f0c Mon Sep 17 00:00:00 2001 From: Martijn Swinkels Date: Fri, 15 Mar 2024 16:26:43 +0100 Subject: [PATCH 2/5] Added example for adding price adjustment row to order totals in admin UI --- .../components/price-adjustments.md | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/src/pages/development/components/price-adjustments.md b/src/pages/development/components/price-adjustments.md index 0f33e4e83..1d9a60fc7 100644 --- a/src/pages/development/components/price-adjustments.md +++ b/src/pages/development/components/price-adjustments.md @@ -168,7 +168,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal const COLLECTOR_TYPE_CODE = 'custom-surcharge'; /** - * Custom constructor. + * Surcharge constructor. */ public function __construct() { @@ -176,7 +176,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal } /** - * Collect address discount amount + * Collect totals including custom surcharge. * * @param Quote $quote * @param ShippingAssignmentInterface $shippingAssignment @@ -250,7 +250,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal */ public function getLabel() { - return __('Custom Surchange'); + return __('Custom Surcharge'); } } ``` @@ -424,3 +424,101 @@ To display the price-adjusted total on the checkout page, add it to the `totals` The `VENDOR_MODULE/js/view/cart/totals/surcharge` component was defined earlier in the article. If all has gone smoothly, when run, you should see the adjusted price reflected in the shopping cart. + +### Display price adjustment in order totals in the admin UI + +To display the price adjustment as a separate row in the order totals on the order detail page within the admin UI, add it to the `order_totals` block. + +`VENDOR/MODULE/view/adminhtml/layout/sales_order_view.xml`: + +```xml + + + + + + + tax + + + + + +``` +You can adjust the value for `condition` in the `setBeforeCondition` action to change before which row the adjustment is added. + +The `order_totals` block triggers the `initTotals()` method for each child block. + +Next we need to define our block. + +`VENDOR/MODULE/Block/Adminhtml/Sales/Order/Totals`: + +```php +namespace VENDOR\MODULE\Block\Adminhtml\Sales\Order; + +use Magento\Framework\DataObject; +use Magento\Framework\View\Element\Template; +use Magento\Framework\View\Element\Template\Context; +use Magento\Sales\Model\Order; +use VENDOR\MODULE\Pricing\Adjustment; +use VENDOR\MODULE\Model\Quote\Surcharge; + +class Totals extends Template +{ + + /** + * @var Surcharge + */ + private Surcharge $surcharge; + + /** + * @param Context $context + * @param array $data + */ + public function __construct( + Context $context, + Adjustment $adjustment, + Surcharge $surcharge, + array $data = [] + ) { + parent::__construct($context, $data); + $this->surcharge = $surcharge; + } + + /** + * @return $this + */ + public function initTotals() + { + $adjustmentValue = 0; + $items = $this->getParentBlock()->getSource()->getAllItems(); + + foreach ($items as $item) { + $adjustmentValue += $item->getQtyOrdered() * Adjustment::ADJUSTMENT_VALUE; + } + + if ($adjustmentValue) { + $totals = $this->getParentBlock()->getTotals(); + $total = new DataObject( + [ + 'code' => Surcharge::COLLECTOR_TYPE_CODE, + 'label' => $this->surcharge->getLabel(), + 'value' => $adjustmentValue, + 'base_value' => $adjustmentValue + ] + ); + + if (isset($totals['grand_total_incl'])) { + $this->getParentBlock()->addTotalBefore($total, 'grand_total'); + } else { + $this->getParentBlock()->addTotalBefore($total, $this->getBeforeCondition()); + } + } + + return $this; + } +} + +``` + +If all went well you should now see a new row as part of the order totals in the admin UI whenever your price adjustment is applied. \ No newline at end of file From 94b7234c4958e6baa8c0e14cef1232109c8524c7 Mon Sep 17 00:00:00 2001 From: Martijn Swinkels Date: Fri, 15 Mar 2024 16:31:10 +0100 Subject: [PATCH 3/5] Removed redundancy from code sample --- src/pages/development/components/price-adjustments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/development/components/price-adjustments.md b/src/pages/development/components/price-adjustments.md index 1d9a60fc7..e7701f085 100644 --- a/src/pages/development/components/price-adjustments.md +++ b/src/pages/development/components/price-adjustments.md @@ -473,11 +473,11 @@ class Totals extends Template /** * @param Context $context + * @param Surcharge $surcharge * @param array $data */ public function __construct( Context $context, - Adjustment $adjustment, Surcharge $surcharge, array $data = [] ) { From 8b8f37c2556beb57c08ccbbf8f75952c11bebd58 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sat, 16 Mar 2024 09:10:58 +0100 Subject: [PATCH 4/5] Update src/pages/development/components/price-adjustments.md Co-authored-by: Jeff Matthews --- src/pages/development/components/price-adjustments.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/development/components/price-adjustments.md b/src/pages/development/components/price-adjustments.md index e7701f085..eb0783f4f 100644 --- a/src/pages/development/components/price-adjustments.md +++ b/src/pages/development/components/price-adjustments.md @@ -445,6 +445,7 @@ To display the price adjustment as a separate row in the order totals on the ord ``` + You can adjust the value for `condition` in the `setBeforeCondition` action to change before which row the adjustment is added. The `order_totals` block triggers the `initTotals()` method for each child block. From c22d2c1f691b7325c84a8fc80df7e73108a58cc4 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sat, 16 Mar 2024 09:11:06 +0100 Subject: [PATCH 5/5] Update src/pages/development/components/price-adjustments.md Co-authored-by: Jeff Matthews --- src/pages/development/components/price-adjustments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/development/components/price-adjustments.md b/src/pages/development/components/price-adjustments.md index eb0783f4f..bcab5932e 100644 --- a/src/pages/development/components/price-adjustments.md +++ b/src/pages/development/components/price-adjustments.md @@ -522,4 +522,4 @@ class Totals extends Template ``` -If all went well you should now see a new row as part of the order totals in the admin UI whenever your price adjustment is applied. \ No newline at end of file +If all went well you should now see a new row as part of the order totals in the admin UI whenever your price adjustment is applied.