custom/plugins/NimbitsPriceOnRequestNext/src/Service/PriceOnRequestService.php line 83

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nimbits\NimbitsPriceOnRequestNext\Service;
  3. use Nimbits\NimbitsPriceOnRequestNext\Setting\Service\SettingService;
  4. use Shopware\Core\Framework\Struct\StructCollection;
  5. class PriceOnRequestService
  6. {
  7.     public const SYSTEM_CONFIG_DOMAIN = 'NimbitsPriceOnRequestNext.config.';
  8.     /** @var SettingService $settingsService */
  9.     private $settingsService;
  10.     
  11.     /** @var string */
  12.     private $salesChannelId;
  13.     
  14.     /**
  15.      * Creates a new instance of the service.
  16.      *
  17.      * @param SettingService $settingsService
  18.      */
  19.     public function __construct(
  20.         SettingService $settingsService,
  21.          ?string $salesChannelId = null
  22.     )
  23.     {
  24.         $this->settingsService = $settingsService;
  25.         $this->salesChannelId = $salesChannelId;
  26.     }
  27.     
  28.     /**
  29.      * Sets the sales channel to get the configuration for.
  30.      *
  31.      * @param string $salesChannelId
  32.      */
  33.     public function setSalesChannelId(string $salesChannelId): void
  34.     {
  35.         $this->salesChannelId = $salesChannelId;
  36.     }
  37.     //helper for the plugin config
  38.     private function getPluginConfig(){
  39.         $config = $this->settingsService->getSettingsAsStruct($this->salesChannelId);
  40.         return $config;
  41.     }
  42.     
  43.     //calculates array of rulesets to one major ruleset which applies to the father and all its children articles
  44.     public function sortRulesets($rulesets){
  45.         
  46.         //default set
  47.         $fnRuleset = [
  48.                     "is_por" => false,
  49.                     "is_purchaseable" => true,
  50.                     "show_price" => true,
  51.                     "breakPorRules" => false
  52.                 ];
  53.         
  54.         foreach($rulesets AS $ruleset){
  55.             
  56.             if($ruleset->is_por == true){
  57.                 $fnRuleset["is_por"] = true;
  58.             }
  59.             
  60.             if($ruleset->is_purchaseable == false){
  61.                 $fnRuleset["is_purchaseable"] = false;
  62.             }
  63.             
  64.             if($ruleset->show_price == false){
  65.                 $fnRuleset["show_price"] = false;
  66.             }
  67.             
  68.             if($ruleset->breakPorRules == true){
  69.                 $fnRuleset["breakPorRules"] = true;
  70.             }
  71.         }
  72.         
  73.         return (new StructCollection())->assign($fnRuleset);
  74.     }
  75.     
  76.     
  77.     public function getPriceOnRequestRules($product, $cg){
  78.         $isPor = $this->checkForPOR($product);
  79.         $isPurchaseable = $this->isPurchaseable($product, $isPor);
  80.         $showPrice = $this->showPrice($product, $isPor);
  81.         
  82.         $breakPorRules = $this->validateCustomergroupRules($cg);
  83.         
  84.         if($breakPorRules){
  85.             $isPor = false;
  86.             $isPurchaseable = true;
  87.             $showPrice = true;
  88.         }
  89.     
  90.         return (new StructCollection())->assign([
  91.                     "is_por" => $isPor,
  92.                     "is_purchaseable" => $isPurchaseable,
  93.                     "show_price" => $showPrice,
  94.                     "breakPorRules" => $breakPorRules
  95.                 ]);
  96.     }
  97.     
  98.     //returns true or false depending if checkout has to be disabled for this single product
  99.     public function checkForProductCheckoutDisabled($product, $cg){
  100.         $isPor = $this->checkForPOR($product);
  101.         $isPurchaseable = $this->isPurchaseable($product, $isPor);
  102.         $showPrice = $this->showPrice($product, $isPor);
  103.         
  104.         $breakPorRules = $this->validateCustomergroupRules($cg);
  105.         
  106.         if($breakPorRules){
  107.             $isPor = false;
  108.             $isPurchaseable = true;
  109.             $showPrice = true;
  110.         }
  111.     
  112.         if($isPor && !$isPurchaseable && !$showPrice){
  113.             return true;
  114.         }    
  115.         
  116.         return false;
  117.     }
  118.     
  119.     //determines weather or not the checkout has to be disabled depending on the products in the cart
  120.     public function checkForCheckoutDisabled($products, $cg){    
  121.     
  122.         $config = $this->getPluginConfig();
  123.         if($config->nbporcheckoutdeactivate){
  124.             return (new StructCollection())->assign(['checkoutdisabled' => true]);
  125.         }
  126.         
  127.         foreach($products AS $product){
  128.             if($this->checkForProductCheckoutDisabled($product, $cg)){
  129.                 return (new StructCollection())->assign(['checkoutdisabled' => true]);
  130.             }
  131.         }
  132.         
  133.         return (new StructCollection())->assign(['checkoutdisabled' => false]);
  134.     }
  135.     
  136.     //checks if a product is price on request
  137.     public function checkForPOR($product){    
  138.     
  139.         $config = $this->getPluginConfig();
  140.         
  141.         $highest = 0;
  142.         
  143.         //iteration for advanced pricing
  144.         
  145.         foreach($product->get("price")->getIterator() as $i => $item) {
  146.             if($item->getGross() > $highest){
  147.                     $highest = $item->getGross();
  148.             }                        
  149.         }
  150.         
  151.         $productprices = $product->get("prices");
  152.         
  153.         
  154.         //iteration for advanced pricing
  155.         foreach($productprices->getIterator() as $i => $item) {
  156.             foreach($item->get("price") as $j => $jtem) {
  157.                 if($jtem->getGross() > $highest){
  158.                     $highest = $jtem->getGross();
  159.                 }            
  160.             }
  161.         }
  162.         
  163.         //config price is above value for advanced pricing
  164.         if($highest > $config->porabove){
  165.             return true;
  166.         }
  167.         
  168.         //onfig price is exactly this value for advanced pricing
  169.         if($highest == $config->porequals){
  170.             return true;
  171.         }
  172.     
  173.         //config price is above value
  174.         if($product->get("calculatedPrice")->getTotalPrice() > $config->porabove){
  175.             return true;
  176.         }
  177.         
  178.         //config price is exactly this value
  179.         if($product->get("calculatedPrice")->getTotalPrice() == $config->porequals){
  180.             return true;
  181.         }
  182.         
  183.         //config stock below or 0
  184.         if($product->get("stock")< 1 && $config->nbporactiveviastock){
  185.             return true;
  186.         }
  187.         
  188.         
  189.         //custom field settings
  190.         $cf = $product->getCustomFields();
  191.         if(isset($cf["custom_nimbits_priceonrequest_active"]) && $cf["custom_nimbits_priceonrequest_active"]){
  192.             return true;
  193.         }
  194.         
  195.         //default
  196.         return false;
  197.     }
  198.     
  199.     //determines if a article is purchaseable
  200.     public function isPurchaseable($product, $isPor){    
  201.     
  202.         $config = $this->getPluginConfig();
  203.     
  204.         //just matters if we have a price on request article
  205.         if($isPor){
  206.             //config whitelist
  207.             if($config->nbporpurchaseable){
  208.                 return true;
  209.             }
  210.             //case addable but no price
  211.             if($config->nbporaddable){
  212.                 return false;
  213.             }
  214.             
  215.             //custom field settings
  216.             $cf = $product->getCustomFields();
  217.             
  218.             //whitelist via customfields
  219.             if(isset($cf["custom_nimbits_priceonrequest_purchaseable"]) && $cf["custom_nimbits_priceonrequest_purchaseable"]){
  220.                 return true;
  221.             }
  222.                         
  223.             //default
  224.             return false;
  225.         }
  226.         return true;
  227.     }
  228.     
  229.     //determines if a article is allowed to show its price
  230.     public function showPrice($product, $isPor){    
  231.     
  232.         $config = $this->getPluginConfig();
  233.         
  234.         if($isPor){
  235.             //case addable but no price
  236.             if($config->nbporaddable){
  237.                 return false;
  238.             }
  239.             
  240.             //custom field settings
  241.             $cf = $product->getCustomFields();
  242.             
  243.             //whitelist via customfields
  244.             if(isset($cf["custom_nimbits_priceonrequest_showprice"]) && $cf["custom_nimbits_priceonrequest_showprice"]){
  245.                 return true;
  246.             }
  247.             
  248.             //default
  249.             return false;
  250.         }
  251.         return true;
  252.     }
  253.     
  254.     //validates customergroup rules for price on request, returns true if price on request rules are allowed to break
  255.     public function validateCustomergroupRules($cg){
  256.         
  257.         $config = $this->getPluginConfig();
  258.         
  259.         if(!$config->nbporcustomergroupcsvactive){
  260.             return false;
  261.         }
  262.         
  263.         try{
  264.             $nbporcustomergroupcsv = $config->nbporcustomergroupcsv;
  265.             
  266.             if(empty($nbporcustomergroupcsv)){
  267.                 return false;
  268.             }
  269.             
  270.             $cgs = explode(";", $nbporcustomergroupcsv);
  271.             
  272.             if(empty($cgs)){
  273.                 return false;
  274.             }
  275.             
  276.             foreach($cgs AS $current){
  277.                 if($current == $cg){
  278.                     return true;
  279.                 }
  280.             }
  281.         }
  282.         catch(\Exception $e){
  283.             
  284.         }
  285.         
  286.         return false;
  287.     }
  288.     
  289.     
  290. }