Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Model/AbstractPixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ protected function formatPrice(float $price): float
*/
protected function getPrice(Product $product): float
{
if (!$this->config->isCustomerGroupAllowedToSeeProductPrice()) {
return 0.0;
}

$priceInfo = $product->getPriceInfo()->getPrice('final_price')->getAmount();
$price = $priceInfo->getValue();
return $this->formatPrice($price);
Expand Down
36 changes: 35 additions & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Customer\Model\Session as CustomerSession;

class Config
{
Expand All @@ -36,20 +37,53 @@ class Config
*/
public const XML_PATH_PROTECT_CUSTOMER_DATA = 'mffacebookpixel/customer_data/protect';

/**
* Display Product Price For customer groups
*/
public const XML_PATH_DISPLAY_PRODUCT_PRICE_FOR = 'mffacebookpixel/attributes/display_product_price_for';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var CustomerSession|null
*/
private $customerSession;

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param CustomerSession|null $customerSession
*/
public function __construct(
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
?CustomerSession $customerSession = null
) {
$this->scopeConfig = $scopeConfig;
$this->customerSession = $customerSession;
}

/**
* @return bool
*/
public function isCustomerGroupAllowedToSeeProductPrice(): bool
{
$allowedGroups = $this->getConfig(self::XML_PATH_DISPLAY_PRODUCT_PRICE_FOR);

if (!$allowedGroups) {
return false;
}

$allowedGroups = explode(',', $allowedGroups);

$session = $this->customerSession ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(CustomerSession::class);
$customerGroupId = (string)$session->getCustomerGroupId();

return in_array('all', $allowedGroups) || in_array($customerGroupId, $allowedGroups);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions Model/Config/Source/CustomerGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\FacebookPixel\Model\Config\Source;

use Magento\Customer\Model\ResourceModel\Group\CollectionFactory as GroupCollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;

class CustomerGroups implements OptionSourceInterface
{
/**
* @var GroupCollectionFactory
*/
private $groupCollectionFactory;

/**
* @param GroupCollectionFactory $groupCollectionFactory
*/
public function __construct(
GroupCollectionFactory $groupCollectionFactory
) {
$this->groupCollectionFactory = $groupCollectionFactory;
}

/**
* @return array
*/
public function toOptionArray(): array
{
$options = [
['value' => 'all', 'label' => __('All Customer Groups')]
];

$groups = $this->groupCollectionFactory->create();
foreach ($groups as $group) {
$options[] = [
'value' => $group->getId(),
'label' => $group->getCustomerGroupCode()
];
}

return $options;
}
}
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[<strong class="colorRed">Warning!</strong> Enabling this option may cause the performance impact.]]></comment>
</field>
<field id="display_product_price_for" translate="label" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Display Product Price For</label>
<source_model>Magefan\FacebookPixel\Model\Config\Source\CustomerGroups</source_model>
</field>
</group>
<group id="page_speed_optimization" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Page Speed Optimization</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<attributes>
<product>sku</product>
<categories/>
<display_product_price_for>all</display_product_price_for>
</attributes>
<page_speed_optimization>
<enabled>1</enabled>
Expand Down