【问题标题】:Get all image urls when creating csv in magento在magento中创建csv时获取所有图片url
【发布时间】:2013-09-05 07:35:28
【问题描述】:

我是 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);

?>

【问题讨论】:

    标签: magento csv export


    【解决方案1】:

    试试这个

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

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

    这样就可以了

    【讨论】:

    • 感谢您的回答。我正在以不同的方式尝试它,但它不起作用。例如,如果我尝试使用这个 foreach ($product->getGalleryImages() as $_image) { echo $this- >getMediaConfig()->getMediaUrl($this->getData('image')); } $_image 是空的。
    • 抱歉回复了,帮手工作了吗 $product->getGalleryUrl($_image);在 foreach 循环中试试这个
    • 感谢您的回答。我正在使用这些建议,但我无法访问图片网址...:(
    • 很遗憾我无法解决,谢谢您的帮助。
    • 这个答案最初对我有用,直到我意识到它使用了 admin(?) 商店?更改为 this question 的答案,以便能够指定要使用的商店 ID。
    猜你喜欢
    • 1970-01-01
    • 2012-01-07
    • 2019-01-02
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    相关资源
    最近更新 更多