diff --git a/Model/AbstractPixel.php b/Model/AbstractPixel.php index 9948b85..6d7e285 100644 --- a/Model/AbstractPixel.php +++ b/Model/AbstractPixel.php @@ -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); diff --git a/Model/Config.php b/Model/Config.php index 7c8148d..aec321a 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -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 { @@ -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); } /** diff --git a/Model/Config/Source/CustomerGroups.php b/Model/Config/Source/CustomerGroups.php new file mode 100644 index 0000000..5fb82b6 --- /dev/null +++ b/Model/Config/Source/CustomerGroups.php @@ -0,0 +1,49 @@ +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; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 08dc29a..de8b410 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -63,6 +63,10 @@ Magento\Config\Model\Config\Source\Yesno Warning! Enabling this option may cause the performance impact.]]> + + + Magefan\FacebookPixel\Model\Config\Source\CustomerGroups + diff --git a/etc/config.xml b/etc/config.xml index fb0f924..ee36030 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -21,6 +21,7 @@ sku + all 1