Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Apr 27, 2022
0 parents commit 96b95e8
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Block/Widget/ProductPrice.php
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();
}
}
38 changes: 38 additions & 0 deletions Model/Widget/Source/Attribute.php
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;
}
}
16 changes: 16 additions & 0 deletions composer.json
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\\": ""
}
}
}
5 changes: 5 additions & 0 deletions etc/module.xml
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>
22 changes: 22 additions & 0 deletions etc/widget.xml
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>

5 changes: 5 additions & 0 deletions registration.php
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__);
5 changes: 5 additions & 0 deletions view/frontend/templates/price-block.phtml
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>

0 comments on commit 96b95e8

Please sign in to comment.