src/Controller/ProductController.php line 148

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Helpers\WoocommerceHelper;
  4. use App\Images\ImageRenderer;
  5. use App\Service\SchemaService;
  6. use Metabolism\WordpressBundle\Entity\Post;
  7. use Metabolism\WordpressBundle\Entity\PostCollection;
  8. use Metabolism\WordpressBundle\Entity\Term;
  9. use Metabolism\WordpressBundle\Service\PaginationService;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class ProductController extends AbstractController
  14. {
  15.     #[Route('/product/{slug}'requirements: ['slug' => '.+'])]
  16.     public function productAction(Post $postSchemaService $schemaServiceImageRenderer $imageRendererWoocommerceHelper $woocommerceHelper)
  17.     {
  18.         $product wc_get_product($post->ID);
  19.         $producto['id'] = $product->get_id();
  20.         $producto['link'] = $product->get_permalink();
  21.         $producto['slug'] = $product->get_slug();
  22.         $producto['name'] = $product->get_name();
  23.         $producto['sku'] = $product->get_sku();
  24.         $producto['description'] = apply_filters('the_content'nl2br($product->get_description()));
  25.         $producto['short_description'] = strip_tags($product->get_short_description(), '<p><strong>');
  26.         $producto['is_in_stock'] = $product->is_in_stock() && $product->get_price();
  27.         $tax_rates $product->get_tax_class() ? \WC_Tax::get_rates($product->get_tax_class()) : \WC_Tax::get_rates('');
  28.         $taxes 1;
  29.         if (!empty($tax_rates)) {
  30.             $tax_rate reset($tax_rates);
  31.             $taxes += $tax_rate['rate'] / 100;
  32.         }
  33.         $producto['taxes'] = !empty($tax_rates) ? $taxes null;
  34.         $producto['price'] = $product->get_price() ? $product->get_price() * $taxes null;
  35.         $producto['regular_price'] = $product->is_type('variable') ? floatval($product->get_variation_regular_price('min'true) + 0) * $taxes floatval($product->get_regular_price()) * $taxes;
  36.         $producto['sale_price'] = $product->is_type('variable') ? floatval($product->get_variation_sale_price('min'true) + 0) * $taxes floatval($product->get_sale_price()) * $taxes;
  37.         if ($producto['regular_price'] == $producto['sale_price']) {
  38.             $producto['sale_price'] = $product->is_type('variable') ? floatval($product->get_variation_price('min'true) + 0) * $taxes floatval($product->get_sale_price()) * $taxes;
  39.         } else if ($producto['sale_price']) {
  40.             $producto['discount'] = round(100 - ($producto['sale_price'] * 100) / $producto['regular_price'], 2PHP_ROUND_HALF_UP);
  41.         }
  42.         $producto['width'] = get_post_meta($product->get_id(), '_width'true);
  43.         $producto['height'] = get_post_meta($product->get_id(), '_height'true);
  44.         $producto['length'] = get_post_meta($product->get_id(), '_length'true);
  45.         $producto['attributes'] = WoocommerceHelper::getProductAttributes($product);
  46.         $image_id $product->get_image_id();
  47.         $producto['gallery'] = [];
  48.         if ($image_id) {
  49.             $producto['image'] = $imageRenderer->getAttributesWordpress($image_id'product''gallery')['src'];
  50.             $imageAttributes $imageRenderer->getAttributesWordpress($image_id'product''gallery');
  51.             $imageAttributes['thumbnail'] = $imageRenderer->getAttributesWordpress($image_id'product''thumbnail');
  52.             $producto['gallery'][] = $imageAttributes;
  53.         }
  54.         foreach ($product->get_gallery_image_ids() as $key => $id) {
  55.             $imageAttributes $imageRenderer->getAttributesWordpress($id'product''gallery');
  56.             $imageAttributes['thumbnail'] = $imageRenderer->getAttributesWordpress($id'product''thumbnail');
  57.             $producto['gallery'][] = $imageAttributes;
  58.         }
  59.         $attributes = [];
  60.         foreach ($_GET as $key => $value) {
  61.             if (strpos($key'attribute_pa_') === 0) {
  62.                 $attributes[$key] = $value;
  63.             }
  64.         }
  65.         if ($product->is_type('variable')) {
  66.             $producto['available_variations'] = $product->get_available_variations();
  67.             $producto['variation_attributes'] = WoocommerceHelper::getVariationAttributes($product);
  68.             $producto['default_variation'] = WoocommerceHelper::getDefaultVariation($product);
  69.             if ($attributes) {
  70.                 foreach ($producto['default_variation']['attributes'] as $key => $value) {
  71.                     if (!isset($attributes[$key])) {
  72.                         $attributes[$key] = $value;
  73.                     }
  74.                 }
  75.                 $producto['default_variation'] = $producto['available_variations'][array_search(WoocommerceHelper::findProductVariationId($product$attributes), array_column($producto['available_variations'], 'variation_id'))];
  76.             } else if (isset($_GET['v']) && array_search($_GET['v'], array_column($producto['available_variations'], 'variation_id')) !== false) {
  77.                 $producto['default_variation'] = $producto['available_variations'][array_search($_GET['v'], array_column($producto['available_variations'], 'variation_id'))];
  78.             }
  79.             // Poner un valor a los atributos de la variación por defecto que este vacía
  80.             foreach ($producto['default_variation']['attributes'] as $key => $value) {
  81.                 if (empty($value)) {
  82.                     if (isset($attributes[$key]) && $attributes[$key]) {
  83.                         $producto['default_variation']['attributes'][$key] = $attributes[$key];
  84.                     } else {
  85.                         $attribute str_replace('attribute_'''$key);
  86.                         $producto['default_variation']['attributes'][$key] = $producto['variation_attributes'][$attribute][0];
  87.                     }
  88.                 }
  89.             }
  90.             $producto['galleryVariations'] = $woocommerceHelper->getProductAttributesGallery($product);
  91.         }
  92.         $producto['upsell_ids'] = $product->get_upsell_ids();
  93.         foreach ($product->get_category_ids() as $key => $categoria) {
  94.             $term get_term($categoria'product_cat');
  95.             $producto['categories'][$key]['id'] = $categoria;
  96.             $producto['categories'][$key]['parent'] = $term->parent;
  97.             $producto['categories'][$key]['name'] = $term->name;
  98.             $producto['categories'][$key]['slug'] = $term->slug;
  99.             $producto['categories'][$key]['link'] = get_term_link($term);
  100.         }
  101.         $producto['accordion'] = [];
  102.         if (!empty($producto['description'])) {
  103.             $producto['accordion'][] = [
  104.                 'titulo' => 'Descripción',
  105.                 'descripcion' => $producto['description']
  106.             ];
  107.         }
  108.         if ($producto['sku'] || $producto['attributes']) {
  109.             $specifications = [];
  110.             if ($producto['sku']) {
  111.                 $specifications[] = [
  112.                     'name' => 'SKU',
  113.                     'value' => $producto['sku']
  114.                 ];
  115.             }
  116.             foreach ($producto['attributes'] as $key => $attributes) {
  117.                 if ($key == 'pa_tiempo-de-envio') {
  118.                     continue;
  119.                 }
  120.                 $specifications[] = [
  121.                     'name' => wc_attribute_label($key),
  122.                     'value' => implode(', 'array_map(function ($attribute) {
  123.                         return $attribute['name'];
  124.                     }, $attributes))
  125.                 ];
  126.             }
  127.             $producto['accordion'][] = [
  128.                 'titulo' => 'Especificaciones',
  129.                 'descripcion' => $this->render('components/product-specifications.html.twig', [
  130.                     'specifications' => $specifications
  131.                 ])->getContent()
  132.             ];
  133.         }
  134.         return $this->render('product.html.twig', [
  135.             'product' => $producto,
  136.             'cart_url' => wc_get_cart_url(),
  137.             'json_schema' => $schemaService->injectSchemaProduct($producto)->toScript()
  138.         ]);
  139.     }
  140.     public function productCatAction(Term $termPostCollection $posts)
  141.     {
  142.         $facets = ['precio''product_cat'];
  143.         $shortcodes = [];
  144.         foreach ($facets as $facet) {
  145.             $shortcodes[] = do_shortcode('[facetwp facet="' $facet '"]');
  146.         }
  147.         $filtros = [
  148.             [
  149.                 'filter_slug' => 'product_cat',
  150.                 'filter_value' => $term->slug
  151.             ]
  152.         ];
  153.         FWP()->request->url_vars += array_reduce($filtros, function ($carry$item) {
  154.             $carry[$item['filter_slug']] = array_merge($carry[$item['filter_slug']] ?? [], explode('|'$item['filter_value']));
  155.             return $carry;
  156.         }, []);
  157.         return $this->render('archive-product.html.twig', [
  158.             'term' => $term,
  159.             'tipo_productos' => $this->getFilterTipoProductos(),
  160.             'filtros' => $filtros,
  161.             'facets_shortcodes' => $shortcodes
  162.         ]);
  163.     }
  164.     public function productTagAction(Term $termPostCollection $posts)
  165.     {
  166.         $facets = ['precio''product_cat'];
  167.         $shortcodes = [];
  168.         foreach ($facets as $facet) {
  169.             $shortcodes[] = do_shortcode('[facetwp facet="' $facet '"]');
  170.         }
  171.         return $this->render('archive-product.html.twig', [
  172.             'term' => $term,
  173.             'tipo_productos' => $this->getFilterTipoProductos(),
  174.             'facets_shortcodes' => $shortcodes
  175.         ]);
  176.     }
  177.     public function productArchiveAction(PostCollection $posts)
  178.     {
  179.         $post = new Post(wc_get_page_id('shop'));
  180.         $facets = ['precio''product_cat'];
  181.         $shortcodes = [];
  182.         foreach ($facets as $facet) {
  183.             $shortcodes[] = do_shortcode('[facetwp facet="' $facet '"]');
  184.         }
  185.         return $this->render('archive-product.html.twig', [
  186.             'post' => $post,
  187.             'tipo_productos' => $this->getFilterTipoProductos(),
  188.             'facets_shortcodes' => $shortcodes
  189.         ]);
  190.     }
  191.     private function getFilterTipoProductos()
  192.     {
  193.         $args = [
  194.             'post_type' => 'wp_block',
  195.             'title' => 'FILTRO CATEGORIAS PRODUCTOS',
  196.             'posts_per_page' => 1
  197.         ];
  198.         $query = new \WP_Query($args);
  199.         if ($query->have_posts()) {
  200.             $block_post $query->posts[0];
  201.             return do_blocks($block_post->post_content);
  202.         }
  203.     }
  204. }