Processing to make free shipping over XX yen with Eccube
Sep 9, 2020
PHP
eccube
eccube4
`app/Customize/Service/PurchaseFlow/Processor/CustomDeliveryFeePreprocessor.php` というファイルを下記のように作成します。
<?php
namespace Customize\Service\PurchaseFlow\Processor;
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
use Eccube\Entity\ItemHolderInterface;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Annotation\ShoppingFlow;
/**
* Description of CustomDeliveryFeePreprocessor
*
* @ShoppingFlow
*/
class CustomDeliveryFeePreprocessor implements ItemHolderPreprocessor {
/** @var BaseInfo */
protected $BaseInfo;
/**
* DeliveryFeePreprocessor constructor.
*
* @param BaseInfoRepository $baseInfoRepository
*/
public function __construct(
BaseInfoRepository $baseInfoRepository,
)
{
$this->BaseInfo = $baseInfoRepository->get();
}
public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
{
$this->updateDeliveryFeeItem($itemHolder);
}
/**
* @param ItemHolderInterface $itemHolder
*/
private function updateDeliveryFeeItem(ItemHolderInterface $itemHolder)
{
// 10,000円を超える場合は送料無料
$totalPrice = 0.0;
foreach ($Shipping->getOrderItems() as $item) {
if (!$item->isDeliveryFee()) {
$totalPrice = $totalPrice + $item->getTotalPrice();
}
}
if ( $totalPrice >= 10000 ) {
foreach ($Shipping->getOrderItems() as $item) {
if ($item->isDeliveryFee()) {
$item->setPrice(0);
}
}
}
}
}
かんたんですね。