Skip to content

Commit

Permalink
add configuration for showing special price
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Mulder committed Jul 19, 2024
1 parent d55bc38 commit cd8c095
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Observer/CategoryPricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
use Magento\Authorization\Model\UserContextInterface;
use Magento\Catalog\Model\Product;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class CategoryPricing implements ObserverInterface
{
public function __construct(
protected Session $customerSession,
protected UserContextInterface $userContext,
protected CustomerPrice $customerPrice
protected CustomerPrice $customerPrice,
protected ScopeConfigInterface $scopeConfig,
protected StoreManagerInterface $storeManager
) {
}

Expand All @@ -36,7 +41,11 @@ public function execute(Observer $observer): void
continue;
}

$product->setPrice($price);
if ($this->scopeConfig->isSetFlag('customer_pricing/price/as_special_price',ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getId())) {
$product->setSpecialPrice($price);
} else {
$product->setPrice($price);
}
$product->setFinalPrice($price);
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/Observer/FinalPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoreManagerInterface;

class FinalPrice implements ObserverInterface
{
Expand All @@ -23,7 +26,9 @@ public function __construct(
protected LoggerInterface $logger,
protected State $state,
protected Quote $quote,
protected CustomerPrice $customerPrice
protected CustomerPrice $customerPrice,
protected ScopeConfigInterface $scopeConfig,
protected StoreManagerInterface $storeManager
) {
}

Expand Down Expand Up @@ -53,7 +58,11 @@ public function execute(Observer $observer): void
}

$product->setData('final_price', $price);
$product->setData('price', $price);
if ($this->scopeConfig->isSetFlag('customer_pricing/price/as_special_price', ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getId())) {
$product->setData('special_price', $price);
} else {
$product->setData('price', $price);
}
$observer->setData('product', $product);
}
}
17 changes: 17 additions & 0 deletions src/etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="JustBetter_CustomerPricing::config_justbetter_customer_pricing" title="Customer Pricing" sortOrder="99999" />
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
22 changes: 22 additions & 0 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="justbetter" translate="label" sortOrder="1">
<label>justbetter</label>
</tab>
<section id="customer_pricing" sortOrder="15" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Customer Pricing</label>
<tab>justbetter</tab>
<resource>JustBetter_CustomerPricing::config_justbetter_customer_pricing</resource>
<group id="price" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="as_special_price" translate="label comment" type="select" sortOrder="1" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Set price as special price</label>
<comment>Render customer price as a special price on the frontend</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
7 changes: 7 additions & 0 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<customer_pricing>
<price>
<as_special_price>0</as_special_price>
</price>
</customer_pricing>
</default>
</config>

0 comments on commit cd8c095

Please sign in to comment.