2

I am trying to create a custom block on the checkout/cart page so that I can show a countdown to free shipping. I keep getting Invalid block type as an error, though.

checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="checkout.cart.totals.container">
        <block type="checkout/cart" class="Miles\FreeShippingCountOnPage\Block\Checkout\Cart\Countdown" name="checkout.cart.miles.countdown" after="-" template="Vendor_Module::checkout/cart/countdown.phtml"/>
    </referenceContainer>
</body>
</page>

Vendor\Module\view\frontend\templates\checkout\cart\countdown.phtml

<span><?php

$_item = $block->getItem();
$product = $_item->getProduct();
$additional_data = $block->getAdditionalData();
?>
<div>
<span><?php echo $additional_data?></span>
</div>

?></span>

Vendor\Module\Block\Checkout\Cart\Countdown.php

<?php

namespace Vendor\Module\Block\Checkout\Cart;

class Countdown extends \Magento\Framework\View\Element\Template
{

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = []
) {
    parent::__construct($context, $data);
}


}

UPDATE

I have found an html file that looks to actually build the area I need to add the information on located in the path below that I am attempting to use/override

\vendor\magento\module-tax\view\frontend\web\template\checkout\summary\subtotal.html

<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->


<!-- ko if: isBothPricesDisplayed() -->
<tr class="totals sub excl">
    <th class="mark" scope="row">
        <span data-bind="text: title"></span>
        <span data-bind="text: excludingTaxMessage"></span>
    </th>
    <td class="amount">
        <span class="price" data-bind="text: getValue(), attr: {'data-th': excludingTaxMessage}"></span>
    </td>
</tr>
<tr class="totals sub incl">
    <th class="mark" scope="row">
        <span data-bind="text: title"></span>
        <span data-bind="text: includingTaxMessage"></span>
    </th>
    <td class="amount">
        <span class="price" data-bind="text: getValueInclTax(), attr: {'data-th': includingTaxMessage}"></span>
    </td>
</tr>
<!-- /ko -->
<!-- ko if: !isBothPricesDisplayed() && isIncludingTaxDisplayed() -->
<tr class="totals sub">
    <th data-bind="text: title" class="mark" scope="row"></th>
    <td class="amount">
        <span class="price" data-bind="text: getValueInclTax(), attr: {'data-th': title}"></span>
    </td>
</tr>
<!-- /ko -->
<!-- ko if: !isBothPricesDisplayed() && !isIncludingTaxDisplayed() -->
<tr class="totals sub">
    <th data-bind="text: title" class="mark" scope="row"></th>
    <td class="amount">
        <span class="price" data-bind="text: getValue(), attr: {'data-th': title}"></span><br/>
        <!-- ko if: getValue() >= 50 -->
        <div data-bind="text: 'This order qualifies for free shipping!'"></div>
        <!-- /ko -->
        <!-- ko ifnot: getValue() >= 50 -->
        <div data-bind="text: getValue(), html: 'You have $' + (50.00 - getValue()) + ' left until free shipping!'"></div>
        <!-- /ko -->


    </td>
</tr>
<!-- /ko -->

Any help would be greatly appreciated and thanks in advance!

1
  • Figured it out and posted below. Thanks for the help everyone!
    – Rich
    Nov 4, 2016 at 16:01

2 Answers 2

1

you need to add the class of your block.Try below code

<?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> 
        <body> 
            <referenceContainer name="checkout.cart.totals.container"> 
                <block class="Miles\FreeShippingCountOnPage\Block\Checkout\Cart\Countdown" name="checkout.cart.miles.countdown" after="-" template="Vendor_Module::checkout/cart/countdown.phtml"/> 
            </referenceContainer> 
        </body>
    </page>
3
  • "Type" is no longer available in magento 2. Nov 2, 2016 at 17:28
  • 1
    Ya... Adding in mobile instead of keeping class I kept type.
    – Priyank
    Nov 2, 2016 at 17:31
  • Hey, thanks. I actually tried that already though. That's what gave me the "invalid block type" error in the first place. Meant to post that version of my code instead of this one. I knew that type was deprecated, I was just taking shots in the dark at that point. Sorry.
    – Rich
    Nov 3, 2016 at 13:32
1

I actually just figured it out. I had to update the lines in the if ifnot statement to this:

<span class="price" data-bind="text: getValue(), attr: {'data-th': title}"></span><br/>
<!-- ko if: totals().subtotal >= 50 -->
<div data-bind="text: 'This order qualifies for free shipping!'"></div>
<!-- /ko -->
<!-- ko ifnot: totals().subtotal >= 50 -->
<div data-bind="html: 'You have $' + (50.00 - totals().subtotal) + ' left until free shipping!'"></div>
<!-- /ko -->

I just needed the correct param/functions and variables.

Your Answer

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

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