Skip to content

Commit b5ad489

Browse files
authored
Merge pull request #1061 from algolia/admin-reindex-sku
Reindex SKU(s) Feature
2 parents 2250a7a + 3fa24f7 commit b5ad489

File tree

16 files changed

+425
-9
lines changed

16 files changed

+425
-9
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Block_Adminhtml_ReindexSku_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
4+
{
5+
/**
6+
* Internal constructor.
7+
*/
8+
protected function _construct()
9+
{
10+
parent::_construct();
11+
12+
$this->_objectId = 'sku';
13+
$this->_blockGroup = 'algoliasearch';
14+
$this->_controller = 'adminhtml_reindexsku';
15+
}
16+
17+
/**
18+
* Get header text.
19+
*
20+
* @return string
21+
*/
22+
public function getHeaderText()
23+
{
24+
return Mage::helper('algoliasearch')->__('Algolia Search - Reindex SKU(s)');
25+
}
26+
27+
/**
28+
* Set custom Algolia icon class.
29+
*
30+
* @return string
31+
*/
32+
public function getHeaderCssClass()
33+
{
34+
return 'icon-head algoliasearch-head-icon';
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Block_Adminhtml_ReindexSku_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
4+
{
5+
/**
6+
* @return Algolia_AlgoliaSearch_Block_Adminhtml_ReindexSku_Edit_Form
7+
*/
8+
protected function _prepareForm()
9+
{
10+
$form = new Varien_Data_Form(array(
11+
'id' => 'edit_form',
12+
'action' => $this->getUrl('*/*/reindexPost'),
13+
'method' => 'post',
14+
));
15+
16+
$fieldset = $form->addFieldset('base_fieldset', array());
17+
18+
$html = '</br></br>';
19+
$html .= '<p>'.__('Enter here the SKU(s) you want to reindex separated by commas or carriage returns.').'</p>';
20+
$html .= '<p>'.__('You will be notified if there is any reason why your product can\'t be reindexed.').'</p>';
21+
$html .= '<p>'.__('It can be :').'</p>';
22+
$html .= '<ul style="list-style: disc; padding-left: 25px;">';
23+
$html .= '<li>'.__('Product is disabled.').'</li>';
24+
$html .= '<li>'.__('Product is deleted.').'</li>';
25+
$html .= '<li>'.__('Product is out of stock.').'</li>';
26+
$html .= '<li>'.__('Product is not visible.').'</li>';
27+
$html .= '<li>'.__('Product is not related to the store.').'</li>';
28+
$html .= '</ul>';
29+
$html .= '<p>'.__('You can reindex up to 10 SKUs at once.').'</p>';
30+
31+
$fieldset->addField('skus', 'textarea', array(
32+
'name' => 'skus',
33+
'label' => Mage::helper('algoliasearch')->__('Product SKU(s)'),
34+
'title' => Mage::helper('algoliasearch')->__('Product SKU(s)'),
35+
'required' => true,
36+
'style' => 'width:100%',
37+
'after_element_html' => $html,
38+
));
39+
40+
$form->setUseContainer(true);
41+
$this->setForm($form);
42+
43+
return parent::_prepareForm();
44+
}
45+
}

app/code/community/Algolia/Algoliasearch/Helper/Data.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,9 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted
490490
continue;
491491
}
492492

493-
if ($product->isDeleted() === true
494-
|| $product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED
495-
|| $this->product_helper->shouldIndexProductByItsVisibility($product, $storeId) === false
496-
|| ($product->getStockItem()->is_in_stock == 0 && !$this->config->getShowOutOfStock($storeId))
497-
) {
493+
try {
494+
$this->product_helper->canProductBeReindexed($product, $storeId);
495+
} catch (Algolia_Algoliasearch_Model_Exception_ProductReindexException $e) {
498496
$productsToRemove[$productId] = $productId;
499497
continue;
500498
}
@@ -511,7 +509,6 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted
511509
'toRemove' => array_unique($productsToRemove),
512510
);
513511
}
514-
515512
public function rebuildStoreProductIndexPage($storeId, $collectionDefault, $page, $pageSize, $emulationInfo = null, $productIds = null, $useTmpIndex = false)
516513
{
517514
if ($this->config->isEnabledBackend($storeId) === false) {

app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
class Algolia_Algoliasearch_Helper_Entity_Producthelper extends Algolia_Algoliasearch_Helper_Entity_Helper
44
{
5+
private $compositeTypes;
6+
57
protected static $_productAttributes;
68
protected static $_currencies;
79

@@ -1041,6 +1043,46 @@ public function getObject(Mage_Catalog_Model_Product $product)
10411043
return $customData;
10421044
}
10431045

1046+
/**
1047+
* Returns all parent product IDs, e.g. when simple product is part of configurable or bundle
1048+
*
1049+
* @param array $productIds
1050+
* @return array
1051+
*/
1052+
public function getParentProductIds(array $productIds)
1053+
{
1054+
$parentIds = array();
1055+
foreach ($this->getCompositeTypes() as $typeInstance) {
1056+
$parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productIds));
1057+
}
1058+
1059+
return $parentIds;
1060+
}
1061+
1062+
/**
1063+
* Returns composite product type instances
1064+
*
1065+
* @return Mage_Catalog_Model_Product_Type[]
1066+
*
1067+
* @see Mage_Catalog_Model_Resource_Product_Flat_Indexer::getProductTypeInstances()
1068+
*/
1069+
private function getCompositeTypes()
1070+
{
1071+
if ($this->compositeTypes === null) {
1072+
/** @var Mage_Catalog_Model_Product $productEmulator */
1073+
$productEmulator = Mage::getModel('catalog/product');
1074+
1075+
/** @var Mage_Catalog_Model_Product_Type $productType */
1076+
$productType = Mage::getModel('catalog/product_type');
1077+
foreach ($productType->getCompositeTypes() as $typeId) {
1078+
$productEmulator->setTypeId($typeId);
1079+
$this->compositeTypes[$typeId] = $productType->factory($productEmulator);
1080+
}
1081+
}
1082+
1083+
return $this->compositeTypes;
1084+
}
1085+
10441086
public function getAllProductIds($storeId)
10451087
{
10461088
$products = Mage::getModel('catalog/product')->getCollection();
@@ -1084,6 +1126,45 @@ private function getVisibilityAttributeValues($storeId)
10841126
return $catalog_productVisibility->{$visibilityMethod}();
10851127
}
10861128

1129+
/**
1130+
* Check if product can be index on Algolia
1131+
*
1132+
* @param Mage_Catalog_Model_Product $product
1133+
* @param int $storeId
1134+
*
1135+
* @return bool
1136+
*
1137+
*/
1138+
public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $storeId)
1139+
{
1140+
if ($product->isDeleted() === true) {
1141+
throw (new Algolia_Algoliasearch_Model_Exception_ProductDeletedException())
1142+
->withProduct($product)
1143+
->withStoreId($storeId);
1144+
}
1145+
1146+
if ($product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
1147+
throw (new Algolia_Algoliasearch_Model_Exception_ProductDisabledException())
1148+
->withProduct($product)
1149+
->withStoreId($storeId);
1150+
}
1151+
1152+
if ($this->shouldIndexProductByItsVisibility($product, $storeId) === false) {
1153+
throw (new Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException())
1154+
->withProduct($product)
1155+
->withStoreId($storeId);
1156+
}
1157+
1158+
if (!$this->config->getShowOutOfStock($storeId)
1159+
&& !$product->getStockItem()->getIsInStock()) {
1160+
throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException())
1161+
->withProduct($product)
1162+
->withStoreId($storeId);
1163+
}
1164+
1165+
return true;
1166+
}
1167+
10871168
private function explodeSynomyms($synonyms)
10881169
{
10891170
return array_map('trim', explode(',', $synonyms));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductDeletedException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductDisabledException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException
4+
{
5+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductReindexException extends RuntimeException
4+
{
5+
/** @var Mage_Catalog_Model_Product */
6+
protected $product;
7+
8+
/** @var int */
9+
protected $storeId;
10+
11+
/**
12+
* Add related product.
13+
*
14+
* @param Mage_Catalog_Model_Product $product
15+
*
16+
* @return $this
17+
*/
18+
public function withProduct($product)
19+
{
20+
$this->product = $product;
21+
22+
return $this;
23+
}
24+
25+
/**
26+
* Add related store ID.
27+
*
28+
* @param int $storeId
29+
*
30+
* @return $this
31+
*/
32+
public function withStoreId($storeId)
33+
{
34+
$this->storeId = $storeId;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* Get related product.
41+
*
42+
* @return Mage_Catalog_Model_Product
43+
*/
44+
public function getProduct()
45+
{
46+
return $this->product;
47+
}
48+
49+
/**
50+
* Get related store ID.
51+
*
52+
* @return int
53+
*/
54+
public function getStoreId()
55+
{
56+
return $this->storeId;
57+
}
58+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException
4+
{
5+
}

0 commit comments

Comments
 (0)