public function index(Application $app, Request $request)
{
$cartService = $app['eccube.service.cart'];
// カートチェック
if (!$cartService->isLocked()) {
log_info('カートが存在しません');
// カートが存在しない、カートがロックされていない時はエラー
return $app->redirect($app->url('cart'));
}
// カートチェック
if (count($cartService->getCart()->getCartItems()) <= 0) {
log_info('カートに商品が入っていないためショッピングカート画面にリダイレクト');
// カートが存在しない時はエラー
return $app->redirect($app->url('cart'));
}
// 登録済みの受注情報を取得
$Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
// 初回アクセス(受注情報がない)の場合は, 受注情報を作成
if (is_null($Order)) {
// 未ログインの場合, ログイン画面へリダイレクト.
if (!$app->isGranted('IS_AUTHENTICATED_FULLY')) {
// 非会員でも一度会員登録されていればショッピング画面へ遷移
$Customer = $app['eccube.service.shopping']->getNonMember($this->sessionKey);
if (is_null($Customer)) {
log_info('未ログインのためログイン画面にリダイレクト');
return $app->redirect($app->url('shopping_login'));
}
} else {
$Customer = $app->user();
}
try {
// 受注情報を作成
$Order = $app['eccube.service.shopping']->createOrder($Customer);
} catch (CartException $e) {
log_error('初回受注情報作成エラー', array($e->getMessage()));
$app->addRequestError($e->getMessage());
return $app->redirect($app->url('cart'));
}
// セッション情報を削除
$app['session']->remove($this->sessionOrderKey);
$app['session']->remove($this->sessionMultipleKey);
}
// 受注関連情報を最新状態に更新
$app['orm.em']->refresh($Order);
// form作成
$builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
$event = new EventArgs(array('builder' => $builder, 'Order' => $Order), $request);
$app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_INDEX_INITIALIZE, $event);
$form = $builder->getForm();
if ($Order->getTotalPrice() < 0) {
// 合計金額がマイナスの場合、エラー
log_info('受注金額マイナスエラー', array($Order->getId()));
$message = $app->trans('shopping.total.price', array('totalPrice' => number_format($Order->getTotalPrice())));
$app->addError($message);
return $app->redirect($app->url('shopping_error'));
}
// 複数配送の場合、エラーメッセージを一度だけ表示
if (!$app['session']->has($this->sessionMultipleKey)) {
if (count($Order->getShippings()) > 1) {
$app->addRequestError('shopping.multiple.delivery');
}
$app['session']->set($this->sessionMultipleKey, 'multiple');
}
return $app->render('Shopping/index.twig', array('form' => $form->createView(), 'Order' => $Order));
}