0

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}\Setup\Patch\Data; 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}\Setup\Patch\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use \Magento\Review\Model\RatingFactory;
use Magento\Store\Api\StoreRepositoryInterface;

class AddRatingsAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /** @var StoreRepositoryInterface */
    private $storeRepository;
    
    /**
     * PatchInitial constructor.
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
        \Magento\Review\Model\RatingFactory $ratingFactory,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->_ratingFactory = $ratingFactory;
        $this->storeRepository = $storeRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $data = [
            \Magento\Review\Model\Rating::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 [];
    }
}

1 Answer 1

0

The below code is the solution to it

namespace namespace\modulename\Setup\Patch\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use \Magento\Review\Model\RatingFactory;
use Magento\Store\Api\StoreRepositoryInterface;

class AddRatingsAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /** @var StoreRepositoryInterface */
    private $storeRepository;
    
    /**
     * PatchInitial constructor.
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
        \Magento\Review\Model\RatingFactory $ratingFactory,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->_ratingFactory = $ratingFactory;
        $this->storeRepository = $storeRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $data = [
            \Magento\Review\Model\Rating::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 [];
    }
}

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.