src/Twig/Extension/ProductExtension.php line 549

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Twig\Extension;
  4. use App\Model\DefaultProduct;
  5. use App\Service\CartService;
  6. use App\Service\ProductAttributeData;
  7. use App\Service\ProductAttributeMapper;
  8. use App\Service\ProductAttributeRenderer;
  9. use App\Service\ShopService;
  10. use Pimcore\Log\Simple;
  11. use Twig\Extension\AbstractExtension;
  12. use Twig\TwigFunction;
  13. use Pimcore\Translation\Translator;
  14. use App\Model\Customer;
  15. /**
  16.  * Description of ProductExtension
  17.  *
  18.  * @author PcCom
  19.  */
  20. class ProductExtension extends AbstractExtension
  21. {
  22.     /**
  23.      * @var Translator
  24.      */
  25.     protected $translator;
  26.     /**
  27.      * @var CartService
  28.      */
  29.     protected $cartService;
  30.     /**
  31.      * @var ShopService
  32.      */
  33.     private $shopService;
  34.     private ProductAttributeMapper $mapper;
  35.     private ProductAttributeRenderer $renderer;
  36.     /**
  37.      * GeneralFilterExtension constructor.
  38.      *
  39.      * @param Translator $translator
  40.      * @param CartService $cartService
  41.      * @param ShopService $shopService
  42.      */
  43.     public function __construct(
  44.         Translator $translator,
  45.         CartService $cartService,
  46.         ShopService $shopService,
  47.         ProductAttributeMapper $mapper,
  48.         ProductAttributeRenderer $renderer
  49.     )
  50.     {
  51.         $this->translator $translator;
  52.         $this->cartService $cartService;
  53.         $this->shopService $shopService;
  54.         $this->mapper $mapper;
  55.         $this->renderer $renderer;
  56.     }
  57.     /**
  58.      * @return TwigFunction[]
  59.      */
  60.     public function getFunctions()
  61.     {
  62.         return [
  63.             new TwigFunction('app_product_generate_link', [$this'generateLink']),
  64.             new TwigFunction('app_product_generate_data', [$this'generateData']),
  65.             new TwigFunction('app_product_attributes_rows', [$this'getAttributesRows']),
  66.             new TwigFunction('app_product_types_rows', [$this'getTypesRows']),
  67.             new TwigFunction('app_product_variants_attributes', [$this'getVariantsAttributes']),
  68.             new TwigFunction('app_product_variant_attribute_value', [$this'getVariantsAttributeValue']),
  69.             new TwigFunction('app_product_variant_prices', [$this'getVariantsPrices']),
  70.             new TwigFunction('app_product_related_products', [$this'getRelatedProducts']),
  71.             new TwigFunction('app_product_default_price',[$this,'getDefaultPrice']),
  72.             new TwigFunction('app_product_default_price_with_discount',[$this,'getDefaultPriceWithDiscount']),
  73.             new TwigFunction('app_product_price_with_discount',[$this,'getPriceWithDiscount']),
  74.             new TwigFunction('app_product_haveError',[$this,'getHaveError']),
  75.         ];
  76.     }
  77.     public function generateData($product)
  78.     {
  79.         $data = new ProductAttributeData($this->mapper);
  80.         $data->initialize($product);
  81.         return $data;
  82.     }
  83.     public function getHaveError($product):bool
  84.     {
  85.         $haveError false;
  86.         if (!$haveError){
  87.             $haveError $this->checkVariantsWithoutPrice($product);
  88.         }
  89.         return $haveError;
  90.     }
  91.     protected function checkVariantsWithoutPrice($product):bool
  92.     {
  93.         $haveError false;
  94.         $i 0;
  95.         $data $this->generateData($product);
  96.         $variants $data->variants;
  97.         foreach ($variants as $variant){
  98.             if ($variant->getPrice()){
  99.                 $i++;
  100.             }else{
  101.                 Simple::log("products-with-errors""product ID: {$variant->getId()} Error type: missing field on price");
  102.             }
  103.         }
  104.         //ERROR PRICE FIELD IS MISSING
  105.         if ($i != count($variants)){
  106.             $haveError true;
  107.         }
  108.         return $haveError;
  109.     }
  110.     public function getAttributesRows($product$data) : ?array
  111.     {
  112.         return $this->renderer->generateRows(get_object_vars($data), $product);
  113.     }
  114.     public function getVariantsAttributes(ProductAttributeData $data)
  115.     {
  116.         return $data->getVariantAttributes();
  117.     }
  118.     public function getTypesRows($product$data): array
  119.     {
  120.         $typeArray = [];
  121.         $typeArray["Activador"] = $this->getActivador($product);
  122.         $typeArray["Base"] = $this->getBaseRow($product);
  123.         $typeArray["Catalizador"] = $this->getCatalizadorRow($product$data);
  124.         $typeArray["Converter"] = $this->getConverterRow($product);
  125.         $typeArray["Disolvente"] = $this->getDisvolventeRow($product);
  126.         $typeArray["Kit"] = $this->getKitRow($product);
  127.         $typeArray["Cleaner"] = $this->getCleanerRow($product);
  128.         $typeArray["Reducer"] = $this->getReducerRow($product);
  129.         $typeArray["Complemento"] = $this->getComplement($product);
  130.         return array_filter($typeArray);
  131.     }
  132.     protected function getActivador($product): string
  133.     {
  134.         $rowString "";
  135.         $activadors $product->getActivador();
  136.         if (count($activadors) > 0) {
  137.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Activador") . ":</td> ";
  138.             $rowString .= "<td class=\"attributevalue\">";
  139.             $resultString "";
  140.             foreach ($activadors as $activador) {
  141.                 $relProduct $activador->getElement();
  142.                 $resultString .= "<br/>";
  143.                 if (!empty($relProduct->getBsncode())) {
  144.                     $resultString .= $relProduct->getBsncode() . " - ";
  145.                 }
  146.                 $resultString .= $relProduct->getName();
  147.             }
  148.             $rowString .= trim($resultString"<br/>");
  149.             $rowString .= "</td></tr>";
  150.         }
  151.         return $rowString;
  152.     }
  153.     protected function getComplement($product){
  154.         $rowString "";
  155.         $activadors $product->getComplement();
  156.         if (count($activadors) > 0) {
  157.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Complemento") . ":</td> ";
  158.             $rowString .= "<td class=\"attributevalue\">";
  159.             $resultString "";
  160.             foreach ($activadors as $activador) {
  161.                 $relProduct $activador->getElement();
  162.                 $resultString .= "<br/>";
  163.                 if (!empty($relProduct->getBsncode())) {
  164.                     $resultString .= $relProduct->getBsncode() . " - ";
  165.                 }
  166.                 $resultString .= $relProduct->getName();
  167.             }
  168.             $rowString .= trim($resultString"<br/>");
  169.             $rowString .= "</td></tr>";
  170.         }
  171.         return $rowString;
  172.     }
  173.     protected function getBaseRow($product): string
  174.     {
  175.         $rowString "";
  176.         $bases $product->getBase();
  177.         if (count($bases) > 0) {
  178.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Base") . ":</td> ";
  179.             $rowString .= "<td class=\"attributevalue\">";
  180.             $resultString "";
  181.             foreach ($bases as $base) {
  182.                 $relProduct $base->getElement();
  183.                 $resultString .= "<br/>";
  184.                 if (!empty($relProduct->getBsncode())) {
  185.                     $resultString .= $relProduct->getBsncode() . " - ";
  186.                 }
  187.                 $resultString .= $relProduct->getName();
  188.             }
  189.             $rowString .= trim($resultString"<br/>");
  190.             $rowString .= "</td></tr>";
  191.         }
  192.         return $rowString;
  193.     }
  194.     protected function getCatalizadorRow($product$data): string
  195.     {
  196.         $rowString "";
  197.         if (!empty($data->valuesArray["catalizador"])) {
  198.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans($data->attributes["catalizador"]["title"]) . ":</td> ";
  199.             $rowString .= "<td class=\"attributevalue\">" $this->translator->trans($data->valuesArray["catalizador"]) . "</td></tr>";
  200.         } else {
  201.             $catalizadors $product->getCatalizador();
  202.             if (count($catalizadors) > 0) {
  203.                 $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Catalizador") . ":</td> ";
  204.                 $rowString .= "<td class=\"attributevalue\">";
  205.                 $resultString "";
  206.                 foreach ($catalizadors as $catalizador) {
  207.                     $relProduct $catalizador->getElement();
  208.                     $resultString .= "<br/>";
  209.                     if (!empty($relProduct->getBsncode())) {
  210.                         $resultString .= $relProduct->getBsncode() . " - ";
  211.                     }
  212.                     $resultString .= $relProduct->getName();
  213.                 }
  214.                 $rowString .= trim($resultString"<br/>");
  215.                 $rowString .= "</td></tr>";
  216.             }
  217.         }
  218.         return $rowString;
  219.     }
  220.     protected function getConverterRow($product): string
  221.     {
  222.         $rowString "";
  223.         $converters $product->getConverter();
  224.         if (count($converters) > 0) {
  225.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Converter") . ":</td> ";
  226.             $rowString .= "<td class=\"attributevalue\">";
  227.             $resultString "";
  228.             foreach ($converters as $converter) {
  229.                 $relProduct $converter->getElement();
  230.                 $resultString .= "<br/>";
  231.                 if (!empty($relProduct->getBsncode())) {
  232.                     $resultString .= $relProduct->getBsncode() . " - ";
  233.                 }
  234.                 $resultString .= $relProduct->getName();
  235.             }
  236.             $rowString .= trim($resultString"<br/>");
  237.             $rowString .= "</td></tr>";
  238.         }
  239.         return $rowString;
  240.     }
  241.     protected function getDisvolventeRow($product): string
  242.     {
  243.         $rowString "";
  244.         $disolventes $product->getDisolvente();
  245.         if (count($disolventes) > 0) {
  246.             $rowString .= "<tr><td class=\"attributekey\">";
  247.             if ($this->translator->getLocale() == 'es') {
  248.                 $rowString .= "Disolvente:";
  249.             } elseif ($this->translator->getLocale() == 'en') {
  250.                 $rowString .= "Solvent:";
  251.             } else {
  252.                 $rowString .= "Solvent:";
  253.             }
  254.             $rowString .= ":</td> ";
  255.             $rowString .= "<td class=\"attributevalue\">";
  256.             $resultString "";
  257.             foreach ($disolventes as $disolvente) {
  258.                 $relProduct $disolvente->getElement();
  259.                 $resultString .= "<br/>";
  260.                 if (!empty($relProduct->getBsncode())) {
  261.                     $resultString .= $relProduct->getBsncode() . " - ";
  262.                 }
  263.                 $resultString .= $relProduct->getName();
  264.             }
  265.             $rowString .= trim($resultString"<br/>");
  266.             $rowString .= "</td></tr>";
  267.         }
  268.         return $rowString;
  269.     }
  270.     protected function getKitRow($product): string
  271.     {
  272.         $rowString "";
  273.         $kits $product->getKit();
  274.         if (count($kits) > 0) {
  275.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Kit") . ":</td> ";
  276.             $rowString .= "<td class=\"attributevalue\">";
  277.             $resultString "";
  278.             foreach ($kits as $kit) {
  279.                 $relProduct $kit->getElement();
  280.                 $resultString .= "<br/>";
  281.                 if (!empty($relProduct->getBsncode())) {
  282.                     $resultString .= $relProduct->getBsncode() . " - ";
  283.                 }
  284.                 $resultString .= $relProduct->getName();
  285.             }
  286.             $rowString .= trim($resultString"<br/>");
  287.             $rowString .= "</td></tr>";
  288.         }
  289.         return $rowString;
  290.     }
  291.     public function getCleanerRow($product): string
  292.     {
  293.         $rowString "";
  294.         $cleaners $product->getCleaner();
  295.         if (count($cleaners) > 0) {
  296.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Cleaner") . ":</td> ";
  297.             $rowString .= "<td class=\"attributevalue\">";
  298.             $resultString "";
  299.             foreach ($cleaners as $cleaner) {
  300.                 $relProduct $cleaner->getElement();
  301.                 $resultString .= "<br/>";
  302.                 if (!empty($relProduct->getBsncode())) {
  303.                     $resultString .= $relProduct->getBsncode() . " - ";
  304.                 }
  305.                 $resultString .= $relProduct->getName();
  306.             }
  307.             $rowString .= trim($resultString"<br/>");
  308.             $rowString .= "</td></tr>";
  309.         }
  310.         return $rowString;
  311.     }
  312.     protected function getReducerRow($product): string
  313.     {
  314.         $rowString "";
  315.         $reducers $product->getReducer();
  316.         if (count($reducers) > 0) {
  317.             $rowString .= "<tr><td class=\"attributekey\">" $this->translator->trans("Reducer") . ":</td> ";
  318.             $rowString .= "<td class=\"attributevalue\">";
  319.             $resultString "";
  320.             foreach ($reducers as $reducer) {
  321.                 $relProduct $reducer->getElement();
  322.                 $resultString .= "<br/>";
  323.                 if (!empty($relProduct->getBsncode())) {
  324.                     $resultString .= $relProduct->getBsncode() . " - ";
  325.                 }
  326.                 $resultString .= $relProduct->getName();
  327.             }
  328.             $rowString .= trim($resultString"<br/>");
  329.             $rowString .= "</td></tr>";
  330.         }
  331.         return $rowString;
  332.     }
  333.     public function getVariantsAttributeValue($variant$field)
  334.     {
  335.         $data = new \stdClass();
  336.         $value $variant->getAttributeValue($field);
  337.         $textAlign "text-align:center;";
  338.         switch ($field["fieldtype"]) {
  339.             case "inputQuantityValue" :
  340.             case "quantityValue" :
  341.                 $textAlign "text-align:center;";
  342.                 break;
  343.         }
  344.         switch ($field["name"]) {
  345.             case "color" :
  346.                 $colorObj \Pimcore\Model\DataObject\Colores::getById($value);
  347.                 if ($colorObj) {
  348.                     if (method_exists($colorObj"getName")) {
  349.                         $value $colorObj->getName();
  350.                     }
  351.                     if (method_exists($colorObj"getHex")) {
  352.                         $hexObj $colorObj->getHex();
  353.                         if($hexObj != null){
  354.                             if (method_exists($hexObj"getHex")) {
  355.                                 $hexValue $hexObj->getHex();
  356.                                 $classStyle "class=\"fa fa-square\" style=\"color:" $hexValue "\"";
  357.                                 if (empty($hexValue) || ($hexValue == "#ffffff")) {
  358.                                     $classStyle "class=\"fa fa-square-o\"";
  359.                                 }
  360.                             }
  361.                         }
  362.                     }
  363.                 }
  364.                 break;
  365.         }
  366.         if (is_array($value)) {
  367.             $value implode(" | "$value);
  368.         }
  369.         $data->textAlign $textAlign;
  370.         $data->value $value;
  371.         return $data;
  372.     }
  373.     public function getVariantsPrices($variant$iva)
  374.     {
  375.         $data = new \stdClass();
  376.         $precioReal $variant->getPriceNumeric();
  377.         $precioIVAReal 0;
  378.         $precioIVA "";
  379.         $precioTotal "";
  380.         if (!empty($precioReal)) {
  381.             $precioIVAReal $variant->getPriceTax($iva);
  382.             $precioIVA number_format($precioIVAReal2","".");
  383.             $precioTotal number_format(((float) $precioReal) + ((float) $precioIVAReal), 2","".");
  384.             $precioReal number_format($precioReal2","".");
  385.         }
  386.         $data->precioIVA $precioIVA;
  387.         $data->precioTotal $precioTotal;
  388.         $data->precioReal $precioReal;
  389.         return $data;
  390.     }
  391.     public function getRelatedProducts($product)
  392.     {
  393.         $relatedProducts = [];
  394.         $attributes $product->getAllAttributes($product);
  395.         $relatedProducts = array();
  396.         if (key_exists("relationproducts"$attributes)) {
  397.             $relationproducts $product->getAttributeValue($attributes["relationproducts"]);
  398.             if (count($relationproducts) > 0) {
  399.                 foreach ($relationproducts as $relationproduct) {
  400.                     $relatedProducts[] = $relationproduct->getElement();
  401.                 }
  402.             }
  403.         }
  404.         if (method_exists($product"getGeneralrelatedproducts")) {
  405.             $relationproducts $product->getGeneralrelatedproducts();
  406.             if (count($relationproducts) > 0) {
  407.                 foreach ($relationproducts as $relationproduct) {
  408.                     $relatedProducts[] = $relationproduct->getElement();
  409.                 }
  410.             }
  411.         }
  412.         return $relatedProducts;
  413.     }
  414.     public function generateLink($product, array $params, array $extra): string
  415.     {
  416.         try {
  417.             /** @var DefaultProduct $linkProduct */
  418.             $linkProduct $product->getLinkProduct();
  419.             $link $linkProduct->getDetailUrl($params);
  420.             $authenticated = ($extra['authenticated']) ?? false;
  421.             $isProductlist = ($extra['isProductlist']) ?? false;
  422.             $isRelatedProduct = ($extra['isRelatedProduct']) ?? false;
  423.             $baseUrl explode('?'$_SERVER['REQUEST_URI']);
  424.             $unique "";
  425.             if (count($baseUrl) > 1) {
  426.                 $unique "?" $baseUrl[1];
  427.             }
  428.             if ($isProductlist && !$isRelatedProduct) {
  429.                 $urlArray explode("/"$link);
  430.                 $link $baseUrl[0] . "/" $urlArray[count($urlArray) - 1];
  431.                 if ($authenticated) {
  432.                     $link $link $unique;
  433.                 }
  434.             }
  435.         }catch (\Exception $exception){
  436.             $link '';
  437.         }
  438.         return $link;
  439.     }
  440.     //Get lower price of the product variants
  441.     public function getDefaultPriceWithDiscount($product,$userIdSage)
  442.     {
  443.         $mountOfVariants count($product->getAllActiveVariants());
  444.         //Does it have variants?
  445.         $haveVariants $mountOfVariants;
  446.         $optimized = ($haveVariants 35);
  447.         if($mountOfVariants){
  448.             // START LOWER PRICE WITH DISCOUNT V2
  449.             $prices =  $product->getVariantPriceAndIdList($optimized);
  450.             //replace this format 54,24 to 54.24
  451.             $prices str_replace(',','.',$prices);
  452.             //convert string prices to float prices
  453.             $prices array_map('floatval'$prices);
  454.             $min_value min($prices);
  455.             $keys array_keys($prices$min_value);
  456.             //Calculate the discount but for one of the cheapest products found in its variants
  457.             $product DefaultProduct::getById($keys[0]);
  458.             //END LOWER PRICE WITH DISCOUNT V2
  459.         }
  460.         return $this->shopService->getProductDiscount($product->getBsncode(), $userIdSage);
  461.     }
  462.     //Get lower price of the product variants
  463.     public function getPriceWithDiscount($product,$userIdSage): array
  464.     {
  465.         return $this->shopService->getProductDiscount($product->getBsncode(), $userIdSage);
  466.     }
  467.     public function getDefaultPrice($product)
  468.     {
  469.         $mountOfVariants count($product->getAllActiveVariants());
  470.         $optimized = ($mountOfVariants 35);
  471.         $prices $product->getVariantPriceList($optimized);
  472.         if (empty($prices)) {
  473.             return 0;
  474.         }
  475.         $floatPrices array_map(function($price) {
  476.             return floatval(str_replace(',''.'$price));
  477.         }, $prices);
  478.         return min($floatPrices);
  479.     }
  480. }