1

I want to add product reviews programatically with some custom fields defined on my code below

$review = Mage::getModel('review/review')
->setEntityPkValue(intval($pressquote->product_id)) //product id
->setStatusId(intval($pressquote->status))
->setDetail($pressquote->text)
->setTitle('')
->setEntityId(1)                     
->setStatusId(intval($pressquote->status))          //approved
->setCustomerId(null)                       //null is for administrator
->setNickname($pressquote->source)
->setLink($pressquote->link)                            //custom field
->setLinktext($pressquote->linktext)                   //custom field
->setYear($pressquote->year)                            //custom field
->setStoreId(1)                                          
->setStores(array('base'))                              
->save();

but it doesn't work for me.. I checked my code and it stops on save(). What went wrong? I tried already removing the custom fields but nothing happened

Netorica
  • 471
  • 1
  • 7
  • 24
  • What went wrong should be in your php error log, perhaps in system.log in magento's var. –  Jan 21 '14 at 08:14

3 Answers3

9

// Invoke the Magento environment

require_once 'app/Mage.php';
Mage::app();

// Set up your own loop to to go through the reviews from the source cart.

// Logic to look up customer that has already been migrated into magento. // So you have $_customer holding a Mage_Customer_Model_Customer

// IMPORTANT: Set up customer session. // the rating/option model resource checks the customer session to get the customer ID.

$_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);

// Add the review

$_review = Mage::getModel('review/review');
->setEntityPkValue($_product->getId())
->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
->setTitle($row_source_review['Title'])
->setDetail($row_source_review['Review'])
->setEntityId(1)
->setStoreId($store)
->setStores(array($store))
->setCustomerId($_customer->getId())
->setNickname($_customer->getFirstname())
->save();

// Map your rating_id to your option_id with an array or something

$rating_options = array(
1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15)
);

// Now save the ratings

foreach($rating_options as $rating_id => $option_ids):
try {
    $_rating = Mage::getModel('rating/rating')
        ->setRatingId($rating_id)
        ->setReviewId($_review->getId())
        ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
} catch (Exception $e) {
    die($e->getMessage());
}
endforeach;
Keyul Shah
  • 7,219
  • 12
  • 37
  • 60
3
$review = Mage::getModel('review/review');
$review->setEntityPkValue(147);//product id
$review->setStatusId(1); // approved
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId(1);                                      
$review->setStoreId(Mage::app()->getStore()->getId());                    
$review->setCustomerId(273);//null is for administrator
$review->setNickname("Me");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));                    
$review->save();

$review->aggregate();

Reference::

http://www.magentocommerce.com/boards/viewthread/199171/

siliconrockstar
  • 550
  • 4
  • 10
0

To expand on this a bit for those needing help with understanding the model here

$rating_options = array(
1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15)
);

These "Rating Options" are from the Mage_Rating_Model_Rating model. A collection of these will yield whatever you have in Catalog > Reviews and Ratings > Manage Ratings.

That being said we can query for a collection of those models and build something a little more programmatic...

/** @var $readConnection Varien_Db_Adapter_Pdo_Mysql */
$readConnection = Mage::getModel('core/resource')->getConnection('core/read');
$ratingOptions  = Mage::getModel('rating/rating')->getCollection();

foreach ($ratingOptions as $ratingOption) {
    $select = new Zend_Db_Select($readConnection);
    $select->from('rating_option');
    $select->columns('option_id');
    $select->where('code=?', $VALUE_FROM_ONE_TO_FIVE);
    $select->where('rating_id=?', $ratingOption->getRatingId());

    $optionId = $readConnection->fetchOne($select);

    $rating = Mage::getModel('rating/rating')
        ->setRatingId($ratingOption->getRatingId())
        ->setReviewId($newReview->getId())
        ->addOptionVote($optionId, $_product->getId());
}

$review->aggregate();

The above will get all the rating options, then select the appropriate "option_id" with the $VALUE_FROM_ONE_TO_FIVE that you specify for each "category" of review (also known as a vote).

You could build an array or something to replace for $VALUE_FROM_ONE_TO_FIVE but you'd need to know what "category" you're looking for. For that you could use

$ratingType = $ratingOption->getRatingCode();

which will return Performance, Price, or whatever other "Rating Name" you have established in Catalog > Reviews and Ratings > Manage Ratings

Something like:

$ratingType = $ratingOption->getRatingCode();
switch($ratingType) {
    case 'Price':
        $VALUE_FROM_ONE_TO_FIVE = 3;
        break;
    case 'Performance':
        $VALUE_FROM_ONE_TO_FIVE = 1;
        break;
    ...
}