ver. 0.294: Remove all 12 legacy autoload/shop/ classes (~2363 lines)
Complete Domain-Driven Architecture migration: - Phase 1-4: Transport, ProductSet, Coupon, Shop, Search, Basket, ProductCustomField, Category, ProductAttribute, Promotion - Phase 5: Order (~562 lines) + Product (~952 lines) - ~20 Product methods migrated to ProductRepository - Apilo sync migrated to OrderAdminService - Production hotfixes: stale Redis cache (prices 0.00), unqualified Product:: refs in LayoutEngine, object->array template conversion - AttributeRepository::getAttributeValueById() Redis cache added Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,16 @@ class PromotionRepository
|
||||
private $db;
|
||||
private ?string $defaultLangId = null;
|
||||
|
||||
public static $condition_type = [
|
||||
1 => 'Rabat procentowy na produkty z kategorii 1 jeżeli w koszyku jest produkt z kategorii 2',
|
||||
2 => 'Rabat procentowy na produkty z kategorii 1 i 2',
|
||||
3 => 'Najtańszy produkt w koszyku (z wybranych kategorii) za X zł',
|
||||
4 => 'Rabat procentowy na cały koszyk',
|
||||
5 => 'Rabat procentowy na produkty z kategorii 1 lub 2',
|
||||
];
|
||||
|
||||
public static $discount_type = [ 1 => 'Rabat procentowy' ];
|
||||
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
@@ -413,13 +423,75 @@ class PromotionRepository
|
||||
try {
|
||||
$cache = new \Shared\Cache\CacheHandler();
|
||||
if (method_exists($cache, 'delete')) {
|
||||
$cache->delete('\shop\Promotion::get_active_promotions');
|
||||
$cache->delete('PromotionRepository::getActivePromotions');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Cache invalidation should not block save/delete.
|
||||
}
|
||||
}
|
||||
|
||||
public function getActivePromotions()
|
||||
{
|
||||
$cacheHandler = new \Shared\Cache\CacheHandler();
|
||||
$cacheKey = "PromotionRepository::getActivePromotions";
|
||||
|
||||
$objectData = $cacheHandler->get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$results = $this->db->select( 'pp_shop_promotion', 'id', [ 'AND' => [ 'status' => 1, 'OR #date_from' => [ 'date_from' => null, 'date_from[<=]' => date( 'Y-m-d' ) ], 'OR #date_to' => [ 'date_to' => null, 'date_to[>=]' => date( 'Y-m-d' ) ] ], 'ORDER' => [ 'id' => 'DESC' ] ] );
|
||||
|
||||
$cacheHandler->set( $cacheKey, $results );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function findPromotion( $basket )
|
||||
{
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
unset( $basket[$key]['discount_type'] );
|
||||
unset( $basket[$key]['discount_amount'] );
|
||||
unset( $basket[$key]['discount_include_coupon'] );
|
||||
unset( $basket[$key]['include_product_promo'] );
|
||||
}
|
||||
|
||||
$basket_tmp = $basket;
|
||||
|
||||
$results = $this->getActivePromotions();
|
||||
if ( is_array( $results ) and count( $results ) )
|
||||
{
|
||||
foreach ( $results as $row )
|
||||
{
|
||||
$promotion = $this->find( (int)$row );
|
||||
|
||||
if ( $promotion['id'] <= 0 )
|
||||
continue;
|
||||
|
||||
if ( $promotion['condition_type'] == 4 )
|
||||
return $this->applyTypeWholeBasket( $basket_tmp, $promotion );
|
||||
|
||||
if ( $promotion['condition_type'] == 3 )
|
||||
return $this->applyTypeCheapestProduct( $basket_tmp, $promotion );
|
||||
|
||||
if ( $promotion['condition_type'] == 5 )
|
||||
return $this->applyTypeCategoriesOr( $basket_tmp, $promotion );
|
||||
|
||||
if ( $promotion['condition_type'] == 2 )
|
||||
return $this->applyTypeCategoriesAnd( $basket_tmp, $promotion );
|
||||
|
||||
if ( $promotion['condition_type'] == 1 )
|
||||
return $this->applyTypeCategoryCondition( $basket_tmp, $promotion );
|
||||
}
|
||||
}
|
||||
return $basket;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Frontend: basket promotion logic (migrated from front\factory\ShopPromotion)
|
||||
// =========================================================================
|
||||
@@ -433,17 +505,17 @@ class PromotionRepository
|
||||
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
$product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );
|
||||
$product_promotion = (new \Domain\Product\ProductRepository($this->db))->isProductOnPromotion( $val['product-id'] );
|
||||
|
||||
if ( !$product_promotion or $product_promotion and $promotion->include_product_promo )
|
||||
if ( !$product_promotion or $product_promotion and $promotion['include_product_promo'] )
|
||||
{
|
||||
$product_categories = $productRepo->productCategoriesFront( (int) $val['product-id'] );
|
||||
foreach ( $product_categories as $category_tmp )
|
||||
{
|
||||
$basket[$key]['discount_type'] = $promotion->discount_type;
|
||||
$basket[$key]['discount_amount'] = $promotion->amount;
|
||||
$basket[$key]['discount_include_coupon'] = $promotion->include_coupon;
|
||||
$basket[$key]['include_product_promo'] = $promotion->include_product_promo;
|
||||
$basket[$key]['discount_type'] = $promotion['discount_type'];
|
||||
$basket[$key]['discount_amount'] = $promotion['amount'];
|
||||
$basket[$key]['discount_include_coupon'] = $promotion['include_coupon'];
|
||||
$basket[$key]['include_product_promo'] = $promotion['include_product_promo'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -457,15 +529,15 @@ class PromotionRepository
|
||||
{
|
||||
$productRepo = new \Domain\Product\ProductRepository( $this->db );
|
||||
$condition_1 = false;
|
||||
$categories = json_decode( $promotion->categories );
|
||||
$categories = $promotion['categories'];
|
||||
|
||||
if ( is_array( $categories ) and is_array( $categories ) )
|
||||
{
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
$product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );
|
||||
$product_promotion = (new \Domain\Product\ProductRepository($this->db))->isProductOnPromotion( $val['product-id'] );
|
||||
|
||||
if ( !$product_promotion or $product_promotion and $promotion->include_product_promo )
|
||||
if ( !$product_promotion or $product_promotion and $promotion['include_product_promo'] )
|
||||
{
|
||||
$product_categories = $productRepo->productCategoriesFront( (int) $val['product-id'] );
|
||||
foreach ( $product_categories as $category_tmp )
|
||||
@@ -477,12 +549,12 @@ class PromotionRepository
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $condition_1 ) >= $promotion->min_product_count )
|
||||
if ( count( $condition_1 ) >= $promotion['min_product_count'] )
|
||||
{
|
||||
$cheapest_position = false;
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
$price = \shop\Product::get_product_price( $val['product-id'] );
|
||||
$price = (new \Domain\Product\ProductRepository($this->db))->getPrice( $val['product-id'] );
|
||||
if ( !$cheapest_position or $cheapest_position['price'] > $price )
|
||||
{
|
||||
$cheapest_position['price'] = $price;
|
||||
@@ -492,9 +564,9 @@ class PromotionRepository
|
||||
|
||||
$basket[$cheapest_position['key']]['quantity'] = 1;
|
||||
$basket[$cheapest_position['key']]['discount_type'] = 3;
|
||||
$basket[$cheapest_position['key']]['discount_amount'] = $promotion->price_cheapest_product;
|
||||
$basket[$cheapest_position['key']]['discount_include_coupon'] = $promotion->include_coupon;
|
||||
$basket[$cheapest_position['key']]['include_product_promo'] = $promotion->include_product_promo;
|
||||
$basket[$cheapest_position['key']]['discount_amount'] = $promotion['price_cheapest_product'];
|
||||
$basket[$cheapest_position['key']]['discount_include_coupon'] = $promotion['include_coupon'];
|
||||
$basket[$cheapest_position['key']]['include_product_promo'] = $promotion['include_product_promo'];
|
||||
}
|
||||
|
||||
return $basket;
|
||||
@@ -506,24 +578,24 @@ class PromotionRepository
|
||||
public function applyTypeCategoriesOr(array $basket, $promotion): array
|
||||
{
|
||||
$productRepo = new \Domain\Product\ProductRepository( $this->db );
|
||||
$categories = json_decode( $promotion->categories );
|
||||
$condition_categories = json_decode( $promotion->condition_categories );
|
||||
$categories = $promotion['categories'];
|
||||
$condition_categories = $promotion['condition_categories'];
|
||||
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
$product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );
|
||||
$product_promotion = (new \Domain\Product\ProductRepository($this->db))->isProductOnPromotion( $val['product-id'] );
|
||||
|
||||
if ( !$product_promotion or $product_promotion and $promotion->include_product_promo )
|
||||
if ( !$product_promotion or $product_promotion and $promotion['include_product_promo'] )
|
||||
{
|
||||
$product_categories = $productRepo->productCategoriesFront( (int) $val['product-id'] );
|
||||
foreach ( $product_categories as $category_tmp )
|
||||
{
|
||||
if ( in_array( $category_tmp['category_id'], $condition_categories ) or in_array( $category_tmp['category_id'], $categories ) )
|
||||
{
|
||||
$basket[$key]['discount_type'] = $promotion->discount_type;
|
||||
$basket[$key]['discount_amount'] = $promotion->amount;
|
||||
$basket[$key]['discount_include_coupon'] = $promotion->include_coupon;
|
||||
$basket[$key]['include_product_promo'] = $promotion->include_product_promo;
|
||||
$basket[$key]['discount_type'] = $promotion['discount_type'];
|
||||
$basket[$key]['discount_amount'] = $promotion['amount'];
|
||||
$basket[$key]['discount_include_coupon'] = $promotion['include_coupon'];
|
||||
$basket[$key]['include_product_promo'] = $promotion['include_product_promo'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -540,8 +612,8 @@ class PromotionRepository
|
||||
$condition_1 = false;
|
||||
$condition_2 = false;
|
||||
|
||||
$categories = json_decode( $promotion->categories );
|
||||
$condition_categories = json_decode( $promotion->condition_categories );
|
||||
$categories = $promotion['categories'];
|
||||
$condition_categories = $promotion['condition_categories'];
|
||||
|
||||
if ( is_array( $condition_categories ) and is_array( $categories ) )
|
||||
{
|
||||
@@ -577,10 +649,10 @@ class PromotionRepository
|
||||
{
|
||||
if ( in_array( $category_tmp['category_id'], $categories ) or in_array( $category_tmp['category_id'], $condition_categories ) )
|
||||
{
|
||||
$basket[$key]['discount_type'] = $promotion->discount_type;
|
||||
$basket[$key]['discount_amount'] = $promotion->amount;
|
||||
$basket[$key]['discount_include_coupon'] = $promotion->include_coupon;
|
||||
$basket[$key]['include_product_promo'] = $promotion->include_product_promo;
|
||||
$basket[$key]['discount_type'] = $promotion['discount_type'];
|
||||
$basket[$key]['discount_amount'] = $promotion['amount'];
|
||||
$basket[$key]['discount_include_coupon'] = $promotion['include_coupon'];
|
||||
$basket[$key]['include_product_promo'] = $promotion['include_product_promo'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -595,8 +667,8 @@ class PromotionRepository
|
||||
{
|
||||
$productRepo = new \Domain\Product\ProductRepository( $this->db );
|
||||
$condition = false;
|
||||
$categories = json_decode( $promotion->categories );
|
||||
$condition_categories = json_decode( $promotion->condition_categories );
|
||||
$categories = $promotion['categories'];
|
||||
$condition_categories = $promotion['condition_categories'];
|
||||
|
||||
if ( is_array( $condition_categories ) and is_array( $categories ) )
|
||||
{
|
||||
@@ -622,10 +694,10 @@ class PromotionRepository
|
||||
{
|
||||
if ( in_array( $category_tmp['category_id'], $categories ) )
|
||||
{
|
||||
$basket[$key]['discount_type'] = $promotion->discount_type;
|
||||
$basket[$key]['discount_amount'] = $promotion->amount;
|
||||
$basket[$key]['discount_include_coupon'] = $promotion->include_coupon;
|
||||
$basket[$key]['include_product_promo'] = $promotion->include_product_promo;
|
||||
$basket[$key]['discount_type'] = $promotion['discount_type'];
|
||||
$basket[$key]['discount_amount'] = $promotion['amount'];
|
||||
$basket[$key]['discount_include_coupon'] = $promotion['include_coupon'];
|
||||
$basket[$key]['include_product_promo'] = $promotion['include_product_promo'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user