@@ -654,7 +654,10 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
654
654
}
655
655
656
656
if (empty ($ cartIds )) {
657
- $ bestIds = static ::getBestSellingProductIds ($ limit , $ orderBy , $ orderWay );
657
+ $ bestIds = static ::filterAvailableCrossSellingProducts (
658
+ static ::getBestSellingProductIds ($ limit , $ orderBy , $ orderWay ),
659
+ $ context
660
+ );
658
661
$ everPresentProducts = static ::everPresentProducts ($ bestIds , $ context );
659
662
660
663
if (!empty ($ everPresentProducts )) {
@@ -692,7 +695,9 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
692
695
$ ids = [];
693
696
foreach ($ productIds as $ row ) {
694
697
$ 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
+ ) {
696
701
$ ids [] = $ id ;
697
702
}
698
703
if (count ($ ids ) >= $ limit ) {
@@ -714,7 +719,9 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
714
719
$ categoryProducts = static ::getProductsByCategoryId ($ cid , $ limit * 2 , $ orderBy , $ orderWay );
715
720
foreach ($ categoryProducts as $ cproduct ) {
716
721
$ 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
+ ) {
718
725
$ ids [] = $ pid ;
719
726
}
720
727
if (count ($ ids ) >= $ limit ) {
@@ -730,14 +737,19 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
730
737
if (count ($ ids ) >= $ limit ) {
731
738
break ;
732
739
}
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
+ ) {
734
743
$ ids [] = $ bid ;
735
744
}
736
745
}
737
746
}
738
747
739
748
if (empty ($ ids )) {
740
- $ bestIds = static ::getBestSellingProductIds ($ limit , $ orderBy , $ orderWay );
749
+ $ bestIds = static ::filterAvailableCrossSellingProducts (
750
+ static ::getBestSellingProductIds ($ limit , $ orderBy , $ orderWay ),
751
+ $ context
752
+ );
741
753
$ everPresentProducts = static ::everPresentProducts ($ bestIds , $ context );
742
754
743
755
if (!empty ($ everPresentProducts )) {
@@ -756,7 +768,8 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
756
768
continue ;
757
769
}
758
770
759
- $ everPresentProducts = static ::everPresentProducts ($ ids , $ context );
771
+ $ filteredIds = static ::filterAvailableCrossSellingProducts ($ ids , $ context );
772
+ $ everPresentProducts = static ::everPresentProducts ($ filteredIds , $ context );
760
773
761
774
if (!empty ($ everPresentProducts )) {
762
775
$ context ->smarty ->assign ([
@@ -775,6 +788,66 @@ public static function getCrossSellingShortcode(string $txt, Context $context, E
775
788
return $ txt ;
776
789
}
777
790
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
+
778
851
public static function addToCartByUrl (Context $ context , int $ productId , int $ productAttributeId = 0 , int $ quantity = 1 )
779
852
{
780
853
try {
0 commit comments