adding a letter before order id

I want to concatenate a string 'P' with the increment_id variable in .html file of email template without affect the translation , but I don't know why it didn't work

Answers 1

  • <p class="order-number">
                    <strong>
                        {{trans 'Order # : P%increment_id' increment_id=$order.increment_id |raw}}
                    </strong>
                </p>
    

    This should solve your issue !

    UPDATE

    I think the only way is via a templatevar. Here is an example on how to it for new order email:

    on etc/di.xml

    <preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender"        type="vendor\module\Preferences\Emails\OrderSender" />
    

    At vendor\module\Preferences\Emails create file OrderSender.php

    <?php
    
    namespace vendor\module\Preferences\Emails;
    
    use Magento\Payment\Helper\Data as PaymentHelper;
    use Magento\Sales\Model\Order;
    use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
    use Magento\Sales\Model\Order\Email\Container\Template;
    use Magento\Sales\Model\Order\Email\Sender;
    use Magento\Sales\Model\ResourceModel\Order as OrderResource;
    use Magento\Sales\Model\Order\Address\Renderer;
    use Magento\Framework\Event\ManagerInterface;
    use Magento\Framework\DataObject;
    
    
    class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
    {
        protected $paymentHelper;
        protected $orderResource;
        protected $globalConfig;
        protected $addressRenderer;
        protected $eventManager;
        protected $_customerRepositoryInterface;
    
        /**
         * @param Template $templateContainer
         * @param OrderIdentity $identityContainer
         * @param Order\Email\SenderBuilderFactory $senderBuilderFactory
         * @param \Psr\Log\LoggerInterface $logger
         * @param Renderer $addressRenderer
         * @param PaymentHelper $paymentHelper
         * @param OrderResource $orderResource
         * @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
         * @param ManagerInterface $eventManager
         */
        public function __construct(
            Template $templateContainer,
            OrderIdentity $identityContainer,
            \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
            \Psr\Log\LoggerInterface $logger,
            Renderer $addressRenderer,
            PaymentHelper $paymentHelper,
            OrderResource $orderResource,
            \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
            ManagerInterface $eventManager,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
        ) {
            parent::__construct( $templateContainer, $identityContainer, $senderBuilderFactory, $logger, $addressRenderer, $paymentHelper, $orderResource, $globalConfig, $eventManager);
            $this->paymentHelper = $paymentHelper;
            $this->orderResource = $orderResource;
            $this->globalConfig = $globalConfig;
            $this->addressRenderer = $addressRenderer;
            $this->eventManager = $eventManager;
            $this->_customerRepositoryInterface = $customerRepositoryInterface;
        }
    
        /**
         * Prepare email template with variables
         *
         * @param Order $order
         * @return void
         */
        protected function prepareTemplate(Order $order)
        {
            parent::prepareTemplate($order);
    
            // Add isquotation variable
            $templateVars = $this->templateContainer->getTemplateVars();
    
            $custom_increment_id = $order->getData('increment_id');
                $templateVars['custom_increment_id '] = 'P'. $custom_increment_id ;    
            
        }
    }
    

    On your code:

    <p class="order-number">
                    <strong>
                        {{trans 'Order # : %increment_id' increment_id= custom_increment_id |raw}}
                    </strong>
                </p>
    

Related Questions