EC-CUBE4で合計金額3000円未満の場合は送料に300円加算するプラグインを作る方法です。
まずは以下のコマンドでプラグインの雛形を作ります。
プラグインコードはなんでも良いのですが、今回は「DeliveryFee」にしてください。
bin/console eccube:plugin:generate
送料計算するプロセッサーを用意
自動生成されたプラグイン一式内にService/PurchaseFlowディレクトリを作り、以下のDeliveryFeePreprocessor.phpファイルを設置して下さい。以上で完成です。
<?php
namespace Plugin\DeliveryFee\Service\PurchaseFlow;
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Entity\ItemHolderInterface;
use Eccube\Service\PurchaseFlow\Processor\DeliveryFeePreprocessor as BaseDeliveryFeePreprocessor;
use Eccube\Annotation\ShoppingFlow;
use Eccube\Annotation\OrderFlow;
use Eccube\Entity\Order;
/**
* 合計金額3000円未満の場合、送料に300円加算
*
* @author Akira Kurozumi <info@a-zumi.net>
*
* ご注文手続きページで実行されるようアノテーションを設定
* @ShoppingFlow
*/
class DeliveryFeePreprocessor implements ItemHolderPreprocessor {
public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
{
if (!$itemHolder instanceof Order) {
return;
}
// お届け先ごとに判定
foreach ($itemHolder->getShippings() as $Shipping) {
// 送料無料の受注明細かどうか確認
foreach ($Shipping->getOrderItems() as $Item) {
// 送料明細を探す
if ($Item->getProcessorName() == BaseDeliveryFeePreprocessor::class) {
// 送料明細の数量が0の場合は送料無料
if ($Item->getQuantity() == 0) {
// 送料無料の場合は次の受注明細へ
continue 2;
}
}
}
// 合計金額計算
$total = 0;
foreach ($Shipping->getProductOrderItems() as $Item) {
$total += $Item->getPriceIncTax() * $Item->getQuantity();
}
// 合計金額が3000円未満の場合、送料に300円加算
if ($total < 3000) {
foreach ($Shipping->getOrderItems() as $Item) {
if ($Item->getProcessorName() == BaseDeliveryFeePreprocessor::class) {
$Item->setPrice($Item->getPrice() + 300);
}
}
}
}
}
}
アノテーションの設定を忘れずに
購入処理(PurchaseFlow)に独自の処理を追加するためには@ShoppingFlowアノテーションを追加する必要があります。
アノテーションの設定を忘れると動作しません。
送料無料条件の考慮
EC-CUBE4本体でも送料計算をしていますのでそちらの考慮も必要です。
EC-CUBE4本体では送料無料条件のチェック機能があり、送料無料条件にマッチした場合送料明細の数量が0に設定されます。
ですので、送料明細の数量が0の場合はプラグインの送料条件を適用しないようにしています。
プラグインのインストールと有効化
プラグインをインストールと有効化を行うと上記の送料条件が動作します。
このプラグインのファイル一式はこちら。

素晴らしい。これやりたかったんです。開発者ではないのでちょっとハードル高いですが、実装すべく集中します。