Last active
October 12, 2021 05:47
-
-
Save SirKwest/d336e76c33391ad5fc3018e406003ff5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/addons/product_variations/src/Product/Repository.php b/app/addons/product_variations/src/Product/Repository.php | |
index cb4537e381..6cc0d3fc2c 100644 | |
--- a/app/addons/product_variations/src/Product/Repository.php | |
+++ b/app/addons/product_variations/src/Product/Repository.php | |
@@ -1055,6 +1055,16 @@ class Repository | |
->setOrderBy(['popularity.total DESC', 'product.product_id ASC']) | |
->setLimit(1); | |
+ /** | |
+ * Allows you to change query for selecting active and more popular product ids. | |
+ * | |
+ * @param Repository $this Current state of object. | |
+ * @param array<int> $product_ids Product ids. | |
+ * @param bool $on_stock If true finds only in stock products. | |
+ * @param \Tygh\Addons\ProductVariations\Tools\Query $query Request query. | |
+ */ | |
+ fn_set_hook('product_variations_product_repository_find_active_and_more_popular_product_id', $this, $product_ids, $on_stock, $query); | |
+ | |
return (int) $query->scalar(); | |
} | |
@@ -1068,7 +1078,7 @@ class Repository | |
* | |
* @return \Tygh\Addons\ProductVariations\Tools\Query | |
*/ | |
- protected function createQuery($table_id, array $conditions = [], array $fields = [], $table_alias = null) | |
+ public function createQuery($table_id, array $conditions = [], array $fields = [], $table_alias = null) | |
{ | |
return $this->query_factory->createQuery($table_id, $conditions, $fields, $table_alias); | |
} | |
diff --git a/app/addons/warehouses/func.php b/app/addons/warehouses/func.php | |
index b5bdcba394..8f4991e1f2 100644 | |
--- a/app/addons/warehouses/func.php | |
+++ b/app/addons/warehouses/func.php | |
@@ -16,6 +16,8 @@ defined('BOOTSTRAP') or die('Access denied'); | |
use Tygh\Addons\CommerceML\Dto\IdDto; | |
use Tygh\Addons\CommerceML\Dto\PropertyDto; | |
+use Tygh\Addons\ProductVariations\Product\Repository; | |
+use Tygh\Addons\ProductVariations\Tools\Query; | |
use Tygh\Addons\Warehouses\Manager; | |
use Tygh\Addons\Warehouses\ProductWarehouse; | |
use Tygh\Addons\Warehouses\ServiceProvider; | |
@@ -1369,3 +1371,78 @@ function fn_warehouses_get_filters_products_count_pre(array &$params, array &$ca | |
$location = Tygh::$app['location']; | |
$params['customer_destination_id'] = $location->getDestinationId(); | |
} | |
+ | |
+/** | |
+ * The `load_products_extra_data_post` hook handler. | |
+ * | |
+ * Action performed: | |
+ * - Adds correct information about product amount if it's stock was split by warehouses. | |
+ * | |
+ * @param array<array<string|int>> $products List of products | |
+ * @param array<int> $product_ids List of product identifiers | |
+ * @param array<string, int> $params Parameters passed to fn_get_products() | |
+ * @param string $lang_code Language code passed to fn_get_products() | |
+ * | |
+ * @return void | |
+ */ | |
+function fn_warehouses_load_products_extra_data_post(array &$products, array $product_ids, array $params, $lang_code) | |
+{ | |
+ if (empty($products) || empty($product_ids)) { | |
+ return; | |
+ } | |
+ $condition = db_quote('AND product_id IN (?n)', $product_ids); | |
+ if (isset($params['storefront_id'])) { | |
+ $condition .= db_quote(' AND storefront_id = ?i', $params['storefront_id']); | |
+ } | |
+ $amounts = db_get_hash_single_array('SELECT product_id, amount FROM ?:warehouses_sum_products_amount WHERE 1 ?p', ['product_id', 'amount'], $condition); | |
+ foreach ($products as $product_id => &$product_data) { | |
+ if (!isset($amounts[$product_id])) { | |
+ continue; | |
+ } | |
+ $product_data['amount'] = $amounts[$product_id]; | |
+ } | |
+ unset($product_data); | |
+} | |
+ | |
+/** | |
+ * The `product_variations_product_repository_find_active_and_more_popular_product_id` hook handler. | |
+ * | |
+ * Action performed: | |
+ * - Allows correctly determine in stock products if they stock was split by warehouses. | |
+ * | |
+ * @param Repository $instance Current state of object. | |
+ * @param array<int> $product_ids Product ids. | |
+ * @param bool $on_stock If true finds only in stock products. | |
+ * @param Query $query Request query. | |
+ * | |
+ * @return void | |
+ */ | |
+function fn_warehouses_product_variations_product_repository_find_active_and_more_popular_product_id( | |
+ Repository $instance, | |
+ array $product_ids, | |
+ $on_stock, | |
+ Query &$query | |
+) { | |
+ if (!$on_stock) { | |
+ return; | |
+ } | |
+ $query = $instance->createQuery([Repository::TABLE_PRODUCTS => 'product'], [ | |
+ 'product_id' => $product_ids, | |
+ 'status' => ObjectStatuses::ACTIVE, | |
+ ]); | |
+ | |
+ $condition = db_quote( | |
+ '(CASE product.is_stock_split_by_warehouses WHEN ?s' | |
+ . ' THEN war_sum_amount.amount' | |
+ . ' ELSE product.amount END)', | |
+ YesNo::YES | |
+ ); | |
+ $query->addCondition($condition); | |
+ | |
+ $query | |
+ ->addField('product.product_id') | |
+ ->addLeftJoin('popularity', Repository::TABLE_PRODUCT_POPULARITY, ['product_id' => 'product_id']) | |
+ ->addLeftJoin('war_sum_amount', 'warehouses_sum_products_amount', ['war_sum_amount.product_id' => 'product.product_id']) | |
+ ->setOrderBy(['popularity.total DESC', 'product.product_id ASC']) | |
+ ->setLimit(1); | |
+} | |
diff --git a/app/addons/warehouses/init.php b/app/addons/warehouses/init.php | |
index 345d424a93..fa635d4ac9 100644 | |
--- a/app/addons/warehouses/init.php | |
+++ b/app/addons/warehouses/init.php | |
@@ -43,5 +43,7 @@ fn_register_hooks( | |
['warehouses_manager_remove_warehouse', '', 'commerceml'], | |
'ult_update_share_object', | |
'ult_unshare_object', | |
- 'get_filters_products_count_pre' | |
+ 'get_filters_products_count_pre', | |
+ 'load_products_extra_data_post', | |
+ 'product_variations_product_repository_find_active_and_more_popular_product_id' | |
); | |
diff --git a/design/backend/templates/addons/warehouses/addons/product_variations/hooks/product_variations/list_quantity.override.tpl b/design/backend/templates/addons/warehouses/addons/product_variations/hooks/product_variations/list_quantity.override.tpl | |
index 51ac683a11..0add21e18c 100644 | |
--- a/design/backend/templates/addons/warehouses/addons/product_variations/hooks/product_variations/list_quantity.override.tpl | |
+++ b/design/backend/templates/addons/warehouses/addons/product_variations/hooks/product_variations/list_quantity.override.tpl | |
@@ -1,4 +1,4 @@ | |
-{if $product.warehouse_amount} | |
+{if $product.warehouse_amount !== null} | |
{if $is_form_readonly} | |
<div class="product-variations__table-quantity">{$product.warehouse_amount}</div> | |
{else} | |
diff --git a/var/langs/en/addons/warehouses.po b/var/langs/en/addons/warehouses.po | |
index 93b9199cd6..e32b46f110 100644 | |
--- a/var/langs/en/addons/warehouses.po | |
+++ b/var/langs/en/addons/warehouses.po | |
@@ -5,8 +5,8 @@ msgstr "Project-Id-Version: tygh\n" | |
"Language: en_US" | |
msgctxt "Addons::name::warehouses" | |
-msgid "Warehouses [Beta]" | |
-msgstr "Warehouses [Beta]" | |
+msgid "Warehouses" | |
+msgstr "Warehouses" | |
msgctxt "Addons::description::warehouses" | |
msgid "Lets you set product stock separately for each store and warehouse. Shows to customers the availability of a product in stores or in particular rate areas." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment