I have created a custom attribute in product and saved a string in that value and I want if some one order those product of same value string i want that collection of product. So, i want to use

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory

these together is there a way to get these product.

  • check this link - ordercollectionfactory use - magento.stackexchange.com/a/160609/85907 – Mohit Patel Aug 9 at 19:03
  • Thanks for the response but i am Getting this error Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'custom_field' in 'where clause', query was: SELECT main_table.* FROM sales_order AS main_table WHERE (custom_field = 'testfield') @MohitPatel – Prits Aug 9 at 19:08
  • Actually i want to use order and product collection togather @Mohit – Prits Aug 9 at 19:09
  • You can use two class together – Amit Bera Aug 10 at 4:14
  • can you suggest me any example for that it will help @Amit – Prits Aug 10 at 4:55

I hope it would be helpful for you. In below code I have used orderCollectionFactory to retrieve custom attribute value:

namespace Vendor\ModuleName\Model;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class YOURClass 
     * @var orderCollection
    private $orderCollection;
     * @var ProductCollection
    private $productCollection;
     * @var logger
    private $logger;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        ProductCollection $productCollection,
        \Psr\Log\LoggerInterface $logger
        $this->orderCollection = $orderCollection;
        $this->productCollection = $productCollection;
        $this->logger = $logger;

    public function execute()
        $collection = $this->orderCollection->create()
            ->addFieldToFilter('increment_id', '000000003');

        foreach ($collection->getItems() as $order) {
            foreach($order->getAllItems() as $item) {
                $this->logger->log('600', print_r($item->getProduct()->getData('custom_field'), true));
| improve this answer | |
  • will implement this will you please help me with magento.stackexchange.com/questions/319422/… this question @Bushra – Prits Aug 11 at 15:53
  • Please share the saveSimpleNote method implementation for the above query. – Bushra Aug 11 at 16:45
  • Updated my code please check tell me anything needed @Bushra – Prits Aug 11 at 16:59
  • Did the above code work for you? – Bushra Aug 15 at 13:35
  • It works only for loggedin user not for guest user if you are talking about this question magento.stackexchange.com/questions/319422/… I have done some other way around for this question.@Bushra – Prits Aug 15 at 14:18

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.