バージョン選択

フォーラム

メニュー

オンライン状況

59 人のユーザが現在オンラインです。 (51 人のユーザが フォーラム を参照しています。)
登録ユーザ: 0
ゲスト: 59
もっと...

サイト内検索

質問 > フロント機能 > ShoppingService.phpでの商品規格情報取得

フロント機能

新規スレッドを追加する

スレッド表示 | 新しいものから 前のトピック | 次のトピック | 下へ
投稿者 スレッド
ゲスト
投稿日時: 2017/12/10 10:43
対応状況: 解決済
ShoppingService.phpでの商品規格情報取得
初めまして、プログラミング初心者なのですが、質問させてください。

現在、送料計算の方法を変更しようと「ShoppingService.php」にコードを書き加えています。
最終的にやろうとしていることは、「注文(配送)商品の規格と数量から、箱の大きさを判定し、適切な送料を計算する」という内容です。

そのうえで、注文(配送)内容から規格別の注文数量を取得しようとしており、以下のように書いてみているのですが、規格別の注文数量がうまく加算されないようです。
ShoppingService.phpのsetShippingDeliveryFee内
----------
// 規格別の本数を取得

$bigItemQuantity = 0;
$smallItemQuantity = 0;

$shipmentItems = $Shipping->getShipmentItems();
foreach ($shipmentItems as $ShipmentItem) {
if ($ShipmentItem->getProductClass()->getClassCategory1() == 1){
$smallItemQuantity += $ShipmentItem->getQuantity();
}
if ($ShipmentItem->getProductClass()->getClassCategory1() == 2){
$bigItemQuantity += $ShipmentItem->getQuantity();
}
}
----------

上記の結果なのですが、ClassCategory1 == 1の商品を複数注文しても、ClassCategory1 == 2の商品を複数注文しても、$smallItemQuantityや$bigItemQuantityが0のままになってしまっているようです。

改善方法やアドバイスなどいただけますと助かります。
よろしくお願いします!

==========
▼テンプレート
[EC-CUBE] 3.0.10
[レンタルサーバ] - (ローカルサーバ)
[OS] OS X 10.12.6
[PHP] 7.0.0
[データベース] MySQL5.5.42
[WEBサーバ] Apache
[ブラウザ] Chrome
[導入プラグインの有無] 無
[カスタマイズの有無] 本件以外はほぼ無し
[現象] 上記
minori
投稿日時: 2017/12/11 10:59
対応状況: −−−
常連
登録日: 2017/10/12
居住地:
投稿: 40
Re: ShoppingService.phpでの商品規格情報取得
比較している値が商品規格IDでしたら、
取得する項目が異なっていることが原因だと思います。

$ShipmentItem->getProductClass()->getClassCategory1()

$ShipmentItem->getProductClass()->getId()
に変更するとどうでしょうか?
ゲスト
投稿日時: 2017/12/11 11:16
対応状況: −−−
Re: ShoppingService.phpでの商品規格情報取得
ご返信ありがとうございます!

ClassCategory1に相当する規格に、各商品共通で大きさを示す内容を設定しております。
それが大なのか小なのか、によって箱の大きさが変わり、送料が変わるためそれを反映したいと考えております。

ProductClassのテーブルに入っているレコードを簡易的に書くと、
商品1 - 大
商品1 - 小
商品2 - 大
商品2 - 小
商品3 - 大
商品3 - 小



という感じになっています。
たとえば商品1 - 大と商品2 -小を1点ずつ注文された場合、小さな箱には入らないので両方を大きな箱1つに入れて送ることになります。
別の注文で、商品3 - 小を2点注文された場合には、小さな箱1つに入れて送ることが可能です。
これらのケースで送料が異なるため、それを反映したい、というのがやろうとしていることです。
商品点数が多くなるとさらに、大きな箱1つと小さな箱1つで送る、などのケースもあるため、まずは大きな商品と小さな商品の数をカウントしようとしているのですが、その部分でつまづいております。

その上で何か思い当たることなどありましたら、引き続き教えていただけますと大変助かります!
minori
投稿日時: 2017/12/11 11:55
対応状況: −−−
常連
登録日: 2017/10/12
居住地:
投稿: 40
Re: ShoppingService.phpでの商品規格情報取得
詳しいご説明ありがとうございます。
状況が理解できました。

getClassCategory1だと値ではなくオブジェクトが返却されて
しまうので、正しく比較できていないかもしれません。

実行して確認していないため動くかどうかわかりませんが、
以下のような感じだとどうでしょうか?

$shipmentItems = $Shipping->getShipmentItems();
foreach ($shipmentItems as $ShipmentItem) {
    // ClassCategory1が存在しない可能性があれば、is_null関数でチェックします
    if (!is_null($ShipmentItem->getProductClass()->getClassCategory1())) {
        $classCategoryId = $ShipmentItem->getProductClass()->getClassCategory1()->getId();
        if ($classCategoryId == 1){
            $smallItemQuantity += $ShipmentItem->getQuantity();
        }
        if ($classCategoryId == 2){
            $bigItemQuantity += $ShipmentItem->getQuantity();
        }
     }
}

ゲスト
投稿日時: 2017/12/11 23:41
対応状況: −−−
Re: ShoppingService.phpでの商品規格情報取得
ご丁寧にありがとうございます!
開発環境を触れる場所にいなかったため、返信遅くなってしまい申し訳ありませんでした。

記載いただいた通りにコードを変更してみましたが、やはりsmallItemQuantityもbigItemQuantityも0のままになってしまうようでした・・・

ご指摘いただいたとおり、getClassCategory1だけでなくその先のgetIdを行う必要がありそうなので、これでいけるかと思ったのですが。
他の部分に原因があるのかもしれないと思いましたので、現状setShippingDeliveryFeeの部分がどうなっているかを以下に記載いたします。

もし他にお気付きの点等あれば、ご指摘いただければ助かりますm(_ _)m


    /**
     * 配送料金の設定
     *
     * @param Shipping $Shipping
     * @param Delivery|null $Delivery
     */
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
    {
        $bigItemQuantity = 0;
        $smallItemQuantity = 0;
        $bigBoxQuantity = 0;
        $smallBoxQuantity = 0;
        // 配送料金の設定
        if (is_null($Delivery)) {
            $Delivery = $Shipping->getDelivery();
        }
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));

        $Shipping->setDeliveryFee($deliveryFee);
        $Shipping->setDelivery($Delivery);

        // 規格別の本数を取得
        $shipmentItems = $Shipping->getShipmentItems();

        foreach ($shipmentItems as $ShipmentItem) {
            if(!is_null($ShipmentItem->getProductClass()->getClassCategory1())){
                $classCategoryId = $ShipmentItem->getProductClass()->getClassCategory1()->getId();
                if ($classCategoryId == 1){
                    $smallItemQuantity += $ShipmentItem->getQuantity();
                }
                if ($classCategoryId == 2){
                    $bigItemQuantity += $ShipmentItem->getQuantity();
                }
            }
        }

        // 箱(大・小)の数を算出
        if ($bigItemQuantity <= 0) {
            if ($smallItemQuantity <= 3){
                $smallBoxQuantity = 1;
            }else{
                $bigBoxQuantity = 1;
            }
        }else if ($bigItemQuantity <= 6) {
            if ($smallItemQuantity + $bigItemQuantity <= 8){
                $bigBoxQuantity = 1;
            }else if ($smallItemQuantity + $bigItemQuantity <= 11){
                $bigBoxQuantity = 1;
                $smallBoxQuantity = 1;
            }else{
                $bigBoxQuantity = 2;
            }
        }else {
            $bigBoxQuantity = 2;
        }

        // 商品ごとの配送料合計
        $productDeliveryFeeTotal = 0;
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
        }

        $sizeFee = 240 * $smallBoxQuantity + 420 * $bigBoxQuantity;  //テスト段階のためここは定数ベタ打ちになっています。
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() * ($bigBoxQuantity + $smallBoxQuantity) + $sizeFee + $productDeliveryFeeTotal);
        $Shipping->setShippingDeliveryName($Delivery->getName());
    }
minori
投稿日時: 2017/12/12 11:56
対応状況: −−−
常連
登録日: 2017/10/12
居住地:
投稿: 40
Re: ShoppingService.phpでの商品規格情報取得
ShoppingServiceの呼び出し元の処理を確認してみました。

        // お届け先情報を作成
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);

        // 受注明細情報、配送商品情報を作成
        $Order = $this->getNewDetails($Order);

        // 小計
        $subTotal = $this->orderService->getSubTotal($Order);

        // 消費税のみの小計
        $tax = $this->orderService->getTotalTax($Order);

        // 配送料合計金額
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));

getNewShippingメソッドから、今回修正した
setShippingDeliveryFeeメソッドが呼び出されているのですが、
この時点ではShippingItemのデータが作成されていません。

次に呼び出されているgetNewDetailsのメソッド内で
カートに入っている商品をループしながら
OrderDetailやShippintItemのデータを作成しています。

そのため、$Shipping->getShipmentItems()の時点では
取得件数が0件ですので、その後のループ文の中に
入っておらず数量が計算されなかったようです。

// 規格別の本数を取得
$shipmentItems = $Shipping->getShipmentItems();

// $shipmentItemsが0件のためループに入らない
foreach ($shipmentItems as $ShipmentItem) {
    if(!is_null($ShipmentItem->getProductClass()->getClassCategory1())){
        $classCategoryId = $ShipmentItem->getProductClass()->getClassCategory1()->getId();
        if ($classCategoryId == 1){
            $smallItemQuantity += $ShipmentItem->getQuantity();
        }
        if ($classCategoryId == 2){
            $bigItemQuantity += $ShipmentItem->getQuantity();
        }
    }
}

ShipmentItem作成後に処理を追加してあげる必要があるので、
getNewDetailsメソッドの後に呼び出されている
配送料合計金額を取得しているメソッド(getShippingDeliveryFeeTotal)に
手を加えてみてはいかがでしょうか?
ゲスト
投稿日時: 2017/12/12 14:11
対応状況: −−−
Re: ShoppingService.phpでの商品規格情報取得
呼び出し元の処理順のこと、全然気がついておりませんでした!
ありがとうございます!
setShippingDeliveryFeeメソッドは元に戻し、
getShippingDeliveryFeeTotalメソッドを以下のように直すことで、
やりたいとおりの動作をするようになりました。

ご丁寧に教えていただき、本当にありがとうございました!!


    public function getShippingDeliveryFeeTotal($shippings)
    {
        $bigItemQuantity = 0;
        $smallItemQuantity = 0;
        $bigBoxQuantity = 0;
        $smallBoxQuantity = 0;

        $deliveryFeeTotal = 0;
        foreach ($shippings as $Shipping) {
            // 規格別の本数を取得
            $shipmentItems = $Shipping->getShipmentItems();

            foreach ($shipmentItems as $ShipmentItem) {
                if(!is_null($ShipmentItem->getProductClass()->getClassCategory1())){
                    $classCategoryId = $ShipmentItem->getProductClass()->getClassCategory1()->getId();
                    if ($classCategoryId == 1){
                        $smallItemQuantity += $ShipmentItem->getQuantity();
                    }
                    if ($classCategoryId == 2){
                        $bigItemQuantity += $ShipmentItem->getQuantity();
                    }
                }
            }

            // 箱(大・小)の数を算出
            if ($bigItemQuantity <= 0) {
                if ($smallItemQuantity <= 3){
                    $smallBoxQuantity = 1;
                }else{
                    $bigBoxQuantity = 1;
                }
            }else if ($bigItemQuantity <= 6) {
                if ($smallItemQuantity + $bigItemQuantity <= 8){
                    $bigBoxQuantity = 1;
                }else if ($smallItemQuantity + $bigItemQuantity <= 11){
                    $bigBoxQuantity = 1;
                    $smallBoxQuantity = 1;
                }else{
                    $bigBoxQuantity = 2;
                }
            }else {
                $bigBoxQuantity = 2;
            }

            $sizeFee = 240 * $smallBoxQuantity + 420 * $bigBoxQuantity;

            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee() * ($bigBoxQuantity + $smallBoxQuantity) + $sizeFee ;
        }

        return $deliveryFeeTotal;

    }
スレッド表示 | 新しいものから 前のトピック | 次のトピック | トップ


 



ログイン


EC-CUBE公式 Amazon Payプラグイン

統計情報

総メンバー数は88,720名です
総投稿数は109,953件です

投稿数ランキング

1
seasoft
7367
2
468
3217
3
AMUAMU
2712
4
nanasess
2313
5
umebius
2085
6
yuh
1819
7
h_tanaka
1638
8
red
1570
9
mcontact
1286
10
tsuji
958
11
fukap
907
12
shutta
835
13
tao_s
799
14 ramrun 789
15 karin 689
16 sumida 641
17
homan
633
18 DELIGHT 572
19
patapata
502
20
flealog
485


ネットショップの壺

EC-CUBEインテグレートパートナー

Copyright© EC-CUBE CO.,LTD. All Rights Reserved.