Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Made a more clear fallback for featured products if api fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
Freek Gruntjes committed Oct 21, 2017
1 parent 73ae09b commit 132af7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/Block/Catalog/Product/ProductList/Featured.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Emico\Tweakwise\Block\Catalog\Product\ProductList;

use Emico\Tweakwise\Exception\ApiException;
use Emico\Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Emico\Tweakwise\Model\Catalog\Product\Recommendation\Context as RecommendationsContext;
use Emico\Tweakwise\Model\Client\Request\Recommendations\FeaturedRequest;
Expand Down Expand Up @@ -103,6 +104,20 @@ private function configureRequest(FeaturedRequest $request)
$request->setTemplate($templateId);
}

/**
* {@inheritdoc}
*/
protected function _beforeToHtml()
{
try {
$this->_getProductCollection();
} catch (ApiException $e) {
return;
}

return parent::_beforeToHtml();
}

/**
* {@inheritdoc}
*/
Expand All @@ -111,6 +126,13 @@ protected function _toHtml()
if (!$this->config->isRecommendationsEnabled(Config::RECOMMENDATION_TYPE_FEATURED)) {
return '';
}

try {
$this->_getProductCollection();
} catch (ApiException $e) {
return '';
}

return parent::_toHtml();
}
}
13 changes: 11 additions & 2 deletions src/Block/TargetRule/Catalog/Product/ProductList/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Emico\Tweakwise\Block\TargetRule\Catalog\Product\ProductList;

use Closure;
use Emico\Tweakwise\Exception\ApiException;
use Emico\Tweakwise\Exception\InvalidArgumentException;
use Emico\Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Emico\Tweakwise\Model\Catalog\Product\Recommendation\Context;
Expand Down Expand Up @@ -81,7 +82,11 @@ public function aroundGetItemCollection(AbstractProductList $subject, Closure $p
return $proceed();
}

return $this->getCollection()->getItems();
try {
return $this->getCollection()->getItems();
} catch (ApiException $e) {
return $proceed();
}
}

/**
Expand All @@ -95,7 +100,11 @@ public function aroundGetPositionLimit(AbstractProductList $subject, Closure $pr
return $proceed();
}

return $this->getCollection()->count();
try {
return $this->getCollection()->count();
} catch (ApiException $e) {
return $proceed();
}
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Model/Client/Response/RecommendationsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
* Class RecommendationsResponse
*
* @package Emico\Tweakwise\Model\Client\Response
*
* @method ItemType[] getItems();
*/
class RecommendationsResponse extends Response
{
/**
* @return ItemType[]
*/
public function getItems()
{
$data = $this->getDataValue('items');
if (!$data) {
return [];
}
return $data;
}

/**
* @param ItemType[]|array[] $items
* @return $this
Expand Down

0 comments on commit 132af7d

Please sign in to comment.