1

我是 magento 和 php 对象的新手,但我必须在根目录中创建一个简单的 php 文件才能将 SKU、产品名称和前 4 个基本图像 url 导出到 csv。不幸的是,img url 丢失了,我找不到有效的示例(我在“SOMETHING MISSING HERE”文本之后遇到了问题)。感谢您的帮助。

<?php 

header('Content-Type: text/html; charset=utf-8');
require 'app/Mage.php';
Mage::app("default");

$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSelect('*') // all attributes
              ->addAttributeToFilter('exportornot', 'yes') // custom attribute filter
              ->addAttributeToFilter('status', 1);//enabled            
$fp = fopen('feed.csv', 'w');//open csv file

//-----------------------------MAKE CSV HEADER------------------------------
 $csvheader = array(
    'title',
    'sku',
    'photo_url_1',
    'photo_url_2',
    'photo_url_3',
    'photo_url_4',
    'category',
    'product_url',
   'description'
 );
fputcsv($fp, $csvheader, $delimiter = ",");//write to the csv  header   

//-----------------------------MAKE CATEGORY ------------------------------
foreach ($collection as $product) {
$fcmCathegoryPath="";
foreach ($product->getCategoryIds() as $category_id) {
    $category = Mage::getModel('catalog/category')->load($category_id);
    $fcmCathegoryPath.=$category->getName();
}

//-----------------------------MAKE IMAGE VARIABLES------------------------------
$_gallery = Mage::getModel('catalog/product')->load(
    $product->getId())->getMediaGalleryImages();
$imgCount = Mage::getModel('catalog/product')->load(
    $product->getId())->getMediaGalleryImages()->count();

if($imgCount >1){
    foreach ($_gallery as $_image ){
    /*
      SOMETHING MISSING HERE
      for example:
      put the first image url to the first variable
      $photo_url_1 = $_image->getFile()
     and so on
    */
 }

//-----------------------------MAKE CSV CONTENT-----------------------------
fcmFeedContent=array( //put the things to the array
    $product->getName(),//title
    $product->getSku(),//seller_product_id 
    $photo_url_1,
    $photo_url_2,
    $photo_url_3,
    $photo_url_4,
    $fcmCathegoryPath,
    'http://mysite.com/'.$product->getUrlPath(),
    $product->getDescription()
);

 fputcsv($fp, fcmFeedContent, $delimiter = ",");//write to the csv    
} 

fclose($fp);

?>
4

1 回答 1

0

尝试这个

 if (count($product->getGalleryImages()) > 0) {
     foreach ($product->getGalleryImages() as $_image) {
         echo $this->getMediaConfig()->getMediaUrl($this->getData('image'));
     }
 } 

或者

Mage::helper('catalog/image')->init($_product, 'image')

这会起作用

于 2013-09-05T07:39:47.243 回答