Question

I am working with an observer where i have to disable a payment method if user has no order existed and show it if at least one order existed.

I am trying something like this but not able to get the order collection for current logged in user, i have tried printing in logger but getting Epmty Array there.

<?php

namespace Apriljune\Payments\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session;


class PaymentMethodAvailable implements ObserverInterface
 {
 /**
 * @var Session
 */
private $session;


protected $logger;

/**
 * RemoveGuestViewPageBlocks constructor.
 *
 * @param \Magento\Customer\Model\Session $session
 */
public function __construct(
    Session $session,
    \Psr\Log\LoggerInterface $logger
){
    $this->session = $session;
     $this->logger = $logger;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    
    $orderCollection = $objectManager->get('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');

    $customerId = $this->session->getCustomer()->getId();

    $order_collection = $orderCollection->create()->addAttributeToFilter('customer_id', $customerId );
            
    $itemCount = $order_collection->getSize();


    $this->logger->log(100, json_encode($order_collection));

    $this->logger->info("CustomerId=>".$customerId."<");

    if( $observer->getEvent()->getMethodInstance()->getCode() =="cashondelivery" ) {
        $checkResult = $observer->getEvent()->getResult();
        if( $itemCount < 1 ) {
            $checkResult->setData('is_available', false); 
        }
    }
  }
}

can Anyone Identify what wrong i am doing ,

I have also tried in this way as well but still not working:

<?php

 namespace Apriljune\Payments\Observer;

 use Magento\Framework\Event\Observer;
 use Magento\Framework\Event\ObserverInterface;
 use Magento\Customer\Model\Session;


 class PaymentMethodAvailable implements ObserverInterface
 {
 /**
 * @var Session
 */
private $session;


protected $logger;

protected $_orderCollectionFactory;

/**
 * RemoveGuestViewPageBlocks constructor.
 *
 * @param \Magento\Customer\Model\Session $session
 */
public function __construct(
    Session $session,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
){
    $this->session = $session;
    $this->logger = $logger;
    $this->_orderCollectionFactory = $orderCollectionFactory;
    parent::__construct($context);
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
     $customerId = $this->session->getCustomer()->getId();
     $collection = $this->orderCollectionFactory()->create($customerId)
     ->addFieldToSelect('*')
     ->addFieldToFilter('status',
            ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
        )
     ->setOrder(
            'created_at',
            'desc'
        );
            
    $itemCount = $collection->getSize();


    $this->logger->log(100, json_encode($collection ));

    $this->logger->info("CustomerId=>".$customerId."<");

    if( $observer->getEvent()->getMethodInstance()->getCode() =="cashondelivery" ) {
        $checkResult = $observer->getEvent()->getResult();
        if( $itemCount < 1 ) {
            $checkResult->setData('is_available', false); 
        }
      }
    }
 }

Also not working with this approach.

Any Help regarding this will be appreciated.

Thanks!

No correct solution

OTHER TIPS

Try wil order Repository:

$searchCriteria = $this->searchCriteriaBuilder
        ->addFilter('customer_id', $customerId)->create();
try {
    $order = $this->orderRepository->getList($searchCriteria);

    if ($order->getTotalCount()) {
        /** Collection of orders */    
        $order->getItems();
    }
} catch (\Exception $e) {
  echo. $e->getMessage();
}

NOTE: Inject $this->searchCriteriaBuilder and $this->orderRepository in custructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top