开发者

Magento - How to link the Configurable Product image to the Simple Product image?

开发者 https://www.devze.com 2023-02-12 01:27 出处:网络
This is the situation: I have a configurable product with several simple products. These simple products need to have the same product image as the configurable product.

This is the situation:

I have a configurable product with several simple products. These simple products need to have the same product image as the configurable product. Currently I have to upload the same image to each simple product over and over again.

Is there a way to link the product 开发者_JS百科image of the configurable product to the simple products?

Some of my products have 30 simple products in 1 configurable product and it is overkill/annoying to upload the same image 30 times.

I hope someone can help me with this problem!

Thanks in advance!


Insert this into your DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\view\media.phtml after $_product = $this->getProduct();

$_parentIdArray = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
if(sizeof($_parentIdArray)==1 && Mage::getModel('catalog/product')->load($_parentIdArray[0])->getTypeId() == 'configurable'){
  $_product = Mage::getModel('catalog/product')->load($_parentIdArray[0]);
}

That will use the images belonging to the parent configurable product if the simple product has a single parent of type configurable.

EDIT

To use this in the list view, open DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\list.phtml and insert the same code block in 2 locations:

  • line 45 after <?php foreach ($_productCollection as $_product): ?> (inside the <?php ?> wrappers)
  • line 106 (approx, might be different in your theme) after <?php $i=0; foreach ($_productCollection as $_product): ?>

Both locations are required to deal with the grid view and list view versions of the page.

HTH,
JD


I believe that the right way to do this is to override the image helper (app/core/Mage/Catalog/Helper/Image.php), so that in the init function, you check if you have a simple product, and if you do, replace that with the configurable product. This should effect the change for ALL templates.


A quick workaround might be to export your product list (Admin > System > Import/Export > Profiles), put the image file name in the appropriate column(s) for all your simple products, copy the file(s) to media/import/ directory then import the modified product list. The various associations will be made for you and the image file(s) will be copied to wherever they need to be.


I think the best way is to override catalog image helper (like @stew said). To avoid performance issues we can use raw sql queries to fetch parent's image value:

class Wfs_Catalog_Helper_Image extends Mage_Catalog_Helper_Image
{
    public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
    {
        parent::init($product, $attributeName, $imageFile);
        if (!$product->getId() || $imageFile || $product->isConfigurable()) {
            return $this;
        }

        $productImage = $product->getData($attributeName);
        if (!$productImage || $productImage == 'no_selection') {
            // Get parent product's attribute
            $value = $this->getParentProductAttribute($product->getId(), $attributeName);
            if ($value) {
                $this->_getModel()->setBaseFile($value);
            }
        }
        return $this;
    }

    public function getParentProductAttribute($productId, $attributeName)
    {
        $coreResource = Mage::getSingleton('core/resource');
        $conn = $coreResource->getConnection('core_read');

        $attrId = Mage::getSingleton('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeName)
            ->getId();

        $select = $conn->select()
            ->from(array('rel'  => $coreResource->getTableName('catalog/product_relation')), array())
            ->join(
                array('var' => $coreResource->getTableName('catalog_product_entity_varchar')),
                'var.entity_id = rel.parent_id',
                array('value')
            )
            ->where('rel.child_id = ?', $productId)
            ->where('var.attribute_id = ?', $attrId);

        return $conn->fetchOne($select);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号