1

There is an event that checks if the goods in the basket when we go to the product page. If there is an attribute in the goods that allows to deliver the goods on the day of the order, if this attribute is included in the product, then it returns "1" if it is disabled, then there is nothing. Now how to show the popup when you click on "add to cart" on the product page if there is already a product in the basket with the possibility of delivery today, and the second product that we want to add does not have this possibility. And on the pop-up there should be two buttons, "Continue" or "Cancel". If the user clicks "Continue" then we add the item to the basket, and if the user clicks "Cancel", then we close the popup and there is nothing to add to the basket.

Observer->DayDelivery.php

namespace Ronis\SameDayDelivery\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class DayDelivery implements ObserverInterface
{
    protected $resultPageFactory;

    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $_cart;
    protected $_productCollectionFactory;
    protected $_resultJsonFactory;

    /**
     * [__construct description]
     * @param \Magento\Framework\App\Action\Context $context [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
     * @param \Magento\Checkout\Model\Cart $cart [description]
     */
    public function __construct(
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Cart $cart,
        CollectionFactory $productCollectionFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_cart = $cart;
        $this->_resultJsonFactory = $resultJsonFactory;
        $this->_productCollectionFactory = $productCollectionFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/samDayDelivery.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $cartProductIds = $this->_cart->getQuoteProductIds();
        $productCollection = $this->_productCollectionFactory->create();
        if (!$cartProductIds) {
            return false;
        } else {
            $productCollection->addFieldToFilter('entity_id', ['in' => [$cartProductIds]]);
            $productCollection->addAttributeToSelect('allow_same_day_delivery');
            foreach ($productCollection as $item) {
                $id = $item->getId();
                $check = $item->getData('allow_same_day_delivery');
                $logger->info('Delivery sam day id: ' . $id . ' status ' . $check);
            }
//            $result = $this->_resultJsonFactory->create()->setData(['Shipment check' => $check]);
//            return $result;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.