2

I am working on a module which creates an xml feed for a price comparison engine. I need in there to pull only the hidden image of the products that exist in the xml. All products have one hidden image.

I have this code:

public function getMediaGallery(array $productIds)
    {
        if (empty($productIds)) {
            return [];
        }

        $productEntityJoinField = $this->getProductEntityLinkField();

        $select = $this->_connection->select()->from(
            ['mgvte' => $this->_resourceModel->getTableName('catalog_product_entity')],
            [
                "mgvte.row_id",
                'mgvte.entity_id'
            ]
        )->joinLeft(
            ['mg' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value_to_entity')],
            '(mg.row_id = mgvte.row_id)',
            [
                'mg.value_id'
            ]
        )->joinLeft(
            ['mgg' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery')],
            '(mg.value_id = mgg.value_id)',
            [
                'mgg.value_id',
                'mgg.attribute_id',
                'filename' => 'mgg.value'
            ]
        )->joinLeft(
            ['mgv' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value')],
            "(mg.value_id = mgv.value_id)",
            [
                'mgv.label',
                'mgv.position',
                'mgv.disabled',
                'mgv.store_id',
            ]
        )->where(
            "mgvte.entity_id IN (?)",
            $productIds
        )
            ->where("mgv.disabled =?", 1);

        $rowMediaGallery = [];
        $stmt = $this->_connection->query($select);
        while ($mediaRow = $stmt->fetch()) {
            $rowMediaGallery[$mediaRow[$productEntityJoinField]][] = [
                '_media_attribute_id' => $mediaRow['attribute_id'],
                '_media_image' => $mediaRow['filename'],
                '_media_label' => $mediaRow['label'],
                '_media_position' => $mediaRow['position'],
                '_media_is_disabled' => $mediaRow['disabled'],
                '_media_store_id' => $mediaRow['store_id'],
            ];
        }

        return $rowMediaGallery;
    }

But it isn't working as expected. I get wrong images in the xml.

With this code I call this query:

SELECT `mgvte`.`row_id`, `mgvte`.`entity_id`, `mg`.`value_id`, `mgg`.`value_id`, `mgg`.`attribute_id`, `mgg`.`value` AS `filename`, `mgv`.`label`, `mgv`.`position`, `mgv`.`disabled`, `mgv`.`store_id` FROM `catalog_product_entity` AS `mgvte`
 LEFT JOIN `catalog_product_entity_media_gallery_value_to_entity` AS `mg` ON (mg.row_id = mgvte.row_id)
 LEFT JOIN `catalog_product_entity_media_gallery` AS `mgg` ON (mg.value_id = mgg.value_id)
 LEFT JOIN `catalog_product_entity_media_gallery_value` AS `mgv` ON (mg.value_id = mgv.value_id) WHERE ((mgvte.entity_id IN (31491)) AND (mgv.disabled =1)) AND (mgvte.created_in <= '1556754420') AND (mgvte.updated_in > '1556754420') LIMIT 1

When I run this in database I get the right result.

But I can't get it in the xml.

Any help please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.