2

Magento の APIを編集して、製品の画像 URLを取得しようとしています。以前はURLを取得していましたが、間違ったURLを取得しています。 items() function$product-> getImageUrl()

私が取得しているURLは、画像のない 製品に配置するデフォルトの画像です(画像の URL のような画像は近日公開)。

XML-RPCを使用してAndroid クライアントから関数を呼び出しています。 製品のその他の詳細は正しく取得していますが、取得している製品のURLが間違っています。そして、私が入手しているさまざまな製品の すべてのURLは同じです。

参考までに、応答で取得している URL は次のようなものです。

http://192.168.1.237/machinetest/media/catalog/product/cache/0/image/265x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg

私が編集している機能は次のとおりです。

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
             'url_path'  =>  $product-> getImageUrl() // Added the Method here 
        );
    }
    return $result;
}
4

3 に答える 3

4

この方法を試してください..あなたは一番上に解決策を得るだろうと書いてください

このコードを使用して画像を取得できます。それを実行すると、画像が取得されます

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }

                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }

    $result = array();

    foreach ($collection as $product) {
//            $result[] = $product->getData();

        $_product = Mage::getModel('catalog/product')->load($product->getId());
        $_image=$_product->getImageUrl();

        $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),

            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
            'image_url_path' =>  $_image
        );
    }

    return $result;
}

ご不明な点がございましたら、お役に立てれば幸いです。

于 2012-09-03T05:15:24.480 に答える
4

私は最近画像を扱っていましたが、それについてかなりよく理解していると思います。

ヨスアの言ったことが「本当の」正解だとは思いません。彼の答えがあなたの問題を解決できるのは良いことですが、誤解を招く情報を見るのは我慢できませんでした.

彼の最初の選択肢は「正解」です。

コードを分解してみましょう。

Mage_Catalog_Model_Product_Media_Config

public function getMediaUrl($file)
{
    $file = $this->_prepareFileForUrl($file);

    if(substr($file, 0, 1) == '/') {
        return $this->getBaseMediaUrl() . $file;
    }

    return $this->getBaseMediaUrl() . '/' . $file;
}

public function getBaseMediaUrl()
{
    return Mage::getBaseUrl('media') . 'catalog/product';
}

protected function _prepareFileForUrl($file)
{
    return str_replace(DS, '/', $file);
}

ご覧のとおり、単に追加するだけですmedia/ + catalog/product + $file

$file はデータベースから取得され、値は次のようになります/e/x/example.jpeg

アップロードした製品画像は、これらのフォルダー内に保存されます。

さて、なぜ$product-> getImageUrl()間違ったURLを提供するのかという問題については、まだ不明です。

Josua が 2 番目のオプションとして提案するコード:

$this->helper('catalog/image')
    ->init($product, $type)
    ->resize(163, 100);

は と「ほぼ」同じですが$product->getImageUrl()、違いがあるだけですresize

Mage_Catalog_Model_Product

public function getImageUrl()
{
    return (string)$this->_getImageHelper()->init($this, 'image')->resize(265);
}

したがって、彼の 2 番目のオプションでは、古いコードで同じ結果が得られます。なぜ彼が 2 番目のオプションを提案したのかはわかりません。彼はそれらの機能の背後にあるものを決してチェックしていないと思います (間違った情報につながる可能性があるため、良い考えではありません)。

を呼び出すと$product->getImageUrl()、画像が存在する場合はキャッシュから画像をロードしようとします。画像が存在しない場合は、データベースから画像をロードして (パスを取得し、メディア フォルダーから正しい画像を探します)、キャッシュを作成します。画像が見つからない場合やエラーが発生した場合は、プレースホルダー画像を取得します。

私の提案は、スローされた例外があるかどうかを確認することです。古いコードを使用する必要があります$product->getImageUrl()。あなたのapp/code/core/Mage/Catalog/Helper/Image.php

次に、次の場所に移動します。

Mage_Catalog_Helper_Image

public function __toString()
{
    try {
        if( $this->getImageFile() ) {
            $this->_getModel()->setBaseFile( $this->getImageFile() );
        } else {
            $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
        }

        if( $this->_getModel()->isCached() ) {
            return $this->_getModel()->getUrl();
        } else {
            if( $this->_scheduleRotate ) {
                $this->_getModel()->rotate( $this->getAngle() );
            }

            if ($this->_scheduleResize) {
                $this->_getModel()->resize();
            }

            if( $this->getWatermark() ) {
                $this->_getModel()->setWatermark($this->getWatermark());
            }

            $url = $this->_getModel()->saveFile()->getUrl();
        }
    } catch( Exception $e ) {
        //put log to show error message
        Mage::log($e->getMessage());
        $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
    }
    return $url;
}

例外Mage::log($e->getMessage());がスローされた場合にログに記録します。例外がスローされたため、プレースホルダー イメージが呼び出された可能性があります。

実際に画像を直接取得することで問題を解決したので、画像/その他に問題がないことを確認するのは私からの提案ですmedia/catalog/product/...

Josua のコードの別の修正:

$full_product = Mage::getModel('catalog/product')->load($product_id); に注意してください。

foreach($collection as $product) 内で製品オブジェクトがロードされるため、これは絶対に不要です。そのため、製品の別のロードは不要です ($product_id も未定義です)。

更新、コードを修正するだけです:

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect(array('name','image'));
        //->addAttributeToSelect('name'); add another select, either image / small_image / thumbnail, modify it as you need

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
             'url_path'  =>  $product-> getImageUrl() // Added the Method here 
        );
    }
    return $result;
}
于 2012-09-01T16:48:43.030 に答える
0

あなたのコードは素晴らしかったです!

はい、次のコマンドで画像のURLを取得できます:

'url_path'  =>  Mage::getModel('catalog/product_media_config')
                ->getMediaUrl($product->getImage());//getSmallImage(), getThumbnail()

または呼び出して別のオプション:

$type = 'small_image';
'url_path'  => $this->helper('catalog/image')
                   ->init($product, $type)
                   ->resize(163, 100);

'image'small_image'または'thumbnail'で変更できます

デフォルト:

  • ベース画像:265x265ピクセル

  • 小さい画像:135x135ピクセル

  • サムネイル画像:75x75ピクセル


より簡単なオプション(詳細):

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $full_product = Mage::getModel('catalog/product')->load($product_id);

           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids'=> $product->getCategoryIds(),
            'url_path'  =>  $full_product->getImageUrl(),
            // 'product_path' => $full_product->getProductUrl() 
            // you can call $full_product->getData();
        );
    }
    return $result;
}
于 2012-09-01T12:45:48.813 に答える