0

When a new product is added in Magento catalog, it should get added to the wishlist of the customer and an order should be automatically created for it. I am using the following code. Can anyone help me with this?

<?php

namespace Wishorder\Asorder\Block;

use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\View\Element\Template\Context;
use \Wishorder\Asorder\Helper\Data;
class Cwish extends \Magento\Framework\View\Element\Template
{
    protected $_productFactory;
    protected $_wishlistRepository;
    protected $_customerFactory;
    protected $_helper;

    public function __construct(
        Context $context,
        \Magento\Wishlist\Model\WishlistFactory $WishlistFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Custom\Wlist\Helper\Data $helper,
        array $data = []       
    ) {
        $this->_wishlistFactory= $WishlistFactory;
        $this->_productFactory = $productFactory;
        $this->_customerFactory = $customerFactory;
        $this->_helper = $helper;
        parent::__construct($context, $data);
    }

    public function getHelloWorldTxt(){
        return 'Hello world!';
    }


    public function getProductCollection() 
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $collection = $this->_customerFactory->create()->getCollection();
        foreach ($collection as $cust) 
        { 
            $Email=$cust->getEmail(); 
            echo "Email : ".$Email.'<br/>';

            $Fname=$cust->getFirstname();
            $Lname=$cust->getLastname();
            echo "First Name : ".$Fname.'<br/>';
            echo "Last Name : ".$Lname.'<br/>';

            $CustomerModel->loadByEmail($Email);
            $userId = $CustomerModel->getId();
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $wishlist = $objectManager->get('\Magento\Wishlist\Model\Wishlist');
            $wishlist_collection = $wishlist->loadByCustomerId($userId, true)->getItemCollection();
            echo "Last Name : ".$userId.'<br/>';
            print_r($wishlist_collection); die;

            foreach ($wishlist_collection as $wlist)
            {
                $orderData=[
                    'currency_id'  => 'USD',
                    'email'        => $Email, //buyer email id
                    'shipping_address' =>[
                    'firstname'    => $Fname, //address Details
                    'lastname'     => $Lname,
                    'street' => 'xxxxx',
                    'city' => 'xxxxx',
                    'country_id' => 'IN',
                    'region' => 'xxx',
                    'postcode' => '43244',
                    'telephone' => '52332',
                    'fax' => '32423',
                    'save_in_address_book' => 1],
                    'items'=> ['product_id'=>$wlist->getProductId(),'qty'=>1]
                ];

                return $this->_helper->createOrder($orderData);
            }
        }
    }
}

1 Answer 1

0

Basically magento manage the order related information before placed order in Quote. So you need to create a quote programmatically and need to set shipping and billing address, shipping method, payment method etc. After that you can convert that quote to order by calling placeOrder($quoteId). For more information you can check here.

4
  • With the reference of above mentioned link, I have written my code. Order is creating but, I have to create order automatically for the items in every customer's wishlist.
    – Vignesh S
    May 8, 2018 at 5:10
  • You mean when customer added product to wishlist? May 8, 2018 at 5:27
  • When a new product is added, it should be added to the customer's wishlist. Then order should be automatically created for the products in the wishlist.
    – Vignesh S
    May 8, 2018 at 5:30
  • then you can use the the event product_save_after to generate the order after adding product to wish list. May 8, 2018 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.