src/Controller/WhatsAppController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\WhatsApp\WhatsAppLinkGenerator;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. #[Route('/wa'name'whatsapp_')]
  8. class WhatsAppController extends AbstractController
  9. {
  10.     private WhatsAppLinkGenerator $linkGenerator;
  11.     public function __construct(WhatsAppLinkGenerator $linkGenerator)
  12.     {
  13.         $this->linkGenerator $linkGenerator;
  14.     }
  15.     #[Route(name'link')]
  16.     public function link()
  17.     {
  18.         $companyName get_bloginfo('name');
  19.         $text = <<<EOD
  20. Hola *{$companyName}*, me gustaría recibir más información sobre: 
  21. EOD;
  22.         return $this->redirect($this->linkGenerator->generateWhatsAppUrl($text));
  23.     }
  24.     #[Route('/product/{slug}'name'product')]
  25.     public function product(string $slug)
  26.     {
  27.         $productID get_page_by_path($slugOBJECT'product')?->ID;
  28.         $product wc_get_product($productID);
  29.         if (!$product) {
  30.             throw new NotFoundHttpException();
  31.         }
  32.         $link $product->get_permalink();
  33.         $name $product->get_name();
  34.         $companyName get_bloginfo('name');
  35.         $text = <<<EOD
  36. Hola *{$companyName}*, me gustaría recibir más información sobre {$name} que he encontrado en $link.
  37. EOD;
  38.         return $this->redirect($this->linkGenerator->generateWhatsAppUrl($text));
  39.     }
  40.     #[Route('/share/product/{slug}'name'share_product')]
  41.     public function shareProduct(string $slug)
  42.     {
  43.         $productID get_page_by_path($slugOBJECT'product')?->ID;
  44.         $product wc_get_product($productID);
  45.         if (!$product) {
  46.             throw new NotFoundHttpException();
  47.         }
  48.         $link $product->get_permalink();
  49.         $name $product->get_name();
  50.         $companyName get_bloginfo('name');
  51.         $sale $product->is_type('variable') ? $product->get_variation_sale_price('min'true) + $product->get_sale_price();
  52.         $price $product->is_type('variable') ? $product->get_variation_regular_price('min'true) + $product->get_regular_price();
  53.         $productPrice $price ' por ' str_replace(',00'''$sale ?? $price) . '€' '';
  54.         $text = <<<EOD
  55. Mira qué he encontrado en *{$companyName}*, ¡un {$name}{$productPrice}$link.
  56. EOD;
  57.         return $this->redirect($this->linkGenerator->generateWhatsAppShare($text));
  58.     }
  59. }