1

I want to create product review and rating programmatically. I was able to create review programmatically by the following code snippet

<?php
ini_set('memory_limit', '128M');
require_once 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(1); //desired store id
$review = Mage::getModel('review/review');
$review->setEntityPkValue(1);//product id
$review->setStatusId(1);
$review->setTitle("mytitle");
$review->setDetail("mydetail");
$review->setEntityId(1);                                      
$review->setStoreId(Mage::app()->getStore()->getId());      //storeview              
$review->setStatusId(1); //approved
$review->setCustomerId(1);
$review->setNickname("Menickname");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->save();
$review->aggregate();
?>

But I don't know how to create rating programmatically. I'm totally blank about it. Can anyone help on this?

2

You need to loop through all the voting options and create a rating for each option selected and like it back to your review.

foreach ($rating as $ratingId => $optionId) {
     Mage::getModel('rating/rating')
          ->setRatingId($ratingId)
          ->setReviewId($review->getId())
          ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
          ->addOptionVote($optionId, $product->getId());
}

To get all the ratings you can use the rating's resource collection:

    $ratingCollection = Mage::getModel('rating/rating')
        ->getResourceCollection()
        ->addEntityFilter('product')
        ->setPositionOrder()
        ->addRatingPerStoreName(Mage::app()->getStore()->getId())
        ->setStoreFilter(Mage::app()->getStore()->getId())
        ->load()
        ->addOptionToItems();

Then for each rating object you can call getOptions() to get each possible option for each rating.

  • Can you make it as a code snippet,Because I'm getting exception when using like this. – SIBHI S Jun 26 '14 at 5:31
  • I'm getting this -> SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'value' cannot be null – SIBHI S Jun 26 '14 at 6:24
  • @SIBHIS make sure that the $optionId is a valid value for the rating. For example if in the default database rating id 3, which is price has the options 11, 12, 13, 14 and 15 so the option vote value need to be one of these. – David Manners Jun 26 '14 at 7:54
1

Note: Create product review and rating programmatically

function generateReview()
{
    $allStores = Mage::app()->getStores();
    foreach ($allStores as $_eachStoreId => $val) {
        $allStoresId[] = Mage::app()->getStore($_eachStoreId)->getId();
    }
    $customerId = 120;
    $_ratingValue = 3;

    /* $_customer =  Mage::getModel('customer/customer')->load($customerId); */
    // IMPORTANT: Set up customer session. The rating/option model resource checks the customer session to get the customer ID.
    /* Mage::getSingleton('customer/session')
        ->setCustomer($_customer)
        ->setCustomerAsLoggedIn($_customer);
    */

    if (!$sku){
       return  'Please pass a valid Product SKU';
    } else {
        $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

        $_review = Mage::getModel('review/review')
            ->setEntityPkValue($_product->getId())                
            ->setTitle('title')
            ->setDetail('detail')
            ->setEntityId(1) // review_entity: 1 - Product
            ->setStatusId(2) // pending
            ->setStoreId(14) //set default store Id, if multiple stores
            ->setStores($allStoresId)
            ->setCustomerId($customerId) //null is for administrator if store Id is 'O' 
            ->setNickname($nickname)
            ->save();

        // Add review Ratting
        $_ratingOptions = array(
            1 => array(1 => 1,  2 => 2,  3 => 3,  4 => 4,  5 => 5),
            2 => array(1 => 6,  2 => 7,  3 => 8,  4 => 9,  5 => 10),
            3 => array(1 => 11, 2 => 12, 3 => 13, 4 => 14, 5 => 15),
        );

        foreach ($_ratingOptions as $_ratingId => $_optionIds) {
            try {
                Mage::getModel('rating/rating')
                    ->setRatingId($_ratingId)
                    ->setReviewId($_review->getId())
                    ->addOptionVote($_optionIds[$_ratingValue], $_product->getId());
            } catch (Exception $e) {
                $this->log($e->getMessage());
            }
        }
        $_review->aggregate();
    }
}

Reference : http://blog.mdnsolutions.com/migrate-products-reviews-and-ratings-programmatically/

  • May i know how to import product reviews & ratings bulk import programmatically in Magento 1? @Pradeep Thakur. – Gem May 25 at 8:06

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.