冷凍と冷蔵は同梱できないのでクール便のコードを書き直しです。 | φ(..)メモとして残しておこう…

冷凍と冷蔵は同梱できないのでクール便のコードを書き直しです。

商品A・通常商品
商品B・冷蔵商品
商品C・冷凍商品

の場合。

商品A+商品B = 送料+クール料金

商品A+商品C = 送料+(送料+クール料金)

商品B+商品C = (送料+クール料金)×2

商品A+商品B+商品C = (送料+クール料金)×2

ということで、送料を再計算。



その前に…。

まずはクール便のフラグを

0 = 通常
1 = 冷蔵
2 = 冷凍

で登録できるようにします。


data/Smarty/templates/admin/products/product.tpl
に以下の項目を追加。

<tr>
<th>クール便フラグ
<td>
<span class="attention">

<select name="cool_flg">
<option value="0" <!--{if $arrForm.cool_flg == 0}-->selected<!--{/if}-->>常温便</option>
<option value="1" <!--{if $arrForm.cool_flg == 1}-->selected<!--{/if}-->>冷蔵便</option>
<option value="2" <!--{if $arrForm.cool_flg == 2}-->selected<!--{/if}-->>冷凍便</option>
</select>

</td>
</tr>


12.15 追記
上記コード赤字部分修正しました!
「$arrForm.cool_flg != 0」から「$arrForm.cool_flg == 1」に
「$arrForm.cool_flg != 0」から「$arrForm.cool_flg == 2」に修正です







data/Smarty/templates/admin/products/confirm.tpl
に以下を追加。


tr>
<th>クール便フラグ</th>
<td>
<!--{if $arrForm.cool_flg == 0}-->常温便
<!--{elseif $arrForm.cool_flg == 1}-->冷蔵便
<!--{else}-->冷凍便
<!--{/if}-->
</td>
</tr>


登録周りはこれで完了。
DBいじったりするのは、この記事を参照。


で、ここからが書き直したコード。


data/class/SC_CartSession.phpの「function calculate()」を以下のようにする。



function calculate($productTypeId, &$objCustomer, $use_point = 0,
$deliv_pref = "", $charge = 0, $discount = 0, $deliv_id = 0) {

$objDb = new SC_Helper_DB_Ex();

$total_point = $this->getAllProductsPoint($productTypeId);
$results['tax'] = $this->getAllProductsTax($productTypeId);
$results['subtotal'] = $this->getAllProductsTotal($productTypeId);
$results['deliv_fee'] = 0;
$cool_flg = "";

// 商品ごとの送料を加算
if (OPTION_PRODUCT_DELIV_FEE == 1) {
$cartItems = $this->getCartList($productTypeId);
foreach ($cartItems as $item) {
$results['deliv_fee'] += $item['productsClass']['deliv_fee'] * $item['quantity'];
}
}

// 配送業者の送料を加算
if (OPTION_DELIV_FEE == 1
&& !SC_Utils_Ex::isBlank($deliv_pref)
&& !SC_Utils_Ex::isBlank($deliv_id)) {
$results['deliv_fee'] += $objDb->sfGetDelivFee($deliv_pref, $deliv_id);
}


$cartItems = $this->getCartList($productTypeId);
foreach($cartItems as $val){


//cool_flgが0なら、normal_flgをonに
if ( $normal_flg <> "on" ){
$normal_flg = ( $val["productsClass"]["cool_flg"] == 0 ) ? "on":"";
}


//cool_flgが1なら、冷蔵便フラグをonに
if ( $cool_flg <> "on" ){
$cool_flg = ( $val["productsClass"]["cool_flg"] == 1 ) ? "on":"";
}
//cool_flgが2なら、冷凍便フラグをonに
if ( $freez_flg <> "on" ){
$freez_flg = ( $val["productsClass"]["cool_flg"] == 2 ) ? "on":"";
}
}

//クール便の場合はクール料金をプラス
if ($cool_flg == "on") { $results['deliv_fee'] += 210; }

//冷凍便の場合は地域送料+クール料金を別途プラス(同梱不可の為)
if ($freez_flg == "on") {
$pref_deliv = $objDb->sfGetDelivFee($deliv_pref, $deliv_id);
$results['deliv_fee'] += ( $pref_deliv + 210 );
if ($cool_flg != "on" && $normal_flg != "on" ){ $results['deliv_fee']-=$pref_deliv; }
}



// 送料無料チェック
if ($this->isDelivFree($productTypeId)) {
$results['deliv_fee'] = 0;
}


//=====複数配送の場合の処理=============================================

if ( count($_SESSION['shipping']) > 1 ){

//---------[初期処理]-------------

//とりあえず、$results['deliv_fee']を0にしておく
$results['deliv_fee'] = 0;

//何人目かを記録しておくためのフラグ
$multi = 0;

//店舗基本情報を取得
$objDb = new SC_Helper_DB_Ex();
$arrInfo = $objDb->sfGetBasisData();

//---------[初期処理終了]---------

//人数分の繰り返し
foreach ( $_SESSION['shipping'] as $deli) {

//冷凍・冷蔵フラグを初期化
$normal_flg = "";//追加
$cool_flg = "";
$freez_flg = "";


//フラグ関係を初期化
$total_indiv = 0;

//商品のコードを配列から取得
$temp_list = $deli['shipment_item'];
$item = array_keys( $temp_list );

//商品分の繰り返し
for ($loop = 0; $loop < count($item); $loop++){

$item_code = $item[$loop];

$quant = $item_code;
$quantity = $deli['shipment_item'][$quant]['quantity'];

//商品価格
$price = $deli['shipment_item'][$quant]["productsClass"]["price02"];
//商品個別送料
$deliv = $deli['shipment_item'][$quant]["productsClass"]["deliv_fee"];



//冷蔵便フラグ
if ( $normal_flg <> "on" ) {
$normal_flg = ( $deli['shipment_item'][$quant]["productsClass"]["cool_flg"] == 1) ? "on":"";
}


if ( $cool_flg <> "on" ) {
$cool_flg = ( $deli['shipment_item'][$quant]["productsClass"]["cool_flg"] == 1) ? "on":"";
}
//冷凍便
if ( $freez_flg <> "on" ) {
$freez_flg = ( $deli['shipment_item'][$quant]["productsClass"]["cool_flg"] == 2) ? "on":"";
}


// 配送業者の送料を加算
if (OPTION_DELIV_FEE == 1) {
$deliv = $objDb->sfGetDelivFee($deli["shipping_pref"], $deli["deliv_id"]);
}

//商品の購入金額
$temp_indiv = $price * $quantity;
$total_indiv += $temp_indiv;

//送料の計算(商品毎に加算)
$deliv_fee .= $deliv;

//送料の合計が最大送料(配送地域別送料)を超えたら、送料は地域別送料に
if ( $deliv_fee > $objDb->sfGetDelivFee($deli["shipping_pref"], $deli["deliv_id"])) {

$deliv_fee = $objDb->sfGetDelivFee($deli["shipping_pref"], $deli["deliv_id"]);

}


}//商品分の繰り返し

//冷蔵便の処理
if ( $cool_flg == "on" && $deliv_fee <> 0) { $deliv_fee += 210; }

//冷凍便の場合は地域送料+クール料金を別途プラス(同梱不可の為)
if ($freez_flg == "on") {
$pref_deliv = $objDb->sfGetDelivFee($deli["shipping_pref"], $deli["deliv_id"]);
$deliv_fee += ( $pref_deliv + 210 );
}


// 送料無料条件が設定されている場合
if ($arrInfo['free_rule'] > 0) {
// 小計が無料条件を超えている場合
if( $total_indiv >= $arrInfo['free_rule']) {
$deliv_fee = 0;
if ($cool_flg != "on" && $normal_flg != "on" ){
$deliv_fee-=$pref_deliv;
}

}
}

//全注文の総トータル送料に個別送料を加算していく
$results['deliv_fee'] += $deliv_fee;

//送料と合計金額をセッションに保存(comfirm.tpl表示用)
$_SESSION['multi_deliv'][$multi]['deliv_fee'] = $deliv_fee;
$_SESSION['multi_deliv'][$multi]['total_indiv'] = $total_indiv;

$multi++;

}//人数分の繰り返し

}//if

//====複数配送の場合の送料再計算終了==============================

// 合計を計算
$results['total'] = $results['subtotal'];
$results['total'] += $results['deliv_fee'];
$results['total'] += $charge;
$results['total'] -= $discount;

// お支払い合計
$results['payment_total'] = $results['total'] - $use_point * POINT_VALUE;
// 加算ポイントの計算
if (USE_POINT !== false) {
$results['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($total_point,$use_point);

if($objCustomer != "") {
// 誕生日月であった場合
if($objCustomer->isBirthMonth()) {
$results['birth_point'] = BIRTH_MONTH_POINT;
$results['add_point'] += $results['birth_point'];
}
}

if($results['add_point'] < 0) {
$results['add_point'] = 0;
}

}

return $results;

}//function calculate()


ということになります。

XAMPP + EC-CUBE2.11.4 でテストしましたが…。




追記。
赤字部分を追加しました。

冷凍便のみを購入した場合(通常便、冷蔵便ともにフラグが立っていない場合)には、送料が加算されてしまうのを防ぐためです。