Magento 用代码批量 添加产品的评论review(通过SKU)

本文解决了一个在Magento中使用前台评论功能时,store_id自动为0而非预期的1的问题。通过手动设置store_id和理解Magento获取store_id的方式,实现了正确的评论添加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:在网站前台添加评论 review_store 表 要添加2条数据。对比发现 自己的代码  添加的  store_id自动 为0(正常应该有2条,还有一条 store_id为1的)

这里的 1和0 ,分表Magento前台和后台。

所以我 指定了 $StoreId =1; 默认  storeid 是通过

Mage::app()->getStore()->getId() 获取的。

在 \app\code\core\Mage\Review\controllers\ProductController.php 

postAction() 方法是处理 前台用户的评论的。

在这个方法里 Mage::app()->getStore()->getId() 获取的值为 1。

所以,我这里 指定了 $storeid = 1。

具体实现代码如下:

<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 



//数据
$data = array(
		"form_key" => "AGPcNKVldb5KkWBA",
		"ratings" => array(3=>'15',2=>'10',1=>'5'),
		"nickname" => "jack",
		"title" => "HB82429",
		"detail" => "good"
	);

$rating = array(3=>'15',2=>'10',1=>'5');	
	
//产品模型对象
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$data['title']);

//StoreId手动设置
$StoreId = '1';

//review模型
$review  = Mage::getModel('review/review')->setData($data);
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
                ->setEntityPkValue($product->getId())
                ->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
                ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
                ->setStoreId($StoreId)
                ->setStores(array($StoreId))
                ->save();
//循环写入rating表
foreach ($rating as $ratingId => $optionId) {
    Mage::getModel('rating/rating')
    ->setRatingId($ratingId)
    ->setReviewId($review->getId())
    ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
    ->addOptionVote($optionId, $product->getId());
}
$review->aggregate();//集合处理
这个是基础核心代码,批量添加评论功能或者去其他网站抓取同sku产品然后写入评论等功能。可在此基础上扩展。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值