Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow choosing image attribute to use on indexation #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Model/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,14 @@ public function canSendProductFeedInAnyScope()
return $this->areAllConfigsEnabledInAnyScopeId(['general/enable_bv', 'feeds/enable_product_feed'], $configScope);
}

/**
* @return string
*/
public function getImageAttributeCode($storeId = null, $scope = ScopeInterface::SCOPE_STORE)
{
return (string) $this->getConfig('indexer/image_attribute_code', $storeId, $scope);
}

/**
* @param string $feedGenerationScope
*
Expand Down
6 changes: 3 additions & 3 deletions Model/Dcc/CatalogData/CatalogProductBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ private function getCustomAttributeData($product, $attributeCode)
*/
private function getProductImageUrl($product, $parentProduct = null)
{
if ($product && $product->getData('small_image')) {
if ($product && $product->getData($this->configProvider->getImageAttributeCode())) {
$imageUrl = $this->mediaConfigFactory->create()->getMediaUrl($product->getSmallImage());
} else if ($parentProduct && $parentProduct->getData('small_image')) {
} else if ($parentProduct && $parentProduct->getData($this->configProvider->getImageAttributeCode())) {
$imageUrl = $this->mediaConfigFactory->create()->getMediaUrl($parentProduct->getSmallImage());
} else {
$imageUrl = $this->mediaConfigFactory->create()->getMediaUrl($product->getSmallImage());
Expand Down Expand Up @@ -347,7 +347,7 @@ private function getBrandAttribute($product)
private function prepareOutput($object)
{
/**
* @var \Magento\Framework\Model\AbstractModel $object
* @var \Magento\Framework\Model\AbstractModel $object
*/
return $this->stringFormatter->stripEmptyValues($object->getData());
}
Expand Down
69 changes: 36 additions & 33 deletions Model/Indexer/Eav.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

/**
* @noinspection DuplicatedCode
* @noinspection DuplicatedCode
*/
declare(strict_types=1);

Expand Down Expand Up @@ -162,7 +162,7 @@ public function canIndex(): bool
public function executeFull()
{
/**
* @var Collection $incompleteIndex
* @var Collection $incompleteIndex
*/

if (!$this->canIndex()) {
Expand Down Expand Up @@ -198,7 +198,7 @@ public function executeFull()
public function execute($ids = [])
{
/**
* @var $idCollection \Bazaarvoice\Connector\Model\ResourceModel\Index\Collection
* @var $idCollection \Bazaarvoice\Connector\Model\ResourceModel\Index\Collection
*/

if (!$this->canIndex()) {
Expand All @@ -217,12 +217,12 @@ public function execute($ids = [])
$this->logger->debug('Found '.count($ids).' products to update.');

/**
* Break ids into pages
* Break ids into pages
*/
$productIdSets = array_chunk($ids, 50);

/**
* Time throttling
* Time throttling
*/
$limit = ($this->configProvider->getCronjobDurationLimit() * 60) - 10;
$stop = time() + $limit;
Expand Down Expand Up @@ -279,7 +279,7 @@ protected function reindexProducts(array $productIds)
case Scope::WEBSITE:
$websites = $this->storeManager->getWebsites();
/**
* @var \Magento\Store\Model\Website $website
* @var \Magento\Store\Model\Website $website
*/
foreach ($websites as $website) {
$defaultStore = $website->getDefaultStore();
Expand All @@ -295,7 +295,7 @@ protected function reindexProducts(array $productIds)
case Scope::STORE_GROUP:
$groups = $this->storeManager->getGroups();
/**
* @var \Magento\Store\Model\Group $group
* @var \Magento\Store\Model\Group $group
*/
foreach ($groups as $group) {
$defaultStore = $group->getDefaultStore();
Expand All @@ -311,7 +311,7 @@ protected function reindexProducts(array $productIds)
case Scope::STORE_VIEW:
$stores = $this->storeManager->getStores();
/**
* @var \Magento\Store\Model\Store $store
* @var \Magento\Store\Model\Store $store
*/
foreach ($stores as $store) {
if ($store->getId()) {
Expand All @@ -337,29 +337,29 @@ protected function flushIndex()
{
$this->canIndex();
/**
* Set indexer to use mview
* Set indexer to use mview
*/
$this->indexer->setScheduled(true);

$writeAdapter = $this->resourceConnection->getConnection('core_write');

/**
* Flush all old data
* Flush all old data
*/
$indexTable = $this->resourceConnection->getTableName('bazaarvoice_index_product');
$writeAdapter->truncateTable($indexTable);
$changelogTable = $this->resourceConnection->getTableName('bazaarvoice_product_cl');
$writeAdapter->truncateTable($changelogTable);

/**
* Setup dummy rows
* Setup dummy rows
*/
$productTable = $this->resourceConnection->getTableName('catalog_product_entity');
$writeAdapter->query("INSERT INTO `$indexTable` (`product_id`, `version_id`) SELECT DISTINCT `entity_id`, '0' FROM `$productTable`;");
$writeAdapter->query("INSERT INTO `$changelogTable` (`entity_id`) SELECT DISTINCT `entity_id` FROM `$productTable`;");

/**
* Reset mview version
* Reset mview version
*/
$mviewTable = $this->resourceConnection->getTableName('mview_state');
$writeAdapter->query("UPDATE `$mviewTable` SET `version_id` = NULL, `status` = 'idle' WHERE `view_id` = 'bazaarvoice_product';");
Expand Down Expand Up @@ -490,7 +490,7 @@ private function populateIndexStoreData($productIds, Store $store)
}

/**
* Version
* Version
*/
$select->joinLeft(
['cl' => $res->getTableName('bazaarvoice_product_cl')],
Expand Down Expand Up @@ -538,7 +538,7 @@ private function populateIndexStoreData($productIds, Store $store)
}

/**
* Use parent URLs/categories if appropriate
* Use parent URLs/categories if appropriate
*/
if ($indexData['visibility'] == Visibility::VISIBILITY_NOT_VISIBLE) {
$this->logger->debug('Not visible');
Expand Down Expand Up @@ -571,7 +571,7 @@ private function populateIndexStoreData($productIds, Store $store)
$standardUrl = $this->getStandardUrl($indexData['product_id']);

/**
* Add Store base to URLs
* Add Store base to URLs
*/
if ($storeId == Store::DEFAULT_STORE_ID) {
$urlStore = $this->storeManager->getStore($indexData['url_store_id']);
Expand Down Expand Up @@ -603,7 +603,7 @@ private function populateIndexStoreData($productIds, Store $store)
}

/**
* @var \Bazaarvoice\Connector\Model\Index $index
* @var \Bazaarvoice\Connector\Model\Index $index
*/
$index = $this->bvIndexFactory->create();
$index->setData($indexData);
Expand All @@ -627,11 +627,11 @@ private function populateIndexLocaleData($productIds, Store $store): void
$read = $res->getConnection('core_read');

/**
* @var Store $localeStore
* @var Store $localeStore
*/
foreach ($locales[$storeId] as $locale => $localeStore) {
/**
* Core Data
* Core Data
*/
$select = $this->getBaseSelect($read, $localeStore, $res);

Expand All @@ -648,7 +648,7 @@ private function populateIndexLocaleData($productIds, Store $store): void
$rows = $select->query();
while (($indexData = $rows->fetch()) !== false) {
/**
* @var Index $productIndex
* @var Index $productIndex
*/

foreach ($this->productIndexes as $productIndex) {
Expand All @@ -660,7 +660,7 @@ private function populateIndexLocaleData($productIds, Store $store): void
}

/**
* Use parent URLs/categories if appropriate
* Use parent URLs/categories if appropriate
*/
if ($indexData['visibility'] == Visibility::VISIBILITY_NOT_VISIBLE) {
$this->logger->debug('Locale not visible');
Expand All @@ -674,7 +674,7 @@ private function populateIndexLocaleData($productIds, Store $store): void
}

/**
* @var Store $localeStore
* @var Store $localeStore
*/
if ($storeId == Store::DEFAULT_STORE_ID && $localeStore->getId() == Store::DEFAULT_STORE_ID) {
$urlStore = $this->storeManager->getStore($indexData['url_store_id']);
Expand Down Expand Up @@ -735,19 +735,20 @@ private function populateIndexLocaleData($productIds, Store $store): void
*/
private function getBaseSelect($read, $store, ResourceConnection $res)
{
$imageAttributeCode = $this->configProvider->getImageAttributeCode();
$storeId = $store->getId();
$select = $read->select()->from(['p' => $res->getTableName('catalog_product_entity')], []);
$select->columns(['product_type' => 'p.type_id', 'product_id' => 'p.entity_id', 'external_id' => 'p.sku']);
$this->addFieldToJoin($select, 'name', $storeId);
$this->addFieldToJoin($select, 'short_description', $storeId);
$this->addFieldToJoin($select, 'small_image', $storeId);
$this->addFieldToJoin($select, $imageAttributeCode, $storeId);
$this->addFieldToJoin($select, 'visibility', $storeId);
$this->addFieldToJoin($select, ProductFeed::INCLUDE_IN_FEED_FLAG, $storeId);
$this->addFieldToJoin($select, 'status', $storeId);

$this->addFieldToSelect($select, 'name', 'name');
$this->addFieldToSelect($select, 'description', 'short_description');
$this->addFieldToSelect($select, 'image_url', 'small_image');
$this->addFieldToSelect($select, 'image_url', $imageAttributeCode);
$this->addFieldToSelect($select, 'visibility', 'visibility');
$this->addFieldToSelect($select, ProductFeed::INCLUDE_IN_FEED_FLAG, ProductFeed::INCLUDE_IN_FEED_FLAG);

Expand Down Expand Up @@ -777,6 +778,8 @@ private function joinParent(Select $select, $storeId, ResourceConnection $res):
''
);

$imageAttributeCode = $this->configProvider->getImageAttributeCode();

$families = $parentFamilies = '';
if ($this->configProvider->isFamiliesEnabled($storeId)) {
$familyAttributes = $this->configProvider->getFamilyAttributesArray($storeId);
Expand All @@ -803,8 +806,8 @@ private function joinParent(Select $select, $storeId, ResourceConnection $res):
$select->columns(['family' => "GROUP_CONCAT(DISTINCT CONCAT_WS(',', {$families}p.sku))"]);
$select->columns(['parent_bvfamily' => "GROUP_CONCAT(DISTINCT CONCAT_WS(',', {$parentFamilies}parent.sku))"]);

$this->addFieldToJoin($select, 'small_image', $storeId, 'parent');
$this->addFieldToSelect($select, 'parent_image', 'small_image', 'parent');
$this->addFieldToJoin($select, $imageAttributeCode, $storeId, 'parent');
$this->addFieldToSelect($select, 'parent_image', $imageAttributeCode, 'parent');
}

/**
Expand All @@ -817,7 +820,7 @@ private function joinParent(Select $select, $storeId, ResourceConnection $res):
private function joinUrlRewrite(Select $select, $storeId, ResourceConnection $res): void
{
/**
* urls
* urls
*/
if ($storeId == Store::DEFAULT_STORE_ID) {
$select
Expand Down Expand Up @@ -875,7 +878,7 @@ private function filterByProducts(Select $select, array $productIds)
private function getStandardUrl($productId): string
{
/**
* Handle missing rewrites
* Handle missing rewrites
*/
return 'catalog/product/view/id/'.$productId;
}
Expand Down Expand Up @@ -903,7 +906,7 @@ private function getStoreUrl(string $storeUrl, string $urlPath, $storeCode = nul
private function _purgeUnversioned(array $productIds)
{
/**
* Database Resources
* Database Resources
*/
$write = $this->resourceConnection->getConnection('core_write');

Expand Down Expand Up @@ -973,10 +976,10 @@ public function executeRow($id): bool
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getImageUrl($store, $indexData)
public function getImageUrl($store, $indexData)
{
/**
* Use parent image if appropriate
* Use parent image if appropriate
*/
if ($indexData['image_url'] == '' || $indexData['image_url'] == 'no_selection') {
if (!empty($indexData['parent_image'])) {
Expand Down Expand Up @@ -1009,10 +1012,10 @@ private function getImageUrl($store, $indexData)
public function getPlaceholderUrl($store)
{
/**
* @var Store $localeStore
* @var Store $localeStore
*/
/**
* @var string $locale
* @var string $locale
*/


Expand Down Expand Up @@ -1075,7 +1078,7 @@ protected function getProductIdFieldName(): string
private function saveProductIndexes()
{
/**
* @var \Bazaarvoice\Connector\Model\Index $bvIndex
* @var \Bazaarvoice\Connector\Model\Index $bvIndex
*/
foreach ($this->productIndexes as $bvIndex) {
try {
Expand Down
Loading