0

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.

2

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

3
  • Haven't had a chance to try this out, but it looks spot-on. Thank you.
    – brackfost
    Oct 7 '18 at 20:55
  • This helped me get to where I needed to be, thank you!
    – brackfost
    Oct 9 '18 at 20:01
  • welcome @BrockfastCowboy
    – Hitesh
    Oct 10 '18 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.