2

I want join order collection, order item collection and product collection. my goal is get all orders based product id.

example : my product_id is '123', and I want know all order number contain product_id '123'

5

Ok I found it,

this example my code, this get order numbers based product id

....
protected $_order;
....
 public function __construct( 
    .....,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $order,
    ......
    )
   {
    ......
    $this->_order  = $order;
    ......
   }


 public function execute()
 {

    $orderCollection = $this->_order->create();

    $product_id = 123;
    $orderCollection->getSelect()
            ->join(
                'sales_order_item',
                'main_table.entity_id = sales_order_item.order_id'
            )->where('product_id = '.$product_id);

        $orderCollection->getSelect()->group('main_table.entity_id');
        foreach ($orderCollection as $order) {
            echo $order->getIncrementId()' <br/>';
        }
    }

Hope this help for someone in the next time.

| improve this answer | |

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.