0

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.

6
  • check this link - ordercollectionfactory use - magento.stackexchange.com/a/160609/85907 Commented Aug 9, 2020 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 Commented Aug 9, 2020 at 19:08
  • Actually i want to use order and product collection togather @Mohit Commented Aug 9, 2020 at 19:09
  • You can use two class together Commented Aug 10, 2020 at 4:14
  • can you suggest me any example for that it will help @Amit Commented Aug 10, 2020 at 4:55

1 Answer 1

0

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 ) { parent::__construct($context); $this->orderCollection = $orderCollection; $this->productCollection = $productCollection; $this->logger = $logger; } public function execute() { $collection = $this->orderCollection->create() ->addAttributeToSelect('*') ->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)); } } } } 
5
  • will implement this will you please help me with magento.stackexchange.com/questions/319422/… this question @Bushra Commented Aug 11, 2020 at 15:53
  • Please share the saveSimpleNote method implementation for the above query. Commented Aug 11, 2020 at 16:45
  • Updated my code please check tell me anything needed @Bushra Commented Aug 11, 2020 at 16:59
  • Did the above code work for you? Commented Aug 15, 2020 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 Commented Aug 15, 2020 at 14:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.