I have created a file app/design/frontend/my_vendor/my_theme/Magento_Checkout/web/template/minicart/item/default.html to write the Magento 2 standard mini cart default.html. And this is working fine.

However, I need to add some PHP code into my default.html. Can I change this to default.phtml, so I can add PHP code? and how do I tell the system to use default.phtml as the mini cart template? I can't find a layout.xml where we can define the file name.

  • it's loading by knkout UiComponent so you can not change it from .html to .phtml – kunj Aug 27 '18 at 11:33
  • you can add any thing php code inside Magento\Checkout\CustomerData\DefaultItem with in function GetItemData and call them inside default.html file – Ansar Husain Aug 27 '18 at 11:35
  • how do you add new things to the function @AnsarHusain can you show me a example i used a preferenace to add a new value in and when ever i call that to html Unable to process binding "html: function(){return row_total }" Message: row_total is not defined – jibin george Jun 12 at 8:54

Magento 2 using private content and KnockoutJs for load minicart.

Example add price row total to minicart.

Override DefaultItem customer data Magento\Checkout\CustomerData\DefaultItem

<preference for="Magento\Checkout\CustomerData\DefaultItem" type="Your_Vendor\Module\CustomerData\DefaultItem"/>

Add row total to doGetItemData function

protected function doGetItemData()
        $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
        $productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());

        return [
            'options' => $this->getOptionList(),
            'qty' => $this->item->getQty() * 1,
            'item_id' => $this->item->getId(),
            'configure_url' => $this->getConfigureUrl(),
            'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
            'product_id' => $this->item->getProduct()->getId(),
            'product_name' => $productName,
            'product_sku' => $this->item->getProduct()->getSku(),
            'product_url' => $this->getProductUrl(),
            'product_has_url' => $this->hasProductUrl(),
            'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
            'product_price_value' => $this->item->getCalculationPrice(),
            'product_image' => [
                'src' => $imageHelper->getUrl(),
                'alt' => $imageHelper->getLabel(),
                'width' => $imageHelper->getWidth(),
                'height' => $imageHelper->getHeight(),
            'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
                && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
            'row_total' => $this->checkoutHelper->formatPrice($this->item->getRowTotal()),

Display on default template. Add snippet code below to default template (app/design/frontend/my_vendor/my_theme/Magento_Checkout/web/template/minicart/item/default.html)

<div class="row-total" data-bind="html: row_total"></div>

Result: enter image description here

  • Hi I am new with Knockout js, i have question if i pass 'row_total' => '1' can i do a check in the html by <! -- ko if: row_total -- > show h1 title and close if off by < !-- /ko -- > would this work? – jibin george Jun 12 at 8:40
  • Unable to process binding "html: function(){return row_total }" Message: row_total is not defined also i get this when i do your method – jibin george Jun 12 at 8:51
  • Did you add row_total to doGetItemData? – bang.nguyen47 Jun 15 at 9:08
  • @ bang.nguyen47, it wont work because its protected even if you use preference it wouldn't work. while i was reading you could use a plugin on \Magento\Checkout\CustomerData\AbstractItem::getItemData() – jibin george Jun 17 at 7:28
  • 1
    @jibingeorge thank for your information – bang.nguyen47 Jun 21 at 7:48

Your Answer

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

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