1

Is there any idea (SQL-statement) or extension on how to show all customers that bought a specific product (id) in Magento 2.1.5

Thanks!

1

Try this

class .................
{

    protected $_productCollectionFactory;
    protected $_productCollectionFactory;
    protected $_productRepository;

    public function __construct(

        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
        \Magento\Catalog\Model\ProductRepository $productFactory

    ) {

        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_collectionFactory = $collectionFactory;
        $this->_productRepository = $productFactory;
    }


        function execute()
        {


                // Best Selling Products Overall

                $bestSellerProdcutCollection = $this->_productCollectionFactory->create()
                 ->addAttributeToSelect('*');

                 foreach($bestSellerProdcutCollection as $bestpdt)
                 {
                    $bestpdt->getId();
                 }



                // Get frequently boughted Products
                 $frequentlyBoughtedProducts = $this->_collectionFactory->create()
                  ->setModel('Magento\Catalog\Model\Product')
                  ->setPeriod('month'); //you can add period daily,yearly

                  foreach($frequentlyBoughtedProducts as $frqpdt)
                 {
                    $frqpdt->getId();
                 }

           }
    }

Customize Based on your requirement ,Hope this help you. Thanks

  • Thanks, Shafeel, maybe there is the possibility to get this via SQL – that would be easier for my purposes, but many thanks anyway! – user3631648 Aug 15 at 10:37
0

You can use below sql query to get order_idand customer_email of customers who bought a specific product with product_id 100.

select order_id, customer_email from sales_order_item JOIN sales_order on sales_order_item.order_id = sales_order.entity_id where sales_order_item.product_id = 100;

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.