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 = 'getModuleConfig('frontend/custom_css'); if ($customStyles) { $customStyles = 'style="' . $customStyles . '"'; } $html .= $tag . ' class="amasty-hide-price-container" ' . $customStyles . '>'; if ($image) { $mediaPath = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); $image = $mediaPath . '/amasty/hide_price/' . $image; $html .= ''; } if ($text) { $html .= '' . $text . ''; } $html .= $closeTag; } return $html; } private function getPriceBox(): string { //fix dropdown in configurable product return $this->_request->getControllerName() !== 'product' ? 'price-box price-final_price' : 'price-final_price'; } /** * @param string $href * * @return string|string */ protected function checkLoginUrl(string $href) { if (strpos($href, 'customer/account/login') !== false) { $href = $this->customerUrl->getLoginUrl(); } return $href; } /** * Generate button replacement html * @param Product|null $product * @param array|null $productData * @return string */ public function getNewAddToCartHtml($product = null, $productData = null) { // help for magento swatches detect category page $result = ''; if ($this->getModuleConfig('information/hide_button') == HideButton::REPLACE_WITH_NEW_ONE) { $text = strip_tags($this->getModuleConfig('information/replace_text')); $link = $this->getModuleConfig('information/replace_link') ?: ''; if (!$product && isset($productData['id'])) { $product = new DataObject([ 'id' => $productData['id'], 'name' => $productData['name'] ]); } switch ($this->getModuleConfig('information/replace_with')) { case ReplaceButton::REDIRECT_URL: $href = $this->checkLoginUrl((string)$this->getModuleConfig('information/redirect_link')); $href = $href ?: '#'; $tag = 'generatePopup($product); break; } $styles = strip_tags($this->getModuleConfig('information/replace_css')); $styles = $styles ? ' style="' . $styles . '"' : ''; $result .= sprintf( '%s class="amasty-hide-price-button" %s>%s', $tag, $styles, $text ); } return $result; } /** * generate Js code for Get a Quote Form * @param Product|DataObject $product * @return string */ private function generateFormJs($product) { $js = ''; return $js; } private function generateFormConfig($product) { $customer = $this->getCustomerSession()->getCustomer(); return $this->jsonEncoder->encode([ 'url' => $this->_getUrl('amasty_hide_price/request/add'), 'id' => $product->getId(), 'name' => $product->getName(), 'customer' => [ 'name' => $customer->getName(), 'email' => $customer->getEmail(), 'phone' => $customer->getPhone() ] ]); } private function convertStringSettingToArray($name) { $setting = $this->getModuleConfig($name); $setting = $this->trimAndExplode($setting); return $setting; } /** * @param $string * @return array */ private function trimAndExplode($string): array { $string = str_replace(' ', '', (string) $string); return $string ? explode(',', $string) : []; } /** * @param $productId * @return bool */ private function issetCachedResult($productId) { if (!array_key_exists($this->currentCustomerGroup, $this->cache)) { return false; } return array_key_exists($productId, $this->cache[$this->currentCustomerGroup]); } /** * @param $productId * @return mixed */ private function getResultFromCache($productId) { return $this->cache[$this->currentCustomerGroup][$productId]; } /** * @param $result * @param $productId */ private function saveResultToCache($result, $productId) { if (!array_key_exists($this->currentCustomerGroup, $this->cache)) { $this->cache[$this->currentCustomerGroup] = []; } $this->cache[$this->currentCustomerGroup][$productId] = $result; } /** * @return bool */ public function isGDPREnabled() { return (bool)$this->getModuleConfig('gdpr/enabled'); } /** * @return string */ public function getGDPRText() { return $this->filterManager->stripTags( $this->getModuleConfig('gdpr/text'), [ 'allowableTags' => '', 'escape' => false ] ); } /** * @param Product|DataObject $product * @return string */ private function generatePopup($product) { $popupHtml = $this->generateFormJs($product) . '_moduleManager->isEnabled('Amasty_Customform'); } /** * Checking module setting and output * @param ProductInterface $product * @return bool */ public function isNeedHidePrice(ProductInterface $product) { return $this->getModuleConfig('information/hide_price') && $this->isApplied($product); } }