Skip to content

Commit 6984f87

Browse files
authored
Merge pull request #8 from justbetter/feature/config-set-customer-price-as-special-price
Feature/config set customer price as special price
2 parents d55bc38 + c23c8b7 commit 6984f87

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

src/Observer/CategoryPricing.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
use Magento\Authorization\Model\UserContextInterface;
77
use Magento\Catalog\Model\Product;
88
use Magento\Customer\Model\Session;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
910
use Magento\Framework\Event\Observer;
1011
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
1114

1215
class CategoryPricing implements ObserverInterface
1316
{
1417
public function __construct(
1518
protected Session $customerSession,
1619
protected UserContextInterface $userContext,
17-
protected CustomerPrice $customerPrice
20+
protected CustomerPrice $customerPrice,
21+
protected ScopeConfigInterface $scopeConfig,
22+
protected StoreManagerInterface $storeManager
1823
) {
1924
}
2025

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

39-
$product->setPrice($price);
44+
if ($this->scopeConfig->isSetFlag('customer_pricing/price/as_special_price', ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getId())) {
45+
$product->setSpecialPrice($price);
46+
} else {
47+
$product->setPrice($price);
48+
}
4049
$product->setFinalPrice($price);
4150
}
4251
}

src/Observer/FinalPrice.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Customer\Model\Session;
1111
use Magento\Framework\App\Area;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
1213
use Magento\Framework\App\State;
1314
use Magento\Framework\Event\Observer;
1415
use Magento\Framework\Event\ObserverInterface;
16+
use Magento\Store\Model\ScopeInterface;
1517
use Psr\Log\LoggerInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
1619

1720
class FinalPrice implements ObserverInterface
1821
{
@@ -23,7 +26,9 @@ public function __construct(
2326
protected LoggerInterface $logger,
2427
protected State $state,
2528
protected Quote $quote,
26-
protected CustomerPrice $customerPrice
29+
protected CustomerPrice $customerPrice,
30+
protected ScopeConfigInterface $scopeConfig,
31+
protected StoreManagerInterface $storeManager
2732
) {
2833
}
2934

@@ -53,7 +58,11 @@ public function execute(Observer $observer): void
5358
}
5459

5560
$product->setData('final_price', $price);
56-
$product->setData('price', $price);
61+
if ($this->scopeConfig->isSetFlag('customer_pricing/price/as_special_price', ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getId())) {
62+
$product->setData('special_price', $price);
63+
} else {
64+
$product->setData('price', $price);
65+
}
5766
$observer->setData('product', $product);
5867
}
5968
}

src/etc/acl.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
4+
<acl>
5+
<resources>
6+
<resource id="Magento_Backend::admin">
7+
<resource id="Magento_Backend::stores">
8+
<resource id="Magento_Backend::stores_settings">
9+
<resource id="Magento_Config::config">
10+
<resource id="JustBetter_CustomerPricing::config_justbetter_customer_pricing" title="Customer Pricing" sortOrder="99999" />
11+
</resource>
12+
</resource>
13+
</resource>
14+
</resource>
15+
</resources>
16+
</acl>
17+
</config>

src/etc/adminhtml/system.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
4+
<system>
5+
<tab id="justbetter" translate="label" sortOrder="1">
6+
<label>justbetter</label>
7+
</tab>
8+
<section id="customer_pricing" sortOrder="15" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
9+
<label>Customer Pricing</label>
10+
<tab>justbetter</tab>
11+
<resource>JustBetter_CustomerPricing::config_justbetter_customer_pricing</resource>
12+
<group id="price" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
13+
<field id="as_special_price" translate="label comment" type="select" sortOrder="1" showInDefault="1" showInWebsite="1"
14+
showInStore="1">
15+
<label>Set price as special price</label>
16+
<comment>Render customer price as a special price on the frontend</comment>
17+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
18+
</field>
19+
</group>
20+
</section>
21+
</system>
22+
</config>

src/etc/config.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
3+
<default>
4+
<customer_pricing>
5+
<price>
6+
<as_special_price>0</as_special_price>
7+
</price>
8+
</customer_pricing>
9+
</default>
310
</config>

0 commit comments

Comments
 (0)