app/Plugin/PiaSimilarProducts/EventSubscriber/ProductEventSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of PiaSimilarProducts
  4.  *
  5.  * Copyright(c) Pia Staff. All Rights Reserved.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\PiaSimilarProducts\EventSubscriber;
  11. use Eccube\Event\TemplateEvent;
  12. use Plugin\PiaSimilarProducts\Service\SimilarProductsService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var SimilarProductsService
  18.      */
  19.     protected $similarProductsService;
  20.     public function __construct(SimilarProductsService $similarProductsService)
  21.     {
  22.         $this->similarProductsService $similarProductsService;
  23.     }
  24.     /**
  25.      * @return array
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             'Product/detail.twig' => 'onRenderProductDetail',
  31.         ];
  32.     }
  33.     /**
  34.      * 商品詳細ページに類似商品を追加
  35.      *
  36.      * @param TemplateEvent $event
  37.      */
  38.     public function onRenderProductDetail(TemplateEvent $event)
  39.     {
  40. //        $parameters = $event->getParameters();
  41. //
  42. //        if (!isset($parameters['Product'])) {
  43. //            return;
  44. //        }
  45. //
  46. //        $product = $parameters['Product'];
  47. //
  48. //        // 類似商品表示対象かチェック
  49. //        if (!$this->similarProductsService->shouldShowSimilarProducts($product)) {
  50. //            return;
  51. //        }
  52. //
  53. //        // 類似商品を取得
  54. //        $similarProducts = $this->similarProductsService->getSimilarProducts($product);
  55. //
  56. //        if (empty($similarProducts)) {
  57. //            return;
  58. //        }
  59. //
  60. //        // テンプレートに類似商品を追加
  61. //        $parameters['similar_products'] = $similarProducts;
  62. //        $event->setParameters($parameters);
  63. //
  64. //        // 類似商品セクションをテンプレートに挿入
  65. //        $source = $event->getSource();
  66. //
  67. //        // 類似商品表示用HTML
  68. //        $similarProductsHtml = '
  69. //        {% if similar_products is defined and similar_products|length > 0 %}
  70. //        <div class="ec-productRole__similarProducts" style="margin-top: 2rem; padding: 1.5rem; background-color: #f8f9fa; border-radius: 8px;">
  71. //            <h3 style="margin-bottom: 1.5rem; font-size: 1.5rem; color: #333;">類似商品</h3>
  72. //            <div class="ec-shelfGrid" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem;">
  73. //                {% for similar_product in similar_products %}
  74. //                <div class="ec-shelfGrid__item">
  75. //                    <div class="ec-shelfGridItem" style="border: 1px solid #e9ecef; border-radius: 8px; padding: 1rem; background-color: white; transition: box-shadow 0.3s ease;">
  76. //                        <div class="ec-shelfGridItem__image" style="text-align: center; margin-bottom: 1rem;">
  77. //                            <a href="{{ url(\'product_detail\', {\'id\': similar_product.id}) }}">
  78. //                                {% if similar_product.MainListImage %}
  79. //                                    <img src="{{ asset(similar_product.MainListImage, \'save_image\') }}" alt="{{ similar_product.name }}" style="max-width: 150px; height: 150px; object-fit: cover; border-radius: 4px;" loading="lazy">
  80. //                                {% else %}
  81. //                                    <img src="{{ asset(\'\' | no_image_product, \'save_image\') }}" alt="{{ similar_product.name }}" style="max-width: 150px; height: 150px; object-fit: cover; border-radius: 4px;" loading="lazy">
  82. //                                {% endif %}
  83. //                            </a>
  84. //                        </div>
  85. //                        <div class="ec-shelfGridItem__itemName" style="margin-bottom: 0.5rem; font-size: 0.9rem; line-height: 1.4;">
  86. //                            <a href="{{ url(\'product_detail\', {\'id\': similar_product.id}) }}" style="color: #333; text-decoration: none;">{{ similar_product.name }}</a>
  87. //                        </div>
  88. //                        <div class="ec-shelfGridItem__price" style="text-align: center;">
  89. //                            {% if similar_product.hasProductClass %}
  90. //                                {% if similar_product.getPrice02IncTaxMin == similar_product.getPrice02IncTaxMax %}
  91. //                                    <span class="ec-price__price" style="font-weight: bold; color: #dc3545; font-size: 1.1rem;">{{ similar_product.getPrice02IncTaxMin|price }}</span>
  92. //                                {% else %}
  93. //                                    <span class="ec-price__price" style="font-weight: bold; color: #dc3545; font-size: 1rem;">{{ similar_product.getPrice02IncTaxMin|price }} ~ {{ similar_product.getPrice02IncTaxMax|price }}</span>
  94. //                                {% endif %}
  95. //                            {% else %}
  96. //                                <span class="ec-price__price" style="font-weight: bold; color: #dc3545; font-size: 1.1rem;">{{ similar_product.getPrice02IncTaxMin|price }}</span>
  97. //                            {% endif %}
  98. //                            <br><span class="ec-price__tax" style="font-size: 0.8rem; color: #6c757d;">税込</span>
  99. //                        </div>
  100. //                    </div>
  101. //                </div>
  102. //                {% endfor %}
  103. //            </div>
  104. //        </div>
  105. //        {% endif %}';
  106. //
  107. //        // レビュープラグインと同じ位置に挿入(関連動画の前)
  108. //        $search = '/<h3>関連動画<\/h3>/';
  109. //        if (preg_match($search, $source, $result)) {
  110. //            $searchText = $result[0];
  111. //            $replace = $similarProductsHtml . $searchText;
  112. //            $source = str_replace($searchText, $replace, $source);
  113. //            $event->setSource($source);
  114. //        }
  115.     }
  116. }