LoginSignup
4
6

More than 3 years have passed since last update.

EC-CUBE4 で割引明細を追加するカスタマイズの例

Last updated at Posted at 2020-05-21

EC-CUBE4 で割引明細を追加するカスタマイズの例

以下のPHPファイルを作成する。

app/Customize/Service/PurchaseFlow/Processor/TestDiscountProcessor.php

app/Customize/Service/PurchaseFlow/Processor/TestDiscountProcessor.php
<?php


namespace Customize\Service\PurchaseFlow\Processor;


use Doctrine\ORM\EntityManagerInterface;
use Eccube\Annotation\OrderFlow;
use Eccube\Annotation\ShoppingFlow;
use Eccube\Entity\ItemHolderInterface;
use Eccube\Entity\Master\OrderItemType;
use Eccube\Entity\Master\TaxDisplayType;
use Eccube\Entity\Master\TaxType;
use Eccube\Entity\OrderItem;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\DiscountProcessor;

/**
 * 値引きを追加する.
 *
 * ここでフローを指定
 * @ShoppingFlow
 * @OrderFlow
 */
class TestDiscountProcessor implements DiscountProcessor
{
    /**
     * @var EntityManagerInterface
     */
    protected $entityManager;

    /**
     * TestDiscountProcessor constructor.
     *
     * @param EntityManagerInterface $entityManager
     */
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function removeDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context)
    {
        foreach ($itemHolder->getItems() as $item) {
            if ($item->getProcessorName() == TestDiscountProcessor::class) {
                $itemHolder->removeOrderItem($item);
                $this->entityManager->remove($item);
            }
        }
    }

    public function addDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context)
    {
        /* 必要であればここで適応条件の制御をする
         * if...
         */

        // 課税/不課税や税込/税抜はここで作成
        $DiscountType = $this->entityManager->find(OrderItemType::class, OrderItemType::DISCOUNT);
        $TaxInclude = $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
        $Taxation = $this->entityManager->find(TaxType::class, TaxType::TAXATION);

        $OrderItem = new OrderItem();
        $OrderItem->setProductName($DiscountType->getName())
            ->setPrice(-100)
            ->setQuantity(1)
            ->setTax(-10)
            ->setTaxRate(10)
            ->setRoundingType(null)
            ->setOrderItemType($DiscountType)
            ->setTaxDisplayType($TaxInclude)
            ->setTaxType($Taxation)
            ->setOrder($itemHolder)
            ->setProcessorName(TestDiscountProcessor::class);
        $itemHolder->addItem($OrderItem);
    }
}
4
6
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
4
6