0

Magento 1.9 version - Controller file

protected function myAjax()
{

   $layout = $this->getLayout();
   Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
   $totalsBlock = $layout->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml');
   return $totalsBlock->toHtml();
}

config.xml

<sales>
     <quote>
         <totals>
             <productpoint>
                 <class>productpoint/sales_quote_address_total_productpoint</class>
                 <after>subtotal</after>
                 <before>tax</before>
             </productpoint>
         </totals>
     </quote>
</sales>

Model/Sales/Quote/Address/Total/Productpoint

    //In function collect() you can add whatever amount you want to the order totals
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {

        parent::collect($address);

        $this->_setAmount(0);
        $this->_setBaseAmount(0);

        $items = $this->_getAddressItems($address);
        if (!count($items)) {
            return $this; //this makes only address type shipping to come through
        }

        $quote = $address->getQuote();

        //check if module enabled, mincredit < credit assigned and creditpoint redeemed only then display in grand_total block        
        if( ($this->ProductPointCode == $this->EstimateProduct) && ($this->moduleStatus == 1) && ($this->minProductConfig < $this->productPoints) ){

                $exist_amount = $quote->getProductpointAmount();
                $productPointPrice = Mage::helper('productpoint')->getPrice($this->PointsRedeem);
                $balance = $productPointPrice - $exist_amount;

        $address->setProductpointAmount($balance);
        $address->setBaseProductpointAmount($balance);

        $quote->setProductpointAmount($balance);

        $address->setGrandTotal($address->getGrandTotal() - $address->getProductpointAmount());
                $address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getBaseProductpointAmount());
            }    

    }

    //The function fetch() is used for display purposes
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
                //check if module enabled, mincredit < credit assigned and creditpoint redeemed only then display in grand_total block        
                if(($this->ProductPointCode == $this->EstimateProduct) && ($this->moduleStatus == 1) && ($this->minProductConfig < $this->productPoints)){

                $amount = $address->getProductpointAmount();
        $address->addTotal(array(
                'code'  => Mage::helper('productpoint')->getCode(),
                'title' => Mage::helper('productpoint')->__('Points Redeem ('.intval($this->PointsRedeem).'&nbsp;points)'),
                'value' => '-'.$amount
        ));

                return $this;
            }    

    }
}

Magento 2.0.2 Controller file

protected function myAjax()
{   
    $layout = $this->_view->getLayout();
    $this->checkoutSession->setCartWasUpdated(true); 
    $totalsBlock = $layout->createBlock('Magento\Checkout\Block\Cart\Totals')->setTemplate('Magento_Checkout::cart/totals.phtml');
    return $totalsBlock->toHtml();

}

view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <referenceContainer name="content">
        <block class="Magento\Checkout\Block\Cart\Totals" name="checkout.cart.totals" template="cart/totals.phtml">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="block-totals" xsi:type="array">
                            <item name="component" xsi:type="string">Magento_Checkout/js/view/cart/totals</item>
                            <item name="displayArea" xsi:type="string">totals</item>
                            <item name="config" xsi:type="array">
                                <item name="template" xsi:type="string">Magento_Checkout/cart/totals</item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </block>
    </referenceContainer>
</page>

Model/Quote/Address/Total/Productpoint

    //In function collect() you can add whatever amount you want to the order totals
    public function collect(
            \Magento\Quote\Model\Quote $quote,
            //\Magento\Quote\Model\Quote\Address $address,
            \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
            \Magento\Quote\Model\Quote\Address\Total $total
            )

    {
            parent::collect($quote,$shippingAssignment,$total);

            $address = $shippingAssignment->getShipping()->getAddress();
            $this->_setAmount(0);
            $this->_setBaseAmount(0);

            $items = $shippingAssignment->getItems();
            if (!count($items)) {
                    return $this; //this makes only address type shipping to come through
            }

            $quote = $address->getQuote();


            //check if module enabled, minpoints < points assigned and productpoint redeemed only then display in grand_total block        
            if( ($this->ProductPointCode == $this->EstimateProduct) && ($this->moduleStatus == 1) && ($this->minPointsConfig < $this->productPoints) ){


            $exist_amount = $quote->getProductpointAmount();
            $productPointPrice = $this->_helper->getPrice($this->PointsRedeem);
            $balance = $productPointPrice - $exist_amount;


            $address->setProductpointAmount($balance);
            $address->setBaseProductpointAmount($balance);

            $quote->setProductpointAmount($balance);

            $address->setGrandTotal($address->getGrandTotal() - $address->getProductpointAmount());
            $address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getBaseProductpointAmount());
        }    

    }

    //The function fetch() is used for display purposes
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {

            //check if module enabled, minpoints < points assigned and productpoint redeemed only then display in grand_total block        
            if(($this->ProductPointCode == $this->EstimateProduct) && ($this->moduleStatus == 1) && ($this->minPointsConfig < $this->productPoints)){

            $amount = $address->getProductpointAmount();
            $address->addTotal(array(
                    'code'  => $this->_helper->getCode(),
                    'title' => __('Points Redeem ('.$this->PointsRedeem.'&nbsp;points)'),
                    'value' => '-'.$amount
            ));

            return $this;
        }    

    }
}

Exception thrown

Notice: Undefined index: components in app\code\Magento\Checkout\Block\Cart\CartTotalsProcessor.php on line 18

Tried using below, but it returns empy block {"productpointblock":{}}

protected function myAjax()
    {

        $resultPage = $this->resultPageFactory->create();
        $this->checkoutSession->setCartWasUpdated(true); 
        $resultPage->getLayout()->getBlock('checkout.cart.totals');
        return $resultPage;

    }

Later, following this, I added below files.

view\frontend\web\js\view\checkout\cart\totals\productpoint.js

define(
    [
        'Ei_Productpoint/js/view/checkout/summary/productpoint'
    ],
    function (Component) {
        'use strict';

        return Component.extend({

            /**
             * @override
             */
            isDisplayed: function () {
                return true;
            }
        });
    }
);

view\frontend\web\js\view\checkout\summary\productpoint.js

define(
    [
        'Magento_Checkout/js/view/summary/abstract-total',
        'Magento_Checkout/js/model/quote',
        'Magento_Catalog/js/price-utils',
        'Magento_Checkout/js/model/totals'
    ],
    function (Component, quote, priceUtils, totals) {
        "use strict";
        return Component.extend({
            defaults: {
                isFullTaxSummaryDisplayed: window.checkoutConfig.isFullTaxSummaryDisplayed || false,
                template: 'Ei_Productpoint/checkout/summary/productpoint'
            },
            totals: quote.getTotals(),
            isTaxDisplayedInGrandTotal: window.checkoutConfig.includeTaxInGrandTotal || false,
            isDisplayed: function() {
                return this.isFullMode();
            },
            getCustomTitle: function() {
                var title;                
                if (this.totals()) {

                    if(totals.getSegment('productpoint')){
                    title = totals.getSegment('productpoint').title;
                    }
                }
                return title;
            },
            getValue: function() {
                var price = 0;
                if (this.totals()) {

                    if(totals.getSegment('productpoint')){
                    price = totals.getSegment('productpoint').value;
                    }
                }
                return this.getFormattedPrice(price);
            }/*,
            getBaseValue: function() {
                var price = 0;
                if (this.totals()) {
                    price = this.totals().base_productpoint;
                }
                return priceUtils.formatPrice(price, quote.getBasePriceFormat());
            }*/
        });
    }
);
  1. How do I set custom template for core block in Magento 2 so that it updates and display custom discount value as shown in below image ?
  2. How can I update cart block on some event, say when user enters points and deduct that value from order total ?

enter image description here

P.S. For earlier version, I am just updating default cart block with custom discount value, passing it as json response and updating like $('shopping-cart-totals-table').update(data.productpointblock);

  • Have you resolve the issue or not? – Suresh Chikani Dec 8 '17 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.