0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EC-CUBE4プラグイン有効時・無効時にマスターデータを書き換える

Posted at

基本環境

EC-CUBE4系

やりたいこと

プラグイン有効時にデータベース(dtb,mtb)のEC-CUBEデフォルトデータをプラグインカスタムデータに書き換える

プラグイン無効時にデータベース(dtb,mtb)のプラグインカスタムデータをEC-CUBEデフォルトデータに戻す

PluginManager.php

app/Plugin/OriginalPlugin/PluginManager.php
<?php
namespace Plugin\OriginalPlugin;

use Eccube\Plugin\AbstractPluginManager;
use Eccube\Entity\[カスタムしたいテーブル関連のEntity];
use Symfony\Component\DependencyInjection\ContainerInterface;


class PluginManager extends AbstractPluginManager

/**
     * プラグイン有効化時に走る
     * @param array{code:string, name:string, version:string, source:int} $meta
     * @param ContainerInterface $container
     */
    public function enable(array $meta, ContainerInterface $container)
    {
        $this->deleteDefault[カスタムしたいテーブル関連のEntity]($container);
        $this->createCustom[カスタムしたいテーブル関連のEntity]($container);
    }

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // デフォルトデータを削除する
    
    private function deleteDefault[カスタムしたいテーブル関連のEntity](ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 1);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(P[カスタムしたいテーブル関連のEntity]::class, 2);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 3);
        $em->remove($Config);
        $em->flush($Config);
    }
        

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // カスタムデータを書き込む
    
    private function createCustom[カスタムしたいテーブル関連のEntity](ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 1);
        
        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(1);
        $Config->setName('[カスタムデータ]');
        $Config->setSortNo('[カスタムデータ]');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(2);
        $Config->setName('[カスタムデータ]');
        $Config->setSortNo('[カスタムデータ]');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(3);
        $Config->setName('[カスタムデータ]');
        $Config->setSortNo('[カスタムデータ]');
       
        $em->persist($Config);
        $em->flush($Config);
    }

    /**
     * プラグイン無効化時に走る
     * @param array{code:string, name:string, version:string, source:int} $meta
     * @param ContainerInterface $container
     */
    public function disable(array $meta, ContainerInterface $container)
    {
        $this->deleteCustom[カスタムしたいテーブル関連のEntity]($container);
        $this->createDefault[カスタムしたいテーブル関連のEntity]$container);
    }

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // カスタムデータを削除する
    
    private function deleteCustom[カスタムしたいテーブル関連のEntity](ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 1);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 2);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 3);
        $em->remove($Config);
        $em->flush($Config);
    }
        

    /**
     * 設定情報を入れる
    * @param ContainerInterface $container
    */
    
    // デフォルトデータを書き込む
    
    private function createDefault[カスタムしたいテーブル関連のEntity](ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find([カスタムしたいテーブル関連のEntity]::class, 1);
        
        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(1);
        $Config->setName('[デフォルトデータ]');
        $Config->setSortNo('[デフォルトデータ]');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(2);
        $Config->setName('[デフォルトデータ]');
        $Config->setSortNo('[デフォルトデータ]');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new [カスタムしたいテーブル関連のEntity]();
        $Config->setId(3);
        $Config->setName('[デフォルトデータ]');
        $Config->setSortNo('[デフォルトデータ]');
    
        $em->persist($Config);
        $em->flush($Config);
    }
}

例)フロント カテゴリーページの「価格の低い順」ドロップダウンリストを英語表記にする

デフォルトの表記

スクリーンショット 2025-04-17 16.04.50.png

カスタム後の表記

スクリーンショット 2025-04-17 16.04.09.png

データベースの「mtb_product_list_order_by」nameカラムを書き換える

例)mtb_product_list_order_by をカスタムする

app/Plugin/OriginalPlugin/PluginManager.php
<?php
namespace Plugin\OriginalPlugin;

use Eccube\Plugin\AbstractPluginManager;
use Eccube\Entity\Master\ProductListOrderBy; //カスタムしたいテーブル関連のEntity
use Symfony\Component\DependencyInjection\ContainerInterface;


class PluginManager extends AbstractPluginManager

/**
     * プラグイン有効化時に走る
     * @param array{code:string, name:string, version:string, source:int} $meta
     * @param ContainerInterface $container
     */
    public function enable(array $meta, ContainerInterface $container)
    {
        $this->deleteDefaultProductListOrderBy($container);
        $this->createCustomProductListOrderBy($container);
    }

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // デフォルトデータを削除する
    
    private function deleteDefaultProductListOrderBy(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 1);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 2);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 3);
        $em->remove($Config);
        $em->flush($Config);
    }
        

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // カスタムデータを書き込む
    
    private function createCustomProductListOrderBy(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 1);
        
        $Config = new ProductListOrderBy();
        $Config->setId(1);
        $Config->setName('Lowest price first');
        $Config->setSortNo('0');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new ProductListOrderBy();
        $Config->setId(2);
        $Config->setName('Newest');
        $Config->setSortNo('2');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new ProductListOrderBy();
        $Config->setId(3);
        $Config->setName('Highest price first');
        $Config->setSortNo('1');
       
        $em->persist($Config);
        $em->flush($Config);
    }

    /**
     * プラグイン無効化時に走る
     * @param array{code:string, name:string, version:string, source:int} $meta
     * @param ContainerInterface $container
     */
    public function disable(array $meta, ContainerInterface $container)
    {
        $this->deleteCustomProductListOrderBy($container);
        $this->createDefaultProductListOrderBy($container);
    }

    /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     */
    
    // カスタムデータを削除する
    
    private function deleteCustomProductListOrderBy(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 1);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 2);
        $em->remove($Config);
        $em->flush($Config);

        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 3);
        $em->remove($Config);
        $em->flush($Config);
    }
        

    /**
     * 設定情報を入れる
    * @param ContainerInterface $container
    */
    
    // デフォルトデータを書き込む
    
    private function createDefaultProductListOrderBy(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(ProductListOrderBy::class, 1);
        
        $Config = new ProductListOrderBy();
        $Config->setId(1);
        $Config->setName('価格が低い順');
        $Config->setSortNo('0');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new ProductListOrderBy();
        $Config->setId(2);
        $Config->setName('新着順');
        $Config->setSortNo('2');
        
        $em->persist($Config);
        $em->flush($Config);

        $Config = new ProductListOrderBy();
        $Config->setId(3);
        $Config->setName('価格が高い順');
        $Config->setSortNo('1');
    
        $em->persist($Config);
        $em->flush($Config);
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?