1

The answer is here

I want to check my current cart products with customer already bought products for disallow repeated buyers for specified category.

I'm getting current cart products like this

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// retrieve quote items array
$items = $cart->getQuote()->getAllItems();   //citem means Cart items :)

foreach($items as $item) {   
   echo $item->getProductId().'<br />';   // current product id


}

Help me to get customer already bought products list

Thanks in Advance

2
  • Did you mean order history ? – MGento Nov 3 '17 at 9:46
  • Yes mr @MGento i want to get order history by customer – ubakara samy Nov 3 '17 at 9:59
3

Using below code, you will get all previously purchased products of logged in customer.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create("Magento\Customer\Model\Session");
$customerId = $customerSession->getCustomerId();
$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection()
            ->addFieldToFilter("customer_id", $customerId);

$products = array();
foreach ($orders as $order) {
    foreach ($order->getAllVisibleItems() as $item) {
        $products[] = $item->getProductId();
    }
}
$product_list = array_unique($products);
print_r($product_list);
5
  • Thanks @Dinesh Yadav, But i can't see the results, i'm working on onepage.phtml from megento module-checkout – ubakara samy Nov 3 '17 at 9:13
  • @ubakarasamy check if the current customer has a previous order or not? – Dinesh Yadav Nov 3 '17 at 9:15
  • screenshots.firefox.com/Va3rBYnNntD1vn19/localhost Yes @Dinesh Yadav current customer have previous orders – ubakara samy Nov 3 '17 at 9:29
  • Can you post your updated code in your question? – Dinesh Yadav Nov 3 '17 at 9:30
  • @ubakarasamy If my answer helped you, you can accept the answer so it can help others also. – Dinesh Yadav Nov 3 '17 at 12:05

Your Answer

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

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