-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 96b95e8
Showing
7 changed files
with
172 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Webidea24\WidgetProductPrice\Block\Widget; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Pricing\Price\FinalPrice; | ||
use Magento\Framework\DataObject\IdentityInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\Pricing\Render; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Widget\Block\BlockInterface; | ||
|
||
class ProductPrice extends Template implements BlockInterface, IdentityInterface | ||
{ | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
protected $_template = 'Webidea24_WidgetProductPrice::price-block.phtml'; | ||
|
||
public function __construct( | ||
Template\Context $context, | ||
ProductRepositoryInterface $productRepository, | ||
array $data = [] | ||
) | ||
{ | ||
parent::__construct($context, $data); | ||
$this->productRepository = $productRepository; | ||
} | ||
|
||
protected function _toHtml() | ||
{ | ||
return $this->getProduct() ? parent::_toHtml() : ''; | ||
} | ||
|
||
public function getPriceBlock() | ||
{ | ||
/** @var Render $priceRender */ | ||
$priceRender = $this->getLayout()->getBlock('product.price.render.default'); | ||
if ($priceRender) { | ||
return $priceRender->render( | ||
FinalPrice::PRICE_CODE, | ||
$this->getProduct(), | ||
[ | ||
'include_container' => true, | ||
'display_minimal_price' => true, | ||
'zone' => Render::ZONE_ITEM_VIEW, | ||
] | ||
); | ||
} | ||
} | ||
|
||
public function getProduct(): ?Product | ||
{ | ||
if (!$product = $this->getData('_product')) { | ||
$idPath = $this->getData('id_path'); | ||
$exploded = explode('/', $idPath); | ||
if (!isset($exploded[1])) { | ||
return null; | ||
} | ||
|
||
try { | ||
$product = $this->productRepository->getById((int)$exploded[1]); | ||
} catch (NoSuchEntityException $e) { | ||
return null; | ||
} | ||
$this->setData('_product', $product); | ||
} | ||
|
||
/** @noinspection PhpIncompatibleReturnTypeInspection */ | ||
return $product; | ||
} | ||
|
||
public function getIdentities() | ||
{ | ||
return ['widget_product_price'] + $this->getProduct()->getIdentities(); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Webidea24\WidgetProductPrice\Model\Widget\Source; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Eav\Model\Config; | ||
use Magento\Framework\Data\OptionSourceInterface; | ||
|
||
class Attribute implements OptionSourceInterface | ||
{ | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $eavConfig; | ||
|
||
public function __construct( | ||
Config $eavConfig | ||
) | ||
{ | ||
$this->eavConfig = $eavConfig; | ||
} | ||
|
||
public function toOptionArray() | ||
{ | ||
$attributes = $this->eavConfig->getEntityAttributes(Product::ENTITY); | ||
$options = []; | ||
foreach ($attributes as $attribute) { | ||
$options[$attribute->getAttributeCode()] = [ | ||
'value' => $attribute->getAttributeCode(), | ||
'label' => $attribute->getDefaultFrontendLabel() . ' (' . $attribute->getAttributeCode() . ')' | ||
]; | ||
} | ||
ksort($options); | ||
|
||
return $options; | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"name": "webidea24/magento2-module-widget-price-box", | ||
"description": "Magento 2 Module: Widget: Adds a price box for a product", | ||
"type": "magento2-module", | ||
"license": [ | ||
"MIT" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Webidea24\\WidgetProductPrice\\": "" | ||
} | ||
} | ||
} |
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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Webidea24_WidgetProductPrice"/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd"> | ||
|
||
<widget id="product_price-box" class="Webidea24\WidgetProductPrice\Block\Widget\ProductPrice" is_email_compatible="true"> | ||
<label translate="true">Product Price Box</label> | ||
<description translate="true">Display the pricebox of a product with the actual price</description> | ||
<parameters> | ||
<parameter name="id_path" xsi:type="block" visible="true" required="true" sort_order="10"> | ||
<label translate="true">Product</label> | ||
<block class="Magento\Catalog\Block\Adminhtml\Product\Widget\Chooser"> | ||
<data> | ||
<item name="button" xsi:type="array"> | ||
<item name="open" xsi:type="string" translate="true">Select Product...</item> | ||
</item> | ||
</data> | ||
</block> | ||
</parameter> | ||
</parameters> | ||
</widget> | ||
</widgets> | ||
|
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,5 @@ | ||
<?php | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webidea24_WidgetProductPrice', __DIR__); |
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,5 @@ | ||
<?php | ||
/** @var \Webidea24\WidgetProductPrice\Block\Widget\ProductPrice $block */ | ||
?> | ||
|
||
<div class="product-price-block"><?= $block->getPriceBlock() ?></div> |