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))          //approved
->setCustomerId(null)                       //null is for administrator
->setLink($pressquote->link)                            //custom field
->setLinktext($pressquote->linktext)                   //custom field
->setYear($pressquote->year)                            //custom field

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

  • What went wrong should be in your php error log, perhaps in system.log in magento's var. – Melvyn Jan 21 '14 at 8:14

// Invoke the Magento environment

require_once 'app/Mage.php';

// 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');

// 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')
} catch (Exception $e) {
  • I am not importing from an external source and i don't need the rating... I just need reviews – Netorica Jan 21 '14 at 6:15
  • 2
    I have provide you review as well as rating if you don't need then just remove from your code It will work.. – Keyul Shah Jan 21 '14 at 8:35
$review = Mage::getModel('review/review');
$review->setEntityPkValue(147);//product id
$review->setStatusId(1); // approved
$review->setCustomerId(273);//null is for administrator




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.