magento2.3 – Add block to order success page

To fulfill your requirements, your can follow below steps and you need to use event observer.

  1. Create a Block
    app/code/StackExchange/Magento/Block/Checkout/Success.php which is
    responsible for render content.

Code

<?php

namespace StackExchangeMagentoBlockCheckout;

use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext ;
use MagentoSalesApiDataOrderInterface;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoSalesModelOrder;

class Success extends Template
{
    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    public function __construct(
        OrderRepositoryInterface $orderRepository,
        Context $context, array $data = ())
    {
        parent::__construct($context, $data);
        $this->orderRepository = $orderRepository;
    }

    /**
     * @param $orderId
     * @return false|OrderInterface
     */
    public function getOrderById($orderId)
    {
        try {
            $order = $this->orderRepository->get($orderId);
        }catch (NoSuchEntityException $exception){
                return false;
        }
        return  $order;
    }
    public function getCmsBlock($identifier)
    {
        $cmsBlock = $this->_layout->createBlock(MagentoCmsBlockBlock::class)
            ->setBlockId($identifier)
            ->toHtml();
        return $cmsBlock;
    }
}

  1. Create a phtml which where the cms block will be rendered based on
    condition

path like : app/code/StackExchange/Magento/view/frontend/templates/custom-block.phtml

Code

<?php
/**
 * @var  StackExchangeMagentoBlockCheckoutSuccess $block
 */
?>

<?php if (!empty($block->getOrderIds())):?>
    <?php foreach ($block->getOrderIds() as $orderId):?>
            <?php $order = $block->getOrderById((int) $orderId);?>
            <?php if ($order):?>
                 <?php /**  @var MagentoSalesModelOrderItem $item */ ?>
                <?php foreach($order->getAllVisibleItems() as $item) { ?>
                    <?php $product = $item->getProduct() ;?>
                    <?php if (null !== $product):?>
                            <?php if ($product->getAttributeSetId() === 'SomeId'){ ?>
                                    // Static block'
                                    <?=  $block->getCmsBlock('My_CMS_BLOCK_IDENTIFIER')?>
                            <?php  } ?>
                     <?php endif; ?>
                <?php } ?>
            <?php endif; ?>
    <?php endforeach ;?>
<?php endif; ?>
  1. Most important, Create an observer which is passed order id to step1 Block class.

Observer class path: app/code/StackExchange/Magento/Observer/Frontend/Checkout/OnepageControllerSuccessAction.php

code

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace StackExchangeMagentoObserverFrontendCheckout;

use MagentoFrameworkEventObserver;
use MagentoFrameworkViewLayoutInterface;

class OnepageControllerSuccessAction implements MagentoFrameworkEventObserverInterface
{
    /**
     * @var LayoutInterface
     */
    private $layout;

    public function __construct(LayoutInterface $layout)
    {
        $this->layout = $layout;
    }
    /**
     * Execute observer
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(
        Observer $observer
    ) {
        $orderIds = $observer->getEvent()->getOrderIds();
        if (empty($orderIds) || !is_array($orderIds)) {
            return;
        }
        $block = $this->layout->getBlock('checkout.success.custom.block');
        if ($block) {
            $block->setOrderIds($orderIds);
        }
    }
}

  1. Next to create an event.xml file where define observer.

File path: app/code/StackExchange/Magento/etc/frontend/events.xml

code

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        si:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="pass_order_to_success"
                  instance="StackExchangeMagentoObserverFrontendCheckoutOnepageControllerSuccessAction"/>
    </event>
</config>

5.Last step to create a layout which is responsible for content to success page

File Path: app/code/StackExchange/Magento/view/frontend/layout/checkout_onepage_success.xml

code

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="StackExchangeMagentoBlockCheckoutSuccess"
                   name="checkout.success.custom.block"
                   template="StackExchange_Magento::custom-block.phtml"/>
        </referenceContainer>
    </body>
</page>