0

Here am using if condition to redirect if product attribute option value is !5431 but attribute is not echo any value here.

<?php

namespace Softadroit\Prescription\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;

class Orderplaceafter implements ObserverInterface
{
    protected $_responseFactory;
    protected $_url;
    protected $_order;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Sales\Api\Data\OrderInterface $order
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_order = $order;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {       
        $event = $observer->getEvent();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session');
        $_quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
                        
        $orderid = $observer->getEvent()->getOrderIds();
        $order = $this->_order->load($orderid);
        
        foreach($order->getItemsCollection() as $_item){        
        $product = $_item->getProductId();          
        //echo $_item->getName(); die();
        
        //$is_priscription =  $_item->getProduct()->getMyCustomAttribute('prescription');       
        
        //echo $_item->getProduct()->getMyCustomAttribute('prescription');
        //die();
        
        $is_priscription = $_item->getProduct()->getAttributeText('prescription');
        
        //echo "call";
        //echo $_item->getProduct()->getPrescription();
        //exit;
        
        if($is_priscription == "" && $is_priscription != '5431'){
             $redirectionUrl =  $this->_url->getUrl('order-success');
             $this->_responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
             //return $this;
              return;
            }
        }       
        
        $order = $_checkoutSession->getLastRealOrder();
        $quote = $_quoteFactory->create()->loadByIdWithoutStore($order->getQuoteId());
        
        if ($quote->getId()) {
            $quote->setIsActive(1)->setReservedOrderId(null)->save();
            $_checkoutSession->replaceQuote($quote);
            $url = $this->_url->getUrl('prescription/index'); //('[ModuleName]/[ModuleName]/[[Action]');
            $this->_responseFactory->create()->setRedirect($url)->sendResponse();
            die();
        }
    }
}
12
  • Just add echo "call";exit; and check it file call or not. Commented Aug 31, 2020 at 11:48
  • Try echo $_item->getProduct()->getData('prescription'); Commented Aug 31, 2020 at 11:49
  • @RohanHapani Call get echo Commented Aug 31, 2020 at 11:51
  • @HamendraSunthwal this display some other data but not attribute value Commented Aug 31, 2020 at 11:53
  • echo $_item->getProduct()->getPrescription(); Commented Aug 31, 2020 at 11:55

1 Answer 1

1

Use the below code :

To get attribute object :

$attribute = $_item->getProduct()->getResource()->getAttribute('your-attribute-code'); // It will return attribute object.

To get attribute option id :

$attribute->getSource()->getOptionId('your-attribute-option-label');

To get attribute value :

echo $_item->getProduct()->getResource()->getAttribute('your-attribute-code')->getFrontend()->getValue($_item->getProduct());
0

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.