-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fd7a11
commit 62d190c
Showing
7 changed files
with
184 additions
and
60 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace DevStone\UsageCalculator\Controller\Usage; | ||
|
||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Controller\ResultFactory; | ||
use Magento\Framework\App\Action\HttpGetActionInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
class Ajax extends Action implements HttpGetActionInterface | ||
{ | ||
|
||
public function __construct( | ||
\Magento\Framework\App\Action\Context $context, | ||
private readonly \Magento\Framework\Registry $coreRegistry, | ||
private readonly \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, | ||
) { | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* Dispatch request | ||
* | ||
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface | ||
* @throws \Magento\Framework\Exception\NotFoundException | ||
*/ | ||
public function execute() | ||
{ | ||
$productId = (int)$this->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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" ?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> | ||
<router id="standard"> | ||
<route id="devstone_usagecalculator" frontName="usage"> | ||
<module name="DevStone_UsageCalculator"/> | ||
</route> | ||
</router> | ||
</config> |
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
28 changes: 28 additions & 0 deletions
28
view/frontend/layout/devstone_usagecalculator_usage_ajax.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0"?> | ||
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd"> | ||
<container name="root"> | ||
<block class="DevStone\UsageCalculator\Block\Catalog\Product\Usage" name="usage.info" as="type_image_usage" before="-" template="DevStone_UsageCalculator::catalog/product/usages-container.phtml"> | ||
<block class="DevStone\UsageCalculator\Block\Catalog\Product\Usage" name="all.usages" template="DevStone_UsageCalculator::catalog/product/usages.phtml"> | ||
<arguments> | ||
<argument name="cache_lifetime" xsi:type="number">360</argument> | ||
</arguments> | ||
|
||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\DefaultType" name="usage.info.options.default" as="default" template="DevStone_UsageCalculator::catalog/product/type/default.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Text" name="usage.info.options.text" as="text" template="DevStone_UsageCalculator::catalog/product/type/text.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\File" name="usage.info.options.file" as="file" template="DevStone_UsageCalculator::catalog/product/type/file.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Select" name="usage.info.options.select" as="select" template="DevStone_UsageCalculator::catalog/product/type/select.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Date" name="usage.info.options.date" as="date" template="DevStone_UsageCalculator::catalog/product/type/date.phtml"/> | ||
</block> | ||
<block class="DevStone\UsageCalculator\Block\Catalog\Product\Usage" name="custom.usages" template="DevStone_UsageCalculator::catalog/product/usages.phtml"> | ||
<arguments> | ||
<argument name="customer_specific" xsi:type="boolean">true</argument> | ||
</arguments> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\DefaultType" name="custom.usage.info.options.default" as="default" template="DevStone_UsageCalculator::catalog/product/type/default.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Text" name="custom.usage.info.options.text" as="text" template="DevStone_UsageCalculator::catalog/product/type/text.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\File" name="custom.usage.info.options.file" as="file" template="DevStone_UsageCalculator::catalog/product/type/file.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Select" name="custom.usage.info.options.select" as="select" template="DevStone_UsageCalculator::catalog/product/type/select.phtml"/> | ||
<block class="DevStone\UsageCalculator\Block\Usage\View\Options\Type\Date" name="custom.usage.info.options.date" as="date" template="DevStone_UsageCalculator::catalog/product/type/date.phtml"/> | ||
</block> | ||
</block> | ||
</container> | ||
</layout> |
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
40 changes: 40 additions & 0 deletions
40
view/frontend/templates/catalog/product/usages-loader.phtml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php /* @var $block \DevStone\UsageCalculator\Block\Catalog\Product\Usage */ ?> | ||
<?php if ($block->getProduct()->isSaleable() ):?> | ||
<div id="usages-container" class="usages-container field" <?php if (!$block->showButton()): ?>style="margin-bottom: 0;" <?php endif; ?>> | ||
<?php if ($block->showButton()): ?> | ||
<button id="usage-button" class="cta-button" title="<?= __('License Image is a type of “renting” of an image for a specific use; examples include licensing an image for the cover of a book, an illustration for a magazine article, or use of an image in an evangelistic handbill.') ?>"> | ||
<span class="calc-icon"> </span> | ||
<span class="button-main"> | ||
<span><?= __('License Image') ?></span> | ||
<span class="button-small"> <?= __('Download - price based on use.') ?></span> | ||
</span> | ||
</button> | ||
<?php endif; ?> | ||
</div> | ||
<?php endif; ?> | ||
|
||
<script> | ||
require([ | ||
'jquery' | ||
], function($) { | ||
$('#usage-button').one('click', function(event ){ | ||
event.preventDefault() | ||
// observer.disconnect(); | ||
|
||
$.ajax({ | ||
method: 'GET', | ||
url: '<?= $block->getBaseUrl() ?>usage/usage/ajax?id=<?= $block->getProduct()->getId() ?>', | ||
showLoader: false, | ||
cache:true | ||
}).then(function (res) { | ||
location.hash = "#license"; | ||
$('#usages-container').html(res).trigger('contentUpdated'); | ||
}); | ||
|
||
|
||
}); | ||
if (location.hash.substr(0, 8) === '#license') { | ||
$('#usage-button').click(); | ||
} | ||
}); | ||
</script> |