$params); } // end getProductNewInstance protected function getProductIDFromProductInstance($product) { $facade = &$this->ecommerceFacade; if ($this->_hasVariationIDInProductInstance($product)) { $idProduct = $facade->getVariationProductID($product); } else { $idProduct = $facade->getProductID($product); } return $idProduct; } // end getProductIDFromProductInstance private function _hasVariationIDInProductInstance($product) { return (bool) $this->ecommerceFacade->getVariationProductID($product); } // end _hasVariationIDInProductInstance public function createProductInstance($idProduct) { $facade = $this->ecommerceFacade; return $facade->getProductByID($idProduct); } // end createProductInstance public function getPrice($product) { return $this->products->getRolePrice($product); } // end getPrice public function getSalePrice($product) { return $this->products->getRoleSalePrice($product); } // end getSalePrice public function isRegisteredUser() { if (!static::$userRole) { return false; } return static::$userRole; } // end isRegisteredUser public function isVariableTypeProduct($product) { return $this->ecommerceFacade->getProductType($product) == 'variable'; } // end isVariableTypeProduct public function getPriceWithDiscountOrMarkUp( $product, $originalPrice, $isSalePrice = true ) { if (!$this->isSupportedProductType($product)) { return $originalPrice; } $plugin = $this->getCorePluginInstance(); $price = $plugin->getPriceWithDiscountOrMarkUp( $product, $originalPrice, $isSalePrice ); return $this->_getQuantityDiscountByUserRole($price); } // end getPriceWithDiscountOrMarkUp public function getAmountOfDiscountOrMarkUp() { $settings = $this->getSettings(); if (!$this->_hasOptionByDiscountRoles('value', $settings)) { return false; } $userRole = $this->getUserRole(); return $settings['discountByRoles'][$userRole]['value']; } // end getAmountOfDiscountOrMarkUp private function _hasOptionByDiscountRoles( $option, $settings, $optionKey = 'discountByRoles' ) { $userRole = $this->getUserRole(); return array_key_exists($optionKey, $settings) && array_key_exists($userRole, $settings[$optionKey]) && array_key_exists($option, $settings[$optionKey][$userRole]); } // end _hasOptionByDiscountRoles public function isPercentDiscountType($optionKey = 'discountByRoles') { $settings = $this->getSettings(); $settings = $this->getPreparedQuantityDiscountOptions($settings); if (!$this->_hasOptionByDiscountRoles('type', $settings, $optionKey)) { return false; } $discountType = $settings[$optionKey][static::$userRole]['type']; return $discountType == PRICE_BY_ROLE_PERCENT_DISCOUNT_TYPE; } // end isPercentDiscountType public function isRolePriceDiscountTypeEnabled() { $settings = $this->getSettings(); $userRole = $this->getUserRole(); if (!$settings) { return false; } if (!isset($settings['discountByRoles'][$userRole]['priceType'])) { return false; } $priceType = $settings['discountByRoles'][$userRole]['priceType']; return $priceType == PRICE_BY_ROLE_DISCOUNT_TYPE_ROLE_PRICE; } // end isRolePriceDiscountTypeEnabled public function isAllowSalePrices($product) { return $this->isEnableBothRegularSalePriceSetting() && $this->hasSalePrice($product); } // end isAllowSalePrices protected function isEnableBothRegularSalePriceSetting() { $settings = $this->getSettings(); return array_key_exists('bothRegularSalePrice', $settings) && $settings['bothRegularSalePrice'] && $this->hasDiscountOrMarkUpForUserRoleInGeneralOptions(); } // end isEnableBothRegularSalePriceSetting protected function hasSalePrice($product) { return (bool) $this->ecommerceFacade->getSalePrice($product); } // end hasSalePrice public function getAmountOfDiscountOrMarkUpInPercentage($price, $discount) { return floatval($price) / 100 * $discount; } // end getAmountOfDiscountOrMarkUpInPercentage public function isDiscountTypeEnabled() { $settings = $this->getSettings(); return $settings['discountOrMakeUp'] == 'discount'; } // end isDiscountTypeEnabled public function onDisplayPriceByDiscountOrMarkupFilter($price, $product) { $product = $this->getProductNewInstance($product); if (!$this->isRegisteredUser()) { return $price; } $newPrice = $this->getPriceWithDiscountOrMarkUp($product, $price); return $this->getPriceWithFixedFloat($newPrice); } // end onDisplayPriceByDiscountOrMarkupFilter public function hasHideAllProductsOptionInSettings() { $settings = $this->getSettings(); return array_key_exists('hideAllProducts', $settings) && $settings['hideAllProducts']; } //end hasHideAllProductsOptionInSettings public function onProductPurchasable($result, $product) { $facade = $this->ecommerceFacade; $idProduct = $facade->getProductID($product); $regularPrice = $facade->getRegularPrice($product); if ($regularPrice) { return $result; } $rolePrice = $this->getRolePrice($idProduct); if (!$rolePrice) { return $result; } return $facade->isProductExists($product) && $facade->isAllowProductEdit($product); } // end onProductPurchasable public function isEnabledUserRoleTaxOptions() { $facade = EcommerceFactory::getInstance(); if (!$facade->isEnabledTaxCalculation()) { return false; } $settings = $this->getSettings(); return array_key_exists('taxOptions', $settings) && $settings['taxOptions']; } //end isEnabledUserRoleTaxOptions public function getTaxByUserRoleOptions() { $settings = $this->getSettings(); $userRole = $this->getUserRole(); if (!$userRole) { return false; } $taxByUserRoles = $settings['taxByUserRoles']; if ($this->hasUserRoleInOptions($userRole, $taxByUserRoles)) { return $taxByUserRoles[$userRole]; } return false; } //end getTaxByUserRoleOptions public function hasUserRoleInOptions($userRole, $options) { return array_key_exists($userRole, $options); } //end hasUserRoleInOptions public function doRestoreDefaultDisplayTaxValues() { $plugin = $this->getFestiModuleInstance( ModulesSwitchListener::TAXES_PLUGIN ); $plugin->doRestoreDefaultDisplayTaxValues(); } // doRestoreDefaultDisplayTaxValues public function isUserRoleDisplayTaxOptionExist() { $facade = EngineFacade::getInstance(); return $facade->getOption(PRICE_BY_ROLE_TAX_DISPLAY_OPTIONS); } // end isUserRoleDisplayTaxOptionExist private function _getMaxExecutionTime() { return ini_get('max_execution_time'); } // end _getMaxExecutionTime protected function isMaxExecutionTimeLowerThanConstant() { $executionTime = static::MAX_EXECUTION_TIME; return $this->_getMaxExecutionTime() < $executionTime; } // end isMaxExecutionTimeLowerThanConstant /** * Returns absolute path to the engine. * * @return string */ public function getBasePath() { return ENGINE_PATH; } // end getBasePath public function getTemplatePath($fileName) { return $this->getBasePath().'templates'.DIRECTORY_SEPARATOR.$fileName; } // end getTemplatesPath public function fetch($template, $vars = array()) { if ($vars) { extract($vars); } ob_start(); if (file_exists($template)) { $templatePath = $template; } else { $templatePath = $this->getTemplatePath($template); } include $templatePath; return ob_get_clean(); } // end fetch /** * @return PriceByUserRoleCorePlugin */ public function getCorePluginInstance() { if (!isset(static::$corePlugin)) { static::$corePlugin = $this->getFestiModuleInstance( ModulesSwitchListener::CORE_PRICES_PLUGIN ); } return static::$corePlugin; } // end getCorePluginInstance protected function getFestiModuleInstance($pluginName) { $core = Core::getInstance(); $module = null; if ($pluginName) { $module = $core->getPluginInstance($pluginName); } return $module; } // end getFestiModuleInstance public function hasProductID($product) { return (bool) $this->ecommerceFacade->getProductID($product); } // end hasProductID public function onInitFestiProducts() { static::$userRole = $this->getUserRole(); if (!isset($this->products)) { $this->products = $this->getProductsInstances(); } } // end onInitFestiProducts public function onInitUserRoleTaxManager() { $plugin = $this->getFestiModuleInstance( ModulesSwitchListener::TAXES_PLUGIN ); if ($this->isEnabledUserRoleTaxOptions()) { $plugin->onInitTaxListeners(); } $plugin->onInitEcommerceSettingsTaxListener(); } // end onInitUserRoleTaxManager public function onEcommercePlatformInit() { $this->onInitFestiProducts(); $this->onInitUserRoleTaxManager(); } // end onEcommercePlatformInit public function isUserRoleExist($userRole) { $userRoles = $this->getUserRoles(); return array_key_exists($userRole, $userRoles); } // end isUserRoleExist protected function isTestEnvironmentDefined() { return $this->engineFacade->isTestEnvironmentDefined(); } // end isTestEnvironmentDefined private function _getQuantityDiscountByUserRole($price) { if (!$this->_hasQuantityDiscountForUserRole($price)) { return $price; } $plugin = $this->getFestiModuleInstance( ModulesSwitchListener::QUANTITY_DISCOUNT_PLUGIN ); return $plugin->getQuantityDiscountByUserRole($price); } // end _getQuantityDiscountByUserRole private function _hasQuantityDiscountForUserRole($price) { if (!$price) { return false; } $name = ModulesSwitchListener::QUANTITY_DISCOUNT_PLUGIN; return $this->_isFrontend() && $this->getUserRole() && $this->isModuleExist($name); } // end _hasQuantityDiscountForUserRole public function getPreparedQuantityDiscountOptions($options) { $key = SettingsWooUserRolePrices::QUANTITY_DISCOUNT_OPTION_KEY; if (!isset($options[$key])) { return $options; } $quantityDiscountRange = array(); $userRoleNames = $this->getUserRoleNames(); $quantityDiscount = $options[$key]; foreach ($quantityDiscount as $value) { $name = $value['userRole']; $isVisible = isset($value['isVisible']) ? $value['isVisible'] : false; if ($isVisible) { $quantityDiscountRange[$name] = $value; } } $options[$key] = $quantityDiscountRange; return $options; } // end getPreparedQuantityDiscountOptions private function _isFrontend() { return get_class($this) == 'WooUserRolePricesFrontendFestiPlugin'; } // end _isFrontend public function isModuleExist(string $name): bool { try { Core::getInstance()->getPluginInstance($name); } catch (Exception $e) { return false; } return true; } // end isModuleExist protected function getUserRoleNames() { $userRoles = $this->getUserRoles(); return array_keys($userRoles); } // end getUserRoleNames protected function isSupportedProductType($product) { $type = $this->products->getProductType($product); return $type != FestiWooCommerceProduct::UNKNOWN_PRODUCT_TYPE; } // end isSupportedProductType }