Question

I'd like to programatically edit all user reviews, to strip them of HTML tags (brought over from a migration). It doesn't look like exporting/importing with a CSV is possible, so I'm hoping for a programatic solution.

Thanks.

Was it helpful?

Solution

Add below code where you need it.

I added below code into front index controller

    protected $objectManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
       parent::__construct($context);
    }

    public function execute()
    {
        /* If you want by particular product's sku
        $sku = 'test-product'; // product sku
        $product = $objectManager->create("Magento\Catalog\Model\Product")->loadByAttribute('sku', $sku); //you can also use load($id) if you know product id*/
        $rating = $this->_objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
        $collection = $rating->create()
                //->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
                //->addEntityFilter('product', $product->getId())
                ->setDateOrder();
        foreach ($collection as $key => $value) {
            $newTitle = $value->getTitle().'-- Edited by programmatically';
            $_review = $this->_objectManager->get("Magento\Review\Model\Review")
            ->setReviewId($value->getReviewId())
            ->setEntityId($value->getEntityId())
            ->setStatusId($value->getStatusId())
            ->setTitle($newTitle)
            ->setDetail($value->getDetail())
            ->setNickname($value->getNickname())
            ->save();
        }
}

run this command rm -rf generated for above Magento 2.2.0 version

run this command rm -rf var/generation for below Magento 2.2.0 version

let me know if you have any query

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top