0

I have a cart rule that discounts an item from a category A by 50% when in the cart with an item from category B. I would like to point this out to customers in the minicart, cart, and checkout summary cart by displaying the discounted price next to it and cross out the previous price.

Now, i've managed to accomplish this in the cart and minicart: cart minicart

but have no idea how to do it in the checkout summary cart: checkout-summary

I've found the piece of code that outputs the price here: /magento/module-weee/view/frontend/web/template/checkout/summary/item/price

<span class="cart-price">
    <span class="price" data-bind="text: getFormattedPrice(getRowDisplayPriceExclTax($parents[2]))"></span>
</span>

I don't know much about knockoutJS so couldn't do much.

Also tried this: https://magento.stackexchange.com/a/363958/103634, didn't work.

Any help would be appreciated, thank you.

1 Answer 1

0
  1. Create plugin, di.xml
    <type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin name="checkout-summary-custom-price"
            type="Lexim\Checkout\Plugin\DefaultConfigProviderPlugin"/>
    </type>
  1. File DefaultConfigProviderPlugin.php
<?php

namespace Lexim\Checkout\Plugin;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
use Magento\Checkout\Model\DefaultConfigProvider;
use Magento\Quote\Api\CartTotalRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Checkout\Model\Session as CheckoutSession;

class DefaultConfigProviderPlugin
{
    /** @var PricingHelper */
    private $_pricingHelper;

    /** @var CartTotalRepositoryInterface */
    private $_cartTotalRepository;

    /** @var CheckoutSession */
    private $_checkoutSession;


    /**
     * @param PricingHelper $_pricingHelper
     * @param CartTotalRepositoryInterface $_cartTotalRepository
     * @param CheckoutSession $_checkoutSession
     */
    public function __construct(
        PricingHelper                $_pricingHelper,
        CartTotalRepositoryInterface $_cartTotalRepository,
        CheckoutSession              $_checkoutSession
    )
    {
        $this->_pricingHelper = $_pricingHelper;
        $this->_cartTotalRepository = $_cartTotalRepository;
        $this->_checkoutSession = $_checkoutSession;
    }

    /**
     * @param DefaultConfigProvider $subject
     * @param $result
     *
     * @return mixed
     */
    public function afterGetConfig(
        DefaultConfigProvider $subject,
                              $result
    )
    {
        if (is_array($result) && isset($result['totalsData'])) {
            $result['totalsData'] = $this->_getTotalsData();
        }
        return $result;
    }


    /**
     * @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
     * @throws LocalizedException
     * @throws NoSuchEntityException
     */
    private function getCurrentQuote()
    {
        return $this->_checkoutSession->getQuote();
    }

    /**
     * Extend from function getAllVisibleItems() in Magento\Quote\Model\Quote
     *
     * @return \Magento\Quote\Model\Quote\Item[]
     */
    private function _getQuoteItems($quote = null): array
    {
        if (!$quote) {
            $quote = $this->getCurrentQuote();
        }
        $items = [];
        foreach ($quote->getItemsCollection() as $item) {
            if (!$item->isDeleted() && !$item->getParentItemId() && !$item->getParentItem()) {
                $items[$item->getId()] = $item;
            }
        }
        return $items;
    }


    /**
     * Return quote totals data
     *
     * @return array
     * @throws NoSuchEntityException
     * @throws LocalizedException
     */
    private function _getTotalsData(): array
    {
        $currentQuote = $this->_checkoutSession->getQuote();
        /** @var \Magento\Quote\Api\Data\TotalsInterface $totals */
        $totals = $this->_cartTotalRepository->get($currentQuote->getId());
        $items = [];
        $quoteItems = $this->_getQuoteItems($currentQuote);

        // Get Sale Price 
        /** @var  \Magento\Quote\Model\Cart\Totals\Item $item */
        foreach ($totals->getItems() as $item) {
            $itemId = $item->getItemId();
            $itemArray = $item->__toArray();
            $itemArray['has_sale_price'] = false;
            $itemArray['regular_price'] = null;
            $itemArray['price_html_format'] = '';
            $finalPrice = (float) $itemArray['base_price'];

            if (isset($quoteItems[$itemId])) {
                $_product = $quoteItems[$itemId]->getProduct();
                $type = $_product->getTypeId();
                $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getValue();
                if ($type == 'bundle') {
                    $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
                }
                $itemArray['regular_price'] = $regularPrice;

                $price_html = '<span class="salePriceWrap">';
                if ($finalPrice < $regularPrice) {
                    $itemArray['has_sale_price'] = true;
                    $price_html .= '<span class="oldPrice">' . $this->getFormattedPrice($regularPrice) . '</span>';
                    $price_html .= '<span class="finalPrice">' . $this->getFormattedPrice($finalPrice) . '</span>';
                } else {
                    $price_html = '<span class="price">' . $this->getFormattedPrice($itemArray['base_price']) . '</span>';
                }
                $price_html .= '</span>';

                $itemArray['price_html_format'] = $price_html;
            }
            $items[] = $itemArray;
        }

        $totalSegmentsData = [];
        /** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
        foreach ($totals->getTotalSegments() as $totalSegment) {
            $totalSegmentArray = $totalSegment->toArray();
            if (is_object($totalSegment->getExtensionAttributes())) {
                $totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
            }
            $totalSegmentsData[] = $totalSegmentArray;
        }
        $totals->setItems($items);
        $totals->setTotalSegments($totalSegmentsData);
        $totalsArray = $totals->toArray();
        if (is_object($totals->getExtensionAttributes())) {
            $totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
        }
        return $totalsArray;
    }

    /**
     * Format Price by store and by currency
     *
     * @param $price
     *
     * @return float|string
     */
    private function getFormattedPrice($price)
    {
        return $this->_pricingHelper->currency($price, true, false);
    }

}
  1. html file
    <div class="item_price" data-bind="html: getPriceHtmlFormat($parents[1])"></div>
  1. in view js file
         /**
         * Get items data
         * @returns {*[]}
         */
        getTotalData: function () {
            const checkoutConfig = window.checkoutConfig;
            return checkoutConfig.totalsData.items || [];
        },

        /**
         * Get Price html format
         * @param {Object} item
         * @return {string}
         */
        getPriceHtmlFormat: function (item) {
            const itemId = item.item_id;
            if (item.price_html_format) {
                return item.price_html_format;
            }
            const items = this.getTotalData();
            for (let i = 0; i < items.length; i++) {
                if (items[i].item_id == itemId) {
                    return items[i].price_html_format || '';
                }
            }
            return '';
        },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.