我发现下面的脚本并且据说它有效,但我只是不确定他们在哪里或如何让文件/脚本工作,并希望有人可以帮助我解决问题。
有多种方法可以将内容导入Magento - 通过自定义xml数据文件或仅通过php脚本,这就是说它是什么,但我很好奇它从哪个数据库中提取数据?
<?php
ini_set('memory_limit', '128M');
require_once 'path-to-Mage.php';
Mage::app();
Mage::app()->setCurrentStore(4); //desired store id
$review = Mage::getModel('review/review');
$review->setEntityPkValue(147);//product id
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId(1);
$review->setStoreId(Mage::app()->getStore()->getId());
$review->setStatusId(1); //approved
$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();
?>
所以,如果这是我的PHP脚本,我把它放在哪里?有人可以帮我制定我用于导入的xml脚本吗?
提前感谢您提供任何帮助
梅根
答案 0 :(得分:0)
您目前希望导入的评论格式是什么?
似乎你需要扩展这个脚本,例如:打开一个包含你的评论的文件,循环使用它们并在这个循环中使用上面的代码,用实际的评论替换“title”和“detail”等信息。
根据您的评论 * ****
进行编辑我假设你有PHP和Magento编码经验 - 如果没有,也许你应该寻求开发人员的帮助。
<?php
ini_set('memory_limit', '128M');
require_once 'path-to-Mage.php';
Mage::app();
$fp = fopen($fileLocation, 'r');
Mage::app()->setCurrentStore(4); //desired store id
while($line = fgetcsv($fp)) {
$review = Mage::getModel('review/review');
$review->setEntityPkValue($line[0]);//product id
$review->setStatusId($line[1]);
$review->setTitle($line[2]);
$review->setDetail($line[3]);
$review->setEntityId($line[4]);
$review->setStoreId(Mage::app()->getStore()->getId());
$review->setStatusId($line[5]); //approved
$review->setCustomerId($line[6]);//null is for administrator
$review->setNickname($line[7]);
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->save();
$review->aggregate();
}
?>
您显然需要将$ fileLocation替换为CSV文件的目录位置和文件名。 CSV的每一行都将读入数组$ line。列将映射到数组中的元素编号。例如$ line [0]是该行的第一列。您需要更改元素引用,以便数据与CSV列匹配。
这足以让你开始。
答案 1 :(得分:0)
如果要通过纯SQL导入,则可以使用以下查询集:
-- Edit values
SET @PRODUCT_ID = 123;
SET @STORE_ID = 1;
SET @CUSTOMER_ID = NULL;
SET @REVIEW_TITLE = 'Lorem Ipsum';
SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
SET @REVIEW_RATING = 5; -- Between 1 to 5
SET @REVIEW_NICKNAME = 'John Doe';
SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format
-- No need to Edit
SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved
INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;
您可以在此处获取详细步骤:
https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/