0

I am trying to override the getValueHtml function inside Magento\Bundle\Block\Sales\Order\Items\Renderer. When I test this in my order emails it results in the entire item block being empty. What gives?

I've got my module setup and inside di.xml I've got:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Bundle\Block\Sales\Order\Items\Renderer" type="Vendor\Module\Block\Sales\Order\Items\Renderer" />
</config>

Then under app/code/Vendore/Module/Block/Sales/Order/Items/Renderer.php I have:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Block\Sales\Order\Items;

/**
 * Order item render block
 *
 * @author      Magento Core Team <[email protected]>
 */
class Renderer extends \Magento\Bundle\Block\Sales\Order\Items\Renderer
{

    /**
     * @param mixed $item
     * @return string
     */
    public function getValueHtml($item)
    {
        if ($attributes = $this->getSelectionAttributes($item)) {
            if($attributes['qty'] == 1) {
                return $this->escapeHtml($item->getName());
            } else {
                return sprintf('%d', $attributes['qty']) . 'x ' . $this->escapeHtml($item->getName());
            }
        } else {
            return $this->escapeHtml($item->getName());
        }
    }

}

1 Answer 1

0

I am making the assumption here that you have installed Magento2 via composer.

Are you loading the core template for that Block properly after rewriting it?

To test set a break point in vendor/magento/module-bundle/view/frontend/templates/email/order/items/creditmemo/default.phtml

If execution isn't reaching that template you will likely need to manually set the template. This will ensure that the template is looked for in the core module and not in your custom module:

One option for setting the template manually is in your existing overridden method:

public function getValueHtml($item)
{
    $this->setTemplate('Magento_Bundle::email/order/items/creditmemo/default.phtml');
    if ($attributes = $this->getSelectionAttributes($item)) {
        if($attributes['qty'] == 1) {
            return $this->escapeHtml($item->getName());
        } else {
            return sprintf('%d', $attributes['qty']) . 'x ' . $this->escapeHtml($item->getName());
        }
    } else {
        return $this->escapeHtml($item->getName());
    }
}

The file path, in this case email/order/items/creditmemo/default.phtml, will depend on which ActionController(s) your Block is being rendered for as the different controller action handle layout files in module-bundle use the same Block instance to render different templates. The example above would be for the route/action controller combo that uses this layout file: sales_email_order_creditmemo_renderers.xml.

There are probably better ways to set the template that would work across multiple routes, this was simply an example.

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.