サムネイル画像をプログラムでギャラリーから別の画像に設定/変更する
-
16-10-2019 - |
質問
私は製品の膨大なカタログを持っていますが、すべて製品ごとに約4つの画像があり、同じ命名構造が「xxxxxx_fr.jpg」「xxxxxxx_bk.jpg "" xxxxxx_fl.jpg "のようなものです。 FLは、フロント、バック、フルレングスなどのスタンドです。
現在、ベース、小型、サムネイルはすべてFR画像に割り当てられていますが、私がやろうとしているのは、各製品のサムネイル画像だけがプログラムでギャラリーからFL画像に変更することです。カスタム製品コレクションを構築し、製品を通過してから次のロードでこれに近づいています。
foreach ($_productCollection as $product){
$workingProduct = Mage::getModel('catalog/product')->load($productID);
$gallery = $workingProduct->getMediaGalleryImages();
$cameraAngle = str_replace(".jpg","",explode("_",$image->getUrl())); //This results in string that is just FR, BK, FL etc.
if (strtolower($cameraAngle[1]) == 'fl') {
//I have tried both of the below methods
$workingProduct->setThumbnail($image->getUrl());
$product->addImageToMediaGallery($image->getUrl(),array('thumbnail'),false,false);
$workingProduct->save();
}
}
最初の方法では何もしません。2番目の方法では、新しい画像を画像ギャラリーに追加します(これは望ましくありません)が、サムネイル画像も変更されません。ギャラリーに新しい画像をインポートまたは作成したくないので、方法1にはより多くの可能性があると考えていますが、私が何を間違えているのかわかりません。
解決 2
私の解決策は、他の誰にとってもこれでした...
foreach ($gallery as $image){
$cameraAngle = str_replace(".jpg","",explode("_",$image->getUrl()));
if (strtolower($cameraAngle[1]) == 'fl') {
Mage::getSingleton('catalog/product_action')->updateAttributes(array($workingProduct->getId()), array('thumbnail'=>$image['file']), 0);
}
他のヒント
私があなたを正しく理解しているなら、あなたはただサムネイルのためにFRをFLに変更したいだけですか?
これを行うだけです:
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('thumbnail);
foreach($collection as $product) {
$product->setThumbnail(str_replace('_FR', '_FL', $product->getThumbnail());
}
$collection->save();
所属していません magento.stackexchange