как я могу получить список уже купленных товаров для клиентов в magento2


Ответ здесь

Я хочу проверить мои текущие товары в корзине с уже купленными клиентами товарами , чтобы запретить повторным покупателям для указанной категории.

Я получаю текущие товары в корзине, подобные этому

$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


}

Помогите мне получить список уже купленных товаров для клиентов

Заранее благодарю

 1
Author: Mohamed El Mrabet, 2017-11-03

1 answers

Используя приведенный ниже код, вы получите все ранее приобретенные продукты зарегистрированного клиента.

$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);
 3
Author: Dinesh Yadav, 2020-02-22 04:21:28