0

Load product by attribute i have using the following code

$attribute ='test1'; 
$objectManager =\Magento\Framework\App\ObjectManager::getInstance(); 
$product =$objectManager->get('Magento\Catalog\Model\Product'); 
$product = $product->loadByAttribute('attributename',$attribute]);

but i have the format of values in attribute name field like

test1|test2|test3

then how do i check this loadByAttribute() method

<?php
namespace Vendor\Module\Controller\Adminhtml\EditComments;

use Magento\Framework\App\Action\Action;
use Magento\Backend\App\Action\Context;
use Vendor\Module\Model\ResourceModel\Post\CollectionFactory;
use Magento\Framework\Controller\ResultFactory; 


class SaveEdit extends \Magento\Backend\App\Action
{
    const URL_PATH_EDIT = 'catalog/product/edit';
    protected $_modelFactory;
    protected $urlBuilder;


    public function __construct(
        Context $context,
        \Magento\Framework\UrlInterface $urlBuilder,
        CollectionFactory $modelFactory
    ) {

         parent::__construct($context);
        $this->_modelFactory = $modelFactory;
        $this->urlBuilder = $urlBuilder;
    }

    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $data  = $this->getRequest()->getParam('custom_table');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $product = $objectManager->get('Magento\Catalog\Model\Product');
       $product = $product->loadByAttribute('customattribute',$data['customattribute']);
        // echo $data['customattribute'];
        // echo "<pre>";
        // print_r(get_class_methods($product)); 
        //die;
        $productId = $product->getId();
        if (isset($data['customattribute'])) {
            $collections = $this->_modelFactory->create();
            foreach ($collections as $model) {
            $model = $model->load($data['customattribute']);
            $model->setData('customattribute', $data['customattribute']);
            $model->setData('comments', $data['comments']);
            $model->save();
            }

        }
        $this->messageManager->addSuccess(__('comments has been updated.'));
        if ($productId) {
            $resultRedirect->setPath(
                'catalog/product/edit',
                   ['id' =>  $productId]

            );
        }
        //$resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}
12
  • you means load products by multiple attributes ??? May 6, 2020 at 17:42
  • No Mohit Patel ,i need how to check multiple values for ex: 'attributename ' this attribute have the values in test1|test2|test3 this format product->loadByAttribute('attributename',$attribute]); $attribute = test then i have to load product it shows error If i have the value of 'attributename' only single value it load the product
    – kanidhaya
    May 7, 2020 at 4:26
  • i am not sure but try this $product = $product->loadByAttribute('attributename',$attribute]); echo $product . "|"; May 7, 2020 at 4:30
  • Thanks but its not working
    – kanidhaya
    May 7, 2020 at 4:34
  • what's showing ??? May 7, 2020 at 4:35

0

Your Answer

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

Browse other questions tagged or ask your own question.