Skip to content

Commit 4a45c38

Browse files
committed
Filter out unavailable products in cross-selling
1 parent 4cdbb31 commit 4a45c38

File tree

1 file changed

+79
-6
lines changed

1 file changed

+79
-6
lines changed

models/EverblockTools.php

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,10 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
654654
}
655655

656656
if (empty($cartIds)) {
657-
$bestIds = static::getBestSellingProductIds($limit, $orderBy, $orderWay);
657+
$bestIds = static::filterAvailableCrossSellingProducts(
658+
static::getBestSellingProductIds($limit, $orderBy, $orderWay),
659+
$context
660+
);
658661
$everPresentProducts = static::everPresentProducts($bestIds, $context);
659662

660663
if (!empty($everPresentProducts)) {
@@ -692,7 +695,9 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
692695
$ids = [];
693696
foreach ($productIds as $row) {
694697
$id = (int) $row['id_product'];
695-
if (!in_array($id, $cartIds) && !in_array($id, $ids)) {
698+
if (!in_array($id, $cartIds) && !in_array($id, $ids)
699+
&& static::isProductAvailableForCrossSelling($id, $context)
700+
) {
696701
$ids[] = $id;
697702
}
698703
if (count($ids) >= $limit) {
@@ -714,7 +719,9 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
714719
$categoryProducts = static::getProductsByCategoryId($cid, $limit * 2, $orderBy, $orderWay);
715720
foreach ($categoryProducts as $cproduct) {
716721
$pid = (int) $cproduct['id_product'];
717-
if (!in_array($pid, $cartIds) && !in_array($pid, $ids)) {
722+
if (!in_array($pid, $cartIds) && !in_array($pid, $ids)
723+
&& static::isProductAvailableForCrossSelling($pid, $context)
724+
) {
718725
$ids[] = $pid;
719726
}
720727
if (count($ids) >= $limit) {
@@ -730,14 +737,19 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
730737
if (count($ids) >= $limit) {
731738
break;
732739
}
733-
if (!in_array($bid, $cartIds) && !in_array($bid, $ids)) {
740+
if (!in_array($bid, $cartIds) && !in_array($bid, $ids)
741+
&& static::isProductAvailableForCrossSelling($bid, $context)
742+
) {
734743
$ids[] = $bid;
735744
}
736745
}
737746
}
738747

739748
if (empty($ids)) {
740-
$bestIds = static::getBestSellingProductIds($limit, $orderBy, $orderWay);
749+
$bestIds = static::filterAvailableCrossSellingProducts(
750+
static::getBestSellingProductIds($limit, $orderBy, $orderWay),
751+
$context
752+
);
741753
$everPresentProducts = static::everPresentProducts($bestIds, $context);
742754

743755
if (!empty($everPresentProducts)) {
@@ -756,7 +768,8 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
756768
continue;
757769
}
758770

759-
$everPresentProducts = static::everPresentProducts($ids, $context);
771+
$filteredIds = static::filterAvailableCrossSellingProducts($ids, $context);
772+
$everPresentProducts = static::everPresentProducts($filteredIds, $context);
760773

761774
if (!empty($everPresentProducts)) {
762775
$context->smarty->assign([
@@ -775,6 +788,66 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
775788
return $txt;
776789
}
777790

791+
protected static function filterAvailableCrossSellingProducts(array $productIds, Context $context): array
792+
{
793+
$filtered = [];
794+
795+
foreach ($productIds as $productId) {
796+
$productId = (int) $productId;
797+
798+
if ($productId <= 0) {
799+
continue;
800+
}
801+
802+
if (static::isProductAvailableForCrossSelling($productId, $context)) {
803+
$filtered[] = $productId;
804+
}
805+
}
806+
807+
return $filtered;
808+
}
809+
810+
protected static function isProductAvailableForCrossSelling(int $productId, Context $context): bool
811+
{
812+
static $availabilityCache = [];
813+
814+
if (isset($availabilityCache[$productId])) {
815+
return $availabilityCache[$productId];
816+
}
817+
818+
if (!Configuration::get('PS_STOCK_MANAGEMENT')) {
819+
$availabilityCache[$productId] = true;
820+
821+
return true;
822+
}
823+
824+
$product = new Product($productId);
825+
826+
if (!Validate::isLoadedObject($product)) {
827+
$availabilityCache[$productId] = false;
828+
829+
return false;
830+
}
831+
832+
$quantity = StockAvailable::getQuantityAvailableByProduct(
833+
$productId,
834+
0,
835+
(int) $context->shop->id
836+
);
837+
838+
if ($quantity > 0) {
839+
$availabilityCache[$productId] = true;
840+
841+
return true;
842+
}
843+
844+
$isAllowedWhenOutOfStock = Product::isAvailableWhenOutOfStock((int) $product->out_of_stock);
845+
846+
$availabilityCache[$productId] = $isAllowedWhenOutOfStock;
847+
848+
return $isAllowedWhenOutOfStock;
849+
}
850+
778851
public static function addToCartByUrl(Context $context, int $productId, int $productAttributeId = 0, int $quantity = 1)
779852
{
780853
try {

0 commit comments

Comments
 (0)