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'

1 Answer 1


Ok I found it,

this example my code, it will 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;
                'main_table.entity_id = sales_order_item.order_id'
            )->where('product_id = '.$product_id);

        foreach ($orderCollection as $order) {
            echo $order->getIncrementId() . ' <br/>';

Hope this help for someone in the next time.

Your Answer

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

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