当前位置: 首页 > 工具软件 > EC-CUBE > 使用案例 >

如何使用 EC-CUBE4 取消指定县的免费送货条件(单个地区)

梅安平
2023-12-01

1,如果您只在冲绳县取消免费送货条件

在Customize/Service/PurchaseFlow/Processor目录下设置如下DeliveryFeeFreeByShippingPreprocessor.php。

<?php


namespace Customize\Service\PurchaseFlow\Processor;

use Eccube\Annotation\ShoppingFlow;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\ItemHolderInterface;
use Eccube\Entity\Order;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\Master\PrefRepository;
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
use Eccube\Service\PurchaseFlow\Processor\DeliveryFeePreprocessor;
use Eccube\Service\PurchaseFlow\PurchaseContext;

/**
 * 适用免运费条件.
 * 按地区省份判断.
 *
 * @ShoppingFlow()
 */
class DeliveryFeeFreeByShippingPreprocessor implements ItemHolderPreprocessor
{
    /**
     * @var BaseInfo
     */
    protected $BaseInfo;

    /**
     * @var PrefRepository
     */
    private $prefRepository;

    public function __construct(
        BaseInfoRepository $baseInfoRepository,
        PrefRepository $prefRepository
    ) {
        $this->BaseInfo = $baseInfoRepository->get();
        $this->prefRepository = $prefRepository;
    }

    /**
     * @param ItemHolderInterface $itemHolder
     * @param PurchaseContext $context
     */
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
    {
        if (!($this->BaseInfo->getDeliveryFreeAmount() || $this->BaseInfo->getDeliveryFreeQuantity())) {
            return;
        }

        // Order按收货人地址判断.
        if ($itemHolder instanceof Order) {
            // 指定排除免运费条件的都道府县
            $noFreePref = '沖縄県';

            /** @var Order $Order */
            $Order = $itemHolder;
            foreach ($Order->getShippings() as $Shipping) {
                $isFree = false;
                $total = 0;
                $quantity = 0;
                foreach ($Shipping->getProductOrderItems() as $Item) {
                    $total += $Item->getPriceIncTax() * $Item->getQuantity();
                    $quantity += $Item->getQuantity();
                }
                // 超过了免运费(金额)
                if ($this->BaseInfo->getDeliveryFreeAmount()) {
                    if ($total >= $this->BaseInfo->getDeliveryFreeAmount()) {
                        $isFree = true;
                    }
                }
                // 超过了免运费(个数)
                if ($this->BaseInfo->getDeliveryFreeQuantity()) {
                    if ($quantity >= $this->BaseInfo->getDeliveryFreeQuantity()) {
                        $isFree = true;
                    }
                }
                // 超过免费送货条件时
                if ($isFree) {
                    foreach ($Shipping->getOrderItems() as $Item) {
                        if ($Item->getProcessorName() == DeliveryFeePreprocessor::class) {
                            // 与不适用免费运费条件的都道府县相匹配的话,运费明细的数量为1
                            if ($Shipping->getPref() == $this->prefRepository->findOneBy(['name' => $noFreePref])) {
                                $Item->setQuantity(1);
                                // 如果您想使用在都道府县另行设定的运费和其他的运费的话,请添加此项
                                // $Item->setPrice(500);
                            }
                        }
                    }
                }
            }
        }
    }
}

 类似资料: