categoryCollectionFactory = $categoryCollectionFactory; $this->storeManager = $storeManager; $this->jsonEncoder = $jsonEncoder; $this->sessionFactory = $sessionFactory; $this->currentCustomerGroup = $this->getCustomerSession()->getCustomerGroupId(); if (!$this->currentCustomerGroup) { $this->currentCustomerGroup = self::NOT_LOGGED_KEY; } $this->filterManager = $filterManager; $this->customerUrl = $customerUrl; } private function getCustomerSession() { return $this->sessionFactory->create(); } public function getModuleConfig(string $path): string { return (string)$this->scopeConfig->getValue('amasty_hide_price/' . $path, ScopeInterface::SCOPE_STORE); } /** * @return bool */ public function isModuleEnabled() { return $this->getModuleConfig('general/enabled') && $this->isModuleOutputEnabled(); } /** * @param ProductInterface $product * @return bool */ public function isApplied(ProductInterface $product) { if (!$this->isModuleEnabled()) { return false; } if (!$this->issetCachedResult($product->getId())) { /* Checking settings by product and customer group. Order is important.*/ $result = $this->checkGlobalSettings($product); $result = $this->checkStockStatus($result, $product); $result = $this->checkCategorySettings($result, $product); $result = $this->checkProductSettings($result, $product); $result = $this->checkIgnoreSettings($result, $product); // $result = $this->checkByNetsuiteStatus($result); /* save result to cache for multiple places: price button add to wishlist and other*/ $this->saveResultToCache($result, $product->getId()); } else { $result = $this->getResultFromCache($product->getId()); } return $result; } /** * Checking module setting and output * @param ProductInterface $product * @return bool */ public function isNeedHideProduct(ProductInterface $product) { $isConfigEnabled = $this->getModuleConfig('information/hide_price') || $this->getHideAddToCart(); return $isConfigEnabled && $this->isApplied($product); } /** * @return int */ public function getHideAddToCart() { return (int)$this->getModuleConfig('information/hide_button'); } /** * @return bool */ public function getHideWishlist() { return (bool)$this->getModuleConfig('information/hide_wishlist'); } /** * Hide Price depend on selected categories and customer groups in configuration * @param ProductInterface $product * @return bool */ private function checkGlobalSettings(ProductInterface $product) { $result = false; $settingCustomerGroup = $this->convertStringSettingToArray('general/customer_group'); if (in_array($this->currentCustomerGroup, $settingCustomerGroup)) { $productCategories = $product->getCategoryIds(); $settingCategories = $this->convertStringSettingToArray('general/category'); //check for root category - hide price for all $result = in_array(self::ROOT_CATEGORY_ID, $settingCategories) || count(array_uintersect($productCategories, $settingCategories, "strcmp")) > 0 ? true: false; } return $result; } /** * Plugin in stock status must work * * @param $result * @param ProductInterface $product * * @return bool */ public function checkStockStatus($result, ProductInterface $product) { return $result; } /** * Hide Price depend on selected individual category settings * @param $result * @param ProductInterface $product * @return bool */ private function checkCategorySettings($result, ProductInterface $product) { $productCategories = $product->getCategoryIds(); if (!$this->matchedCategories) { /* get categories only with not empty attributes customer_gr_cat and mode_cat */ $collection = $this->categoryCollectionFactory->create() ->addAttributeToSelect('am_hide_price_mode_cat') ->addAttributeToSelect('am_hide_price_customer_gr_cat') ->addAttributeToFilter('am_hide_price_mode_cat', ['notnull' => true]) ->addAttributeToFilter('am_hide_price_customer_gr_cat', ['notnull' => true]); $this->matchedCategories = $collection->getData(); } if (!empty($this->matchedCategories)) { foreach ($this->matchedCategories as $category) { if (!in_array($category['entity_id'], $productCategories)) { continue; } $customerGroups = $this->trimAndExplode($category['am_hide_price_customer_gr_cat']); if (in_array($this->currentCustomerGroup, $customerGroups)) { $result = !(bool)$category['am_hide_price_mode_cat']; } } } return $result; } /** * Hide Price depend on selected individual product settings * @param $result * @param ProductInterface $product * @return bool */ private function checkProductSettings($result, ProductInterface $product) { $mode = $product->getData('am_hide_price_mode'); $customerGroups = $product->getData('am_hide_price_customer_gr'); if ($mode !== null && $customerGroups) { $customerGroups = $this->trimAndExplode($customerGroups); if (in_array($this->currentCustomerGroup, $customerGroups)) { $result = !(bool)$mode; } } return $result; } private function checkByNetsuiteStatus($result) { $customerSession = $this->getCustomerSession(); if ($customerSession->isLoggedIn()) { $customer = $customerSession->getCustomer(); $dataCustomer = $customer->getDataModel(); $netsuiteStatus = $dataCustomer->getExtensionAttributes()?->getNetsuiteStatus(); $netsuiteStatus = $netsuiteStatus? strtolower($netsuiteStatus):''; if (in_array($netsuiteStatus, Status::getApprovedStatuses())) { $result = true; } } $result = false; return $result; } /** * Check ignore settings - the most important * @param $result * @param ProductInterface $product * @return bool */ private function checkIgnoreSettings($result, ProductInterface $product) { $currentCustomerId = $this->getCustomerSession()->getCustomerId(); if ($currentCustomerId) { $ignoredCustomers = $this->convertStringSettingToArray('general/ignore_customer'); if (in_array($currentCustomerId, $ignoredCustomers)) { return false; } } $ignoredProductIds = $this->convertStringSettingToArray('general/ignore_products'); if (in_array($product->getId(), $ignoredProductIds)) { return false; } return $result; } /** * Generate button html depend on module configuration * @param $product * @return string */ public function getNewPriceHtmlBox($product) { $priceBox = $this->getPriceBox(); // help for magento swatches detect category page $html = sprintf('
', $priceBox, $product->getId()); $text = $this->filterManager->stripTags( $this->getModuleConfig('frontend/text'), [ 'allowableTags' => null, 'escape' => true ] ); $image = $this->getModuleConfig('frontend/image'); if ($text || $image) { $href = $this->getModuleConfig('frontend/link'); if ($href) { if ($href == self::HIDE_PRICE_POPUP_IDENTIFICATOR) { $tag = $this->generatePopup($product); } else { $href = $this->checkLoginUrl($href); $tag = ''; } else { $tag = '