TransWikia.com

How to add STORES > Attributes -> RATINGS > New Attributes (Rating / Price / XYZ) - [SOLVED]

Magento Asked on November 23, 2021

I searched alot on internet but did not find a solution for this and finally got this working on my own:

Requirement: Add a new attribute under STORES > RATINGS > (Rating i.e Not coming out of the box in magento)

Solution:
Add the below code in your module file under {namespace}{modulename}SetupPatchData;
Replace the strings with bracket in the code based on ur project needs.
rating_code – Is the name of the new attribute needed.

enter image description here

namespace {namespace}{modulename}SetupPatchData;

use MagentoFrameworkAppResourceConnection;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchVersionInterface;
use MagentoReviewModelRatingFactory;
use MagentoStoreApiStoreRepositoryInterface;

class AddRatingsAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var MagentoFrameworkSetupModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /** @var StoreRepositoryInterface */
    private $storeRepository;
    
    /**
     * PatchInitial constructor.
     * @param MagentoFrameworkSetupModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        MagentoFrameworkSetupModuleDataSetupInterface $moduleDataSetup,
        MagentoReviewModelRatingFactory $ratingFactory,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->_ratingFactory = $ratingFactory;
        $this->storeRepository = $storeRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $data = [
            MagentoReviewModelRating::ENTITY_PRODUCT_CODE => [
                ['rating_code' => 'Rating', 'position' => 0],
            ],
        ];
        foreach ($data as $entityCode => $ratings) {

            $entityId = $this->_ratingFactory->create()->getEntityIdByCode('product');
            
            foreach ($ratings as $bind) {
                //Fill table rating/rating
                $bind['entity_id'] = $entityId;
                $this->moduleDataSetup->getConnection()->insert(
                    $this->moduleDataSetup->getTable('rating'),
                    $bind
                );
                //Fill table rating/rating_option
                $ratingId = $this->moduleDataSetup->getConnection()->lastInsertId(
                    $this->moduleDataSetup->getTable('rating')
                );
                $optionData = [];
                for ($i = 1; $i <= 5; $i++) {
                    $optionData[] = ['rating_id' => $ratingId, 'code' => (string)$i, 'value' => $i, 'position' => $i];
                }
                $this->moduleDataSetup->getConnection()->insertMultiple(
                    $this->moduleDataSetup->getTable('rating_option'),
                    $optionData
                );
                
                $storeCode = {'ur store code for which you want to add this attribute'};
                $store = $this->storeRepository->get($storeCode);
                $storeid = $store->getId();
                $store_data[] = ['rating_id' => $ratingId, 'store_id' => $storeid];
                $this->moduleDataSetup->getConnection()->insertMultiple(
                    $this->moduleDataSetup->getTable('rating_store'),
                    $store_data
                );
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '2.0.0';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

One Answer

The below code is the solution to it

namespace namespacemodulenameSetupPatchData;

use MagentoFrameworkAppResourceConnection;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchVersionInterface;
use MagentoReviewModelRatingFactory;
use MagentoStoreApiStoreRepositoryInterface;

class AddRatingsAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var MagentoFrameworkSetupModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /** @var StoreRepositoryInterface */
    private $storeRepository;
    
    /**
     * PatchInitial constructor.
     * @param MagentoFrameworkSetupModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        MagentoFrameworkSetupModuleDataSetupInterface $moduleDataSetup,
        MagentoReviewModelRatingFactory $ratingFactory,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->_ratingFactory = $ratingFactory;
        $this->storeRepository = $storeRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $data = [
            MagentoReviewModelRating::ENTITY_PRODUCT_CODE => [
                ['rating_code' => 'Rating', 'position' => 0],
            ],
        ];
        foreach ($data as $entityCode => $ratings) {

            $entityId = $this->_ratingFactory->create()->getEntityIdByCode('product');
            
            foreach ($ratings as $bind) {
                //Fill table rating/rating
                $bind['entity_id'] = $entityId;
                $this->moduleDataSetup->getConnection()->insert(
                    $this->moduleDataSetup->getTable('rating'),
                    $bind
                );
                //Fill table rating/rating_option
                $ratingId = $this->moduleDataSetup->getConnection()->lastInsertId(
                    $this->moduleDataSetup->getTable('rating')
                );
                $optionData = [];
                for ($i = 1; $i <= 5; $i++) {
                    $optionData[] = ['rating_id' => $ratingId, 'code' => (string)$i, 'value' => $i, 'position' => $i];
                }
                $this->moduleDataSetup->getConnection()->insertMultiple(
                    $this->moduleDataSetup->getTable('rating_option'),
                    $optionData
                );
                
                $storeCode = {'ur store code for which you want to add this attribute'};
                $store = $this->storeRepository->get($storeCode);
                $storeid = $store->getId();
                $store_data[] = ['rating_id' => $ratingId, 'store_id' => $storeid];
                $this->moduleDataSetup->getConnection()->insertMultiple(
                    $this->moduleDataSetup->getTable('rating_store'),
                    $store_data
                );
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '2.0.0';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

Answered by shraddha on November 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP