diff --git a/Block/Catalog/Product/Usage.php b/Block/Catalog/Product/Usage.php index aec9d41..9d51e0b 100644 --- a/Block/Catalog/Product/Usage.php +++ b/Block/Catalog/Product/Usage.php @@ -133,7 +133,7 @@ public function getAllCustomUsages() $customerUsageItems = $this->usageRepository->getList($searchCriteria)->getItems(); /** @var \DevStone\UsageCalculator\Model\Usage $customerUsageItem */ foreach ($customerUsageItems as $key => $customerUsageItem) { - if ( ! $customerUsageItem->getConditions()->validate($this->getProduct())) { + if (! $customerUsageItem->getConditions()->validate($this->getProduct())) { unset($customerUsageItems[$key]); } } @@ -175,7 +175,7 @@ public function getUsages($category = null): array $customerUsageItems = $this->usageRepository->getList($searchCriteria)->getItems(); /** @var \DevStone\UsageCalculator\Model\Usage $customerUsageItem */ foreach ($customerUsageItems as $key => $customerUsageItem) { - if(!$customerUsageItem->getConditions()->validate($this->getProduct())) { + if (!$customerUsageItem->getConditions()->validate($this->getProduct())) { unset($customerUsageItems[$key]); } } @@ -220,28 +220,29 @@ public function getUsageListAccordingToCustomer($customerId): Collection return $usageCustomerCollection; } - public function getAllCategories() { + public function getAllCategories() + { - if ($this->isCustomerLoggedIn()) { - $customerId = $this->getCustomerId(); - $usageCollection = $this->getUsageListAccordingToCustomer($customerId); - if ($usageCollection->getSize() > 0) { - $searchCriteria = $this->searchCriteriaBuilder->create(); - return $this->categoryRepository->getList($searchCriteria)->getItems(); - } - } + if ($this->isCustomerLoggedIn()) { + $customerId = $this->getCustomerId(); + $usageCollection = $this->getUsageListAccordingToCustomer($customerId); + if ($usageCollection->getSize() > 0) { + $searchCriteria = $this->searchCriteriaBuilder->create(); + return $this->categoryRepository->getList($searchCriteria)->getItems(); + } + } - $searchCriteria = $this->searchCriteriaBuilder - ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'neq') - ->create(); - return $this->categoryRepository->getList($searchCriteria)->getItems(); - } + $searchCriteria = $this->searchCriteriaBuilder + ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'neq') + ->create(); + return $this->categoryRepository->getList($searchCriteria)->getItems(); + } public function getCustomLicenseCategory() { $searchCriteria = $this->searchCriteriaBuilder - ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'eq') - ->create(); - $items = $this->categoryRepository->getList($searchCriteria)->getItems(); + ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'eq') + ->create(); + $items = $this->categoryRepository->getList($searchCriteria)->getItems(); return array_pop($items); } /** @@ -249,19 +250,19 @@ public function getCustomLicenseCategory() */ public function getCategories(): array { - if ( $this->hasData('customer_specific') && $this->getdata('customer_specific') ) { - if ($this->isCustomerLoggedIn()) { - $customerId = $this->getCustomerId(); - $usageCollection = $this->getUsageListAccordingToCustomer($customerId); - if ($usageCollection->getSize() > 0) { - $searchCriteria = $this->searchCriteriaBuilder - ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'eq') - ->create(); - return $this->categoryRepository->getList($searchCriteria)->getItems(); - } - } - return []; - } + if ($this->hasData('customer_specific') && $this->getdata('customer_specific')) { + if ($this->isCustomerLoggedIn()) { + $customerId = $this->getCustomerId(); + $usageCollection = $this->getUsageListAccordingToCustomer($customerId); + if ($usageCollection->getSize() > 0) { + $searchCriteria = $this->searchCriteriaBuilder + ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'eq') + ->create(); + return $this->categoryRepository->getList($searchCriteria)->getItems(); + } + } + return []; + } $searchCriteria = $this->searchCriteriaBuilder ->addFilter('entity_id', $this->config->getCustomLicenseId(), 'neq') diff --git a/Controller/Usage/Ajax.php b/Controller/Usage/Ajax.php new file mode 100644 index 0000000..9202d9e --- /dev/null +++ b/Controller/Usage/Ajax.php @@ -0,0 +1,70 @@ +getRequest()->getParam('id'); + $product = $this->loadProduct($productId); + + if (! $product) { + $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD); + $resultForward->forward('noroute'); + return $resultForward; + } + + return $this->resultFactory->create(ResultFactory::TYPE_LAYOUT); + } + + + /** + * Load product model with data by passed id. + * Return false if product was not loaded or has incorrect status. + * + * @param int $productId + */ + protected function loadProduct($productId) + { + if (!$productId) { + return false; + } + + try { + $product = $this->productRepository->getById($productId); + + if (!$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) { + throw new NoSuchEntityException(); + } + } catch (NoSuchEntityException $noEntityException) { + return false; + } + + $this->coreRegistry->register('current_product', $product); + $this->coreRegistry->register('product', $product); + + return $product; + } +} diff --git a/etc/frontend/routes.xml b/etc/frontend/routes.xml new file mode 100644 index 0000000..3e8c4ca --- /dev/null +++ b/etc/frontend/routes.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/view/frontend/layout/catalog_product_view_type_image.xml b/view/frontend/layout/catalog_product_view_type_image.xml index 5d1f8fb..ffd8a8e 100644 --- a/view/frontend/layout/catalog_product_view_type_image.xml +++ b/view/frontend/layout/catalog_product_view_type_image.xml @@ -9,28 +9,7 @@ - - - - 360 - - - - - - - - - - - true - - - - - - - + diff --git a/view/frontend/layout/devstone_usagecalculator_usage_ajax.xml b/view/frontend/layout/devstone_usagecalculator_usage_ajax.xml new file mode 100644 index 0000000..332b187 --- /dev/null +++ b/view/frontend/layout/devstone_usagecalculator_usage_ajax.xml @@ -0,0 +1,28 @@ + + + + + + + 360 + + + + + + + + + + + true + + + + + + + + + + \ No newline at end of file diff --git a/view/frontend/templates/catalog/product/usages-container.phtml b/view/frontend/templates/catalog/product/usages-container.phtml index d60c087..f7d5829 100644 --- a/view/frontend/templates/catalog/product/usages-container.phtml +++ b/view/frontend/templates/catalog/product/usages-container.phtml @@ -1,7 +1,6 @@ -getProduct()->isSaleable() ):?> -
showButton()): ?>style="margin-bottom: 0;" > - showButton()): ?> + + -
- - + + diff --git a/view/frontend/templates/catalog/product/usages-loader.phtml b/view/frontend/templates/catalog/product/usages-loader.phtml new file mode 100644 index 0000000..b9e0b9b --- /dev/null +++ b/view/frontend/templates/catalog/product/usages-loader.phtml @@ -0,0 +1,40 @@ + +getProduct()->isSaleable() ):?> +
showButton()): ?>style="margin-bottom: 0;" > + showButton()): ?> + + +
+ + + \ No newline at end of file