| <?php |
| <?php |
| /* |
| /* |
| * This file is part of EC-CUBE |
| * This file is part of EC-CUBE |
| * |
| * |
| * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. |
| * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. |
| * |
| * |
| * http://www.ec-cube.co.jp/ |
| * http://www.ec-cube.co.jp/ |
| * |
| * |
| * This program is free software; you can redistribute it and/or |
| * This program is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU General Public License |
| * modify it under the terms of the GNU General Public License |
| * as published by the Free Software Foundation; either version 2 |
| * as published by the Free Software Foundation; either version 2 |
| * of the License, or (at your option) any later version. |
| * of the License, or (at your option) any later version. |
| * |
| * |
| * This program is distributed in the hope that it will be useful, |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * GNU General Public License for more details. |
| * |
| * |
| * You should have received a copy of the GNU General Public License |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| */ |
| */ |
| |
| |
| |
| |
| namespace Eccube\Service; |
| namespace Eccube\Service; |
| |
| |
| use Doctrine\ORM\EntityManager; |
| use Doctrine\ORM\EntityManager; |
| use Eccube\Common\Constant; |
| use Eccube\Common\Constant; |
| use Eccube\Entity\CartItem; |
| use Eccube\Entity\CartItem; |
| use Eccube\Entity\Master\Disp; |
| use Eccube\Entity\Master\Disp; |
| use Eccube\Entity\ProductClass; |
| use Eccube\Entity\ProductClass; |
| use Eccube\Exception\CartException; |
| use Eccube\Exception\CartException; |
| use Symfony\Component\HttpFoundation\Session\Session; |
| use Symfony\Component\HttpFoundation\Session\Session; |
| |
| |
| class CartService |
| class CartService |
| { |
| { |
| /** @var \Eccube\Application */ |
| /** @var \Eccube\Application */ |
| public $app; |
| public $app; |
| |
| |
| /** |
| /** |
| * @var Session |
| * @var Session |
| */ |
| */ |
| private $session; |
| private $session; |
| |
| |
| /** |
| /** |
| * @var EntityManager |
| * @var EntityManager |
| */ |
| */ |
| private $entityManager; |
| private $entityManager; |
| |
| |
| /** |
| /** |
| * @var \Eccube\Entity\Cart |
| * @var \Eccube\Entity\Cart |
| */ |
| */ |
| private $cart; |
| private $cart; |
| |
| |
| /** |
| /** |
| * @var \Eccube\Entity\BaseInfo |
| * @var \Eccube\Entity\BaseInfo |
| */ |
| */ |
| private $BaseInfo; |
| private $BaseInfo; |
| |
| |
| /** |
| /** |
| * @var array |
| * @var array |
| */ |
| */ |
| private $errors = array(); |
| private $errors = array(); |
| |
| |
| private $ProductType = null; |
| private $ProductType = null; |
| |
| |
| /** |
| /** |
| * @var array |
| * @var array |
| */ |
| */ |
| private $messages = array(); |
| private $messages = array(); |
| |
| |
| /** |
| /** |
| * @var array |
| * @var array |
| */ |
| */ |
| private $error; |
| private $error; |
| |
| |
| public function __construct(\Eccube\Application $app) |
| public function __construct(\Eccube\Application $app) |
| { |
| { |
| $this->app = $app; |
| $this->app = $app; |
| $this->session = $app['session']; |
| $this->session = $app['session']; |
| $this->entityManager = $app['orm.em']; |
| $this->entityManager = $app['orm.em']; |
| |
| |
| if ($this->session->has('cart')) { |
| if ($this->session->has('cart')) { |
| $this->cart = $this->session->get('cart'); |
| $this->cart = $this->session->get('cart'); |
| } else { |
| } else { |
| $this->cart = new \Eccube\Entity\Cart(); |
| $this->cart = new \Eccube\Entity\Cart(); |
| } |
| } |
| |
| |
| $this->loadProductClassFromCart(); |
| $this->loadProductClassFromCart(); |
| |
| |
| $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
| $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
| } |
| } |
| |
| |
| /** |
| /** |
| * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
| * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
| */ |
| */ |
| protected function loadProductClassFromCart() |
| protected function loadProductClassFromCart() |
| { |
| { |
| /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
| /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
| $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete'); |
| $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete'); |
| $excludes = $softDeleteFilter->getExcludes(); |
| $excludes = $softDeleteFilter->getExcludes(); |
| $softDeleteFilter->setExcludes(array( |
| $softDeleteFilter->setExcludes(array( |
| 'Eccube\Entity\ProductClass', |
| 'Eccube\Entity\ProductClass', |
| )); |
| )); |
| |
| |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| $this->loadProductClassFromCartItem($CartItem); |
| $this->loadProductClassFromCartItem($CartItem); |
| } |
| } |
| |
| |
| $softDeleteFilter->setExcludes($excludes); |
| $softDeleteFilter->setExcludes($excludes); |
| } |
| } |
| |
| |
| /** |
| /** |
| * CartItem に対応する ProductClass を設定します。 |
| * CartItem に対応する ProductClass を設定します。 |
| * |
| * |
| * @param CartItem $CartItem |
| * @param CartItem $CartItem |
| */ |
| */ |
| protected function loadProductClassFromCartItem(CartItem $CartItem) |
| protected function loadProductClassFromCartItem(CartItem $CartItem) |
| { |
| { |
| $ProductClass = $this |
| $ProductClass = $this |
| ->entityManager |
| ->entityManager |
| ->getRepository($CartItem->getClassName()) |
| ->getRepository($CartItem->getClassName()) |
| ->find($CartItem->getClassId()); |
| ->find($CartItem->getClassId()); |
| |
| |
| $CartItem->setObject($ProductClass); |
| $CartItem->setObject($ProductClass); |
| |
| |
| if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
| if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
| $this->setCanAddProductType($ProductClass->getProductType()); |
| $this->setCanAddProductType($ProductClass->getProductType()); |
| } |
| } |
| } |
| } |
| |
| |
| public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
| public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
| { |
| { |
| if (is_null($this->ProductType)) { |
| if (is_null($this->ProductType)) { |
| $this->ProductType = $ProductType; |
| $this->ProductType = $ProductType; |
| } |
| } |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| public function save() |
| public function save() |
| { |
| { |
| return $this->session->set('cart', $this->cart); |
| return $this->session->set('cart', $this->cart); |
| } |
| } |
| |
| |
| public function unlock() |
| public function unlock() |
| { |
| { |
| $this->cart |
| $this->cart |
| ->setLock(false) |
| ->setLock(false) |
| ->setPreOrderId(null); |
| ->setPreOrderId(null); |
| } |
| } |
| |
| |
| public function lock() |
| public function lock() |
| { |
| { |
| $this->cart |
| $this->cart |
| ->setLock(true) |
| ->setLock(true) |
| ->setPreOrderId(null); |
| ->setPreOrderId(null); |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return bool |
| * @return bool |
| */ |
| */ |
| public function isLocked() |
| public function isLocked() |
| { |
| { |
| return $this->cart->getLock(); |
| return $this->cart->getLock(); |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $pre_order_id |
| * @param string $pre_order_id |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function setPreOrderId($pre_order_id) |
| public function setPreOrderId($pre_order_id) |
| { |
| { |
| $this->cart->setPreOrderId($pre_order_id); |
| $this->cart->setPreOrderId($pre_order_id); |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return string |
| * @return string |
| */ |
| */ |
| public function getPreOrderId() |
| public function getPreOrderId() |
| { |
| { |
| return $this->cart->getPreOrderId(); |
| return $this->cart->getPreOrderId(); |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function clear() |
| public function clear() |
| { |
| { |
| $this->cart |
| $this->cart |
| ->setPreOrderId(null) |
| ->setPreOrderId(null) |
| ->setLock(false) |
| ->setLock(false) |
| ->clearCartItems(); |
| ->clearCartItems(); |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| public function getCanAddProductType() |
| public function getCanAddProductType() |
| { |
| { |
| return $this->ProductType; |
| return $this->ProductType; |
| } |
| } |
| |
| |
| /** |
| /** |
| * |
| * |
| * @param string $productClassId |
| * @param string $productClassId |
| * @param integer $quantity |
| * @param integer $quantity |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function addProduct($productClassId, $quantity = 1) |
| public function addProduct($productClassId, $quantity = 1) |
| { |
| { |
| $quantity += $this->getProductQuantity($productClassId); |
| $quantity += $this->getProductQuantity($productClassId); |
| $this->setProductQuantity($productClassId, $quantity); |
| $this->setProductQuantity($productClassId, $quantity); |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $productClassId |
| * @param string $productClassId |
| * @return integer |
| * @return integer |
| */ |
| */ |
| public function getProductQuantity($productClassId) |
| public function getProductQuantity($productClassId) |
| { |
| { |
| $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
| $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
| if ($CartItem) { |
| if ($CartItem) { |
| return $CartItem->getQuantity(); |
| return $CartItem->getQuantity(); |
| } else { |
| } else { |
| return 0; |
| return 0; |
| } |
| } |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param \Eccube\Entity\ProductClass|integer $ProductClass |
| * @param \Eccube\Entity\ProductClass|integer $ProductClass |
| * @param integer $quantity |
| * @param integer $quantity |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| * @throws CartException |
| * @throws CartException |
| */ |
| */ |
| public function setProductQuantity($ProductClass, $quantity) |
| public function setProductQuantity($ProductClass, $quantity) |
| { |
| { |
| if (!$ProductClass instanceof ProductClass) { |
| if (!$ProductClass instanceof ProductClass) { |
| $ProductClass = $this->entityManager |
| $ProductClass = $this->entityManager |
| ->getRepository('Eccube\Entity\ProductClass') |
| ->getRepository('Eccube\Entity\ProductClass') |
| ->find($ProductClass); |
| ->find($ProductClass); |
| if (!$ProductClass) { |
| if (!$ProductClass) { |
| throw new CartException('cart.product.delete'); |
| throw new CartException('cart.product.delete'); |
| } |
| } |
| } |
| } |
| |
| |
| if (!$this->isProductDisplay($ProductClass)) { |
| if (!$this->isProductDisplay($ProductClass)) { |
| throw new CartException('cart.product.not.status'); |
| throw new CartException('cart.product.not.status'); |
| } |
| } |
| |
| |
| $productName = $this->getProductName($ProductClass); |
| $productName = $this->getProductName($ProductClass); |
| |
| |
| // 商品種別に紐づく配送業者を取得 |
| // 商品種別に紐づく配送業者を取得 |
| $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType()); |
| $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType()); |
| |
| |
| if (count($deliveries) == 0) { |
| if (count($deliveries) == 0) { |
| // 商品種別が存在しなければエラー |
| // 商品種別が存在しなければエラー |
| $this->removeProduct($ProductClass->getId()); |
| $this->removeProduct($ProductClass->getId()); |
| $this->addError('cart.product.not.producttype', $productName); |
| $this->addError('cart.product.not.producttype', $productName); |
| throw new CartException('cart.product.not.producttype'); |
| throw new CartException('cart.product.not.producttype'); |
| } |
| } |
| |
| |
| $this->setCanAddProductType($ProductClass->getProductType()); |
| $this->setCanAddProductType($ProductClass->getProductType()); |
| |
| |
| if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) { |
| if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) { |
| if (!$this->canAddProduct($ProductClass->getId())) { |
| if (!$this->canAddProduct($ProductClass->getId())) { |
| // 複数配送対応でなければ商品種別が異なればエラー |
| // 複数配送対応でなければ商品種別が異なればエラー |
| throw new CartException('cart.product.type.kind'); |
| throw new CartException('cart.product.type.kind'); |
| } |
| } |
| } else { |
| } else { |
| // 複数配送の場合、同一支払方法がなければエラー |
| // 複数配送の場合、同一支払方法がなければエラー |
| if (!$this->canAddProductPayment($ProductClass->getProductType())) { |
| if (!$this->canAddProductPayment($ProductClass->getProductType())) { |
| throw new CartException('cart.product.payment.kind'); |
| throw new CartException('cart.product.payment.kind'); |
| } |
| } |
| } |
| } |
| |
| |
| $tmp_subtotal = 0; |
| $tmp_subtotal = 0; |
| $tmp_quantity = 0; |
| $tmp_quantity = 0; |
| foreach ($this->getCartObj()->getCartItems() as $cartitem) { |
| foreach ($this->getCartObj()->getCartItems() as $cartitem) { |
| $pc = $cartitem->getObject(); |
| $pc = $cartitem->getObject(); |
| if ($pc->getId() != $ProductClass->getId()) { |
| if ($pc->getId() != $ProductClass->getId()) { |
| // 追加された商品以外のtotal priceをセット |
| // 追加された商品以外のtotal priceをセット |
| $tmp_subtotal += $cartitem->getTotalPrice(); |
| $tmp_subtotal += $cartitem->getTotalPrice(); |
| } |
| } |
| } |
| } |
. | for ($i = 0; $i < $quantity; $i++) { |
| |
| $tmp_subtotal += $ProductClass->getPrice02IncTax(); |
| // 購入金額上限を超えてしまう数量はエラー |
| if ($tmp_subtotal > $this->app['config']['max_total_fee']) { |
| $maxQuantity = floor(($this->app['config']['max_total_fee'] - $tmp_subtotal) / $ProductClass->getPrice02IncTax()); |
| $this->setError('cart.over.price_limit'); |
| if ($quantity > $maxQuantity) { |
| break; |
| $this->setError('cart.over.price_limit'); |
| } |
| $tmp_quantity += $maxQuantity; |
| $tmp_quantity++; |
| } else { |
| |
| $tmp_quantity += $quantity; |
| } |
| } |
. | |
| |
| if ($tmp_quantity == 0) { |
| if ($tmp_quantity == 0) { |
| // 数量が0の場合、エラー |
| // 数量が0の場合、エラー |
| throw new CartException('cart.over.price_limit'); |
| throw new CartException('cart.over.price_limit'); |
| } |
| } |
| |
| |
| // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
| // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
| $quantity = $this->setProductLimit($ProductClass, $productName, $tmp_quantity); |
| $quantity = $this->setProductLimit($ProductClass, $productName, $tmp_quantity); |
| |
| |
| // 新しい数量でカート内商品を登録する |
| // 新しい数量でカート内商品を登録する |
| if (0 < $quantity) { |
| if (0 < $quantity) { |
| $CartItem = new CartItem(); |
| $CartItem = new CartItem(); |
| $CartItem |
| $CartItem |
| ->setClassName('Eccube\Entity\ProductClass') |
| ->setClassName('Eccube\Entity\ProductClass') |
| ->setClassId((string)$ProductClass->getId()) |
| ->setClassId((string)$ProductClass->getId()) |
| ->setPrice($ProductClass->getPrice02IncTax()) |
| ->setPrice($ProductClass->getPrice02IncTax()) |
| ->setQuantity($quantity); |
| ->setQuantity($quantity); |
| |
| |
| $this->cart->setCartItem($CartItem); |
| $this->cart->setCartItem($CartItem); |
| } |
| } |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $productClassId |
| * @param string $productClassId |
| * @return boolean |
| * @return boolean |
| */ |
| */ |
| public function canAddProduct($productClassId) |
| public function canAddProduct($productClassId) |
| { |
| { |
| $ProductClass = $this |
| $ProductClass = $this |
| ->entityManager |
| ->entityManager |
| ->getRepository('\Eccube\Entity\ProductClass') |
| ->getRepository('\Eccube\Entity\ProductClass') |
| ->find($productClassId); |
| ->find($productClassId); |
| |
| |
| if (!$ProductClass) { |
| if (!$ProductClass) { |
| return false; |
| return false; |
| } |
| } |
| |
| |
| $ProductType = $ProductClass->getProductType(); |
| $ProductType = $ProductClass->getProductType(); |
| |
| |
| return $this->ProductType == $ProductType; |
| return $this->ProductType == $ProductType; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param \Eccube\Entity\Master\ProductType $ProductType |
| * @param \Eccube\Entity\Master\ProductType $ProductType |
| * @return bool |
| * @return bool |
| */ |
| */ |
| public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
| public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
| { |
| { |
| $deliveries = $this |
| $deliveries = $this |
| ->entityManager |
| ->entityManager |
| ->getRepository('\Eccube\Entity\Delivery') |
| ->getRepository('\Eccube\Entity\Delivery') |
| ->findBy(array('ProductType' => $ProductType)); |
| ->findBy(array('ProductType' => $ProductType)); |
| |
| |
| // 支払方法を取得 |
| // 支払方法を取得 |
| $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
| $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
| |
| |
| if ($this->getCart()->getTotalPrice() < 1) { |
| if ($this->getCart()->getTotalPrice() < 1) { |
| // カートになければ支払方法を全て設定 |
| // カートになければ支払方法を全て設定 |
| $this->getCart()->setPayments($payments); |
| $this->getCart()->setPayments($payments); |
| |
| |
| return true; |
| return true; |
| } |
| } |
| |
| |
| // カートに存在している支払方法と追加された商品の支払方法チェック |
| // カートに存在している支払方法と追加された商品の支払方法チェック |
| $arr = array(); |
| $arr = array(); |
| foreach ($payments as $payment) { |
| foreach ($payments as $payment) { |
| foreach ($this->getCart()->getPayments() as $p) { |
| foreach ($this->getCart()->getPayments() as $p) { |
| if ($payment['id'] == $p['id']) { |
| if ($payment['id'] == $p['id']) { |
| $arr[] = $payment; |
| $arr[] = $payment; |
| break; |
| break; |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| if (count($arr) > 0) { |
| if (count($arr) > 0) { |
| $this->getCart()->setPayments($arr); |
| $this->getCart()->setPayments($arr); |
| |
| |
| return true; |
| return true; |
| } |
| } |
| |
| |
| // 支払条件に一致しない |
| // 支払条件に一致しない |
| return false; |
| return false; |
| |
| |
| } |
| } |
| |
| |
| /** |
| /** |
| * カートブロックに表示するカートを取得します。 |
| * カートブロックに表示するカートを取得します。 |
| * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
| * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
| * |
| * |
| * @return \Eccube\Entity\Cart |
| * @return \Eccube\Entity\Cart |
| */ |
| */ |
| public function getCartObj() |
| public function getCartObj() |
| { |
| { |
| |
| |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| |
| |
| /** @var \Eccube\Entity\ProductClass $ProductClass */ |
| /** @var \Eccube\Entity\ProductClass $ProductClass */ |
| $ProductClass = $CartItem->getObject(); |
| $ProductClass = $CartItem->getObject(); |
| if (!$ProductClass) { |
| if (!$ProductClass) { |
| $this->loadProductClassFromCartItem($CartItem); |
| $this->loadProductClassFromCartItem($CartItem); |
| |
| |
| $ProductClass = $CartItem->getObject(); |
| $ProductClass = $CartItem->getObject(); |
| } |
| } |
| |
| |
| if ($ProductClass->getDelFlg()) { |
| if ($ProductClass->getDelFlg()) { |
| // 商品情報が削除されていたらエラー |
| // 商品情報が削除されていたらエラー |
| $this->setError('cart.product.delete'); |
| $this->setError('cart.product.delete'); |
| // カートから削除 |
| // カートから削除 |
| $this->removeProduct($ProductClass->getId()); |
| $this->removeProduct($ProductClass->getId()); |
| } |
| } |
| } |
| } |
| |
| |
| return $this->cart; |
| return $this->cart; |
| |
| |
| } |
| } |
| |
| |
| /** |
| /** |
| * カートを取得します。 |
| * カートを取得します。 |
| * |
| * |
| * @return \Eccube\Entity\Cart |
| * @return \Eccube\Entity\Cart |
| */ |
| */ |
| public function getCart() |
| public function getCart() |
| { |
| { |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| foreach ($this->cart->getCartItems() as $CartItem) { |
| |
| |
| /** @var \Eccube\Entity\ProductClass $ProductClass */ |
| /** @var \Eccube\Entity\ProductClass $ProductClass */ |
| $ProductClass = $CartItem->getObject(); |
| $ProductClass = $CartItem->getObject(); |
| if (!$ProductClass) { |
| if (!$ProductClass) { |
| $this->loadProductClassFromCartItem($CartItem); |
| $this->loadProductClassFromCartItem($CartItem); |
| |
| |
| $ProductClass = $CartItem->getObject(); |
| $ProductClass = $CartItem->getObject(); |
| } |
| } |
| |
| |
| if ($ProductClass->getDelFlg() == Constant::DISABLED) { |
| if ($ProductClass->getDelFlg() == Constant::DISABLED) { |
| // 商品情報が有効 |
| // 商品情報が有効 |
| |
| |
| if (!$this->isProductDisplay($ProductClass)) { |
| if (!$this->isProductDisplay($ProductClass)) { |
| $this->setError('cart.product.not.status'); |
| $this->setError('cart.product.not.status'); |
| } else { |
| } else { |
| |
| |
| $productName = $this->getProductName($ProductClass); |
| $productName = $this->getProductName($ProductClass); |
| |
| |
| // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
| // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
| $quantity = $this->setProductLimit($ProductClass, $productName, $CartItem->getQuantity()); |
| $quantity = $this->setProductLimit($ProductClass, $productName, $CartItem->getQuantity()); |
| |
| |
| /// 個数が異なれば、新しい数量でカート内商品を更新する |
| /// 個数が異なれば、新しい数量でカート内商品を更新する |
| if ((0 < $quantity) && ($CartItem->getQuantity() != $quantity)) { |
| if ((0 < $quantity) && ($CartItem->getQuantity() != $quantity)) { |
| // 個数が異なれば更新 |
| // 個数が異なれば更新 |
| $CartItem->setQuantity($quantity); |
| $CartItem->setQuantity($quantity); |
| $this->cart->setCartItem($CartItem); |
| $this->cart->setCartItem($CartItem); |
| } |
| } |
| } |
| } |
| |
| |
| } else { |
| } else { |
| // 商品情報が削除されていたらエラー |
| // 商品情報が削除されていたらエラー |
| $this->setError('cart.product.delete'); |
| $this->setError('cart.product.delete'); |
| // カートから削除 |
| // カートから削除 |
| $this->removeProduct($ProductClass->getId()); |
| $this->removeProduct($ProductClass->getId()); |
| } |
| } |
| } |
| } |
| |
| |
| return $this->cart; |
| return $this->cart; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $productClassId |
| * @param string $productClassId |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function removeProduct($productClassId) |
| public function removeProduct($productClassId) |
| { |
| { |
| $this->cart->removeCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
| $this->cart->removeCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
| |
| |
| // 支払方法の再設定 |
| // 支払方法の再設定 |
| if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
| if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
| |
| |
| // 複数配送対応 |
| // 複数配送対応 |
| $productTypes = array(); |
| $productTypes = array(); |
| foreach ($this->getCart()->getCartItems() as $item) { |
| foreach ($this->getCart()->getCartItems() as $item) { |
| /* @var $ProductClass \Eccube\Entity\ProductClass */ |
| /* @var $ProductClass \Eccube\Entity\ProductClass */ |
| $ProductClass = $item->getObject(); |
| $ProductClass = $item->getObject(); |
| $productTypes[] = $ProductClass->getProductType(); |
| $productTypes[] = $ProductClass->getProductType(); |
| } |
| } |
| |
| |
| // 配送業者を取得 |
| // 配送業者を取得 |
| $deliveries = $this->entityManager->getRepository('Eccube\Entity\Delivery')->getDeliveries($productTypes); |
| $deliveries = $this->entityManager->getRepository('Eccube\Entity\Delivery')->getDeliveries($productTypes); |
| |
| |
| // 支払方法を取得 |
| // 支払方法を取得 |
| $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
| $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
| |
| |
| $this->getCart()->setPayments($payments); |
| $this->getCart()->setPayments($payments); |
| } |
| } |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $error |
| * @param string $error |
| * @param string $productName |
| * @param string $productName |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function addError($error = null, $productName = null) |
| public function addError($error = null, $productName = null) |
| { |
| { |
| // Filter duplicate |
| // Filter duplicate |
| $arrError = $this->session->getFlashBag()->peek('eccube.front.request.error'); |
| $arrError = $this->session->getFlashBag()->peek('eccube.front.request.error'); |
| $arrProduct = $this->session->getFlashBag()->peek('eccube.front.request.product'); |
| $arrProduct = $this->session->getFlashBag()->peek('eccube.front.request.product'); |
| if (in_array($error, $arrError) && in_array($productName, $arrProduct)) { |
| if (in_array($error, $arrError) && in_array($productName, $arrProduct)) { |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| $this->errors[] = $error; |
| $this->errors[] = $error; |
| $this->session->getFlashBag()->add('eccube.front.request.error', $error); |
| $this->session->getFlashBag()->add('eccube.front.request.error', $error); |
| if (!is_null($productName)) { |
| if (!is_null($productName)) { |
| // 追加されているエラーのキーを取得する |
| // 追加されているエラーのキーを取得する |
| $cnt = $this->session->getFlashBag()->peek('eccube.front.request.error'); |
| $cnt = $this->session->getFlashBag()->peek('eccube.front.request.error'); |
| end($cnt); |
| end($cnt); |
| $key = key($cnt); |
| $key = key($cnt); |
| // エラーと同じキー商品名を設定する |
| // エラーと同じキー商品名を設定する |
| $arrProduct[$key] = $productName; |
| $arrProduct[$key] = $productName; |
| $this->session->getFlashBag()->set('eccube.front.request.product', $arrProduct); |
| $this->session->getFlashBag()->set('eccube.front.request.product', $arrProduct); |
| } |
| } |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $productClassId |
| * @param string $productClassId |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function upProductQuantity($productClassId) |
| public function upProductQuantity($productClassId) |
| { |
| { |
| $quantity = $this->getProductQuantity($productClassId) + 1; |
| $quantity = $this->getProductQuantity($productClassId) + 1; |
| $this->setProductQuantity($productClassId, $quantity); |
| $this->setProductQuantity($productClassId, $quantity); |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $productClassId |
| * @param string $productClassId |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function downProductQuantity($productClassId) |
| public function downProductQuantity($productClassId) |
| { |
| { |
| $quantity = $this->getProductQuantity($productClassId) - 1; |
| $quantity = $this->getProductQuantity($productClassId) - 1; |
| if ($quantity > 0) { |
| if ($quantity > 0) { |
| $this->setProductQuantity($productClassId, $quantity); |
| $this->setProductQuantity($productClassId, $quantity); |
| } |
| } |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return array |
| * @return array |
| */ |
| */ |
| public function getProductTypes() |
| public function getProductTypes() |
| { |
| { |
| |
| |
| $productTypes = array(); |
| $productTypes = array(); |
| foreach ($this->getCart()->getCartItems() as $item) { |
| foreach ($this->getCart()->getCartItems() as $item) { |
| /* @var $ProductClass \Eccube\Entity\ProductClass */ |
| /* @var $ProductClass \Eccube\Entity\ProductClass */ |
| $ProductClass = $item->getObject(); |
| $ProductClass = $item->getObject(); |
| $productTypes[] = $ProductClass->getProductType(); |
| $productTypes[] = $ProductClass->getProductType(); |
| } |
| } |
| |
| |
| return array_unique($productTypes); |
| return array_unique($productTypes); |
| |
| |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return string[] |
| * @return string[] |
| */ |
| */ |
| public function getErrors() |
| public function getErrors() |
| { |
| { |
| return $this->errors; |
| return $this->errors; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return string[] |
| * @return string[] |
| */ |
| */ |
| public function getMessages() |
| public function getMessages() |
| { |
| { |
| return $this->messages; |
| return $this->messages; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $message |
| * @param string $message |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function setMessage($message) |
| public function setMessage($message) |
| { |
| { |
| $this->messages[] = $message; |
| $this->messages[] = $message; |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @return string |
| * @return string |
| */ |
| */ |
| public function getError() |
| public function getError() |
| { |
| { |
| return $this->error; |
| return $this->error; |
| } |
| } |
| |
| |
| /** |
| /** |
| * @param string $error |
| * @param string $error |
| * @return \Eccube\Service\CartService |
| * @return \Eccube\Service\CartService |
| */ |
| */ |
| public function setError($error = null) |
| public function setError($error = null) |
| { |
| { |
| $this->error = $error; |
| $this->error = $error; |
| $this->session->getFlashBag()->set('eccube.front.request.error', $error); |
| $this->session->getFlashBag()->set('eccube.front.request.error', $error); |
| |
| |
| return $this; |
| return $this; |
| } |
| } |
| |
| |
| /** |
| /** |
| * 商品名を取得 |
| * 商品名を取得 |
| * |
| * |
| * @param ProductClass $ProductClass |
| * @param ProductClass $ProductClass |
| * @return string |
| * @return string |
| */ |
| */ |
| private function getProductName(ProductClass $ProductClass) |
| private function getProductName(ProductClass $ProductClass) |
| { |
| { |
| |
| |
| $productName = $ProductClass->getProduct()->getName(); |
| $productName = $ProductClass->getProduct()->getName(); |
| |
| |
| if ($ProductClass->hasClassCategory1()) { |
| if ($ProductClass->hasClassCategory1()) { |
| $productName .= " - ".$ProductClass->getClassCategory1()->getName(); |
| $productName .= " - ".$ProductClass->getClassCategory1()->getName(); |
| } |
| } |
| |
| |
| if ($ProductClass->hasClassCategory2()) { |
| if ($ProductClass->hasClassCategory2()) { |
| $productName .= " - ".$ProductClass->getClassCategory2()->getName(); |
| $productName .= " - ".$ProductClass->getClassCategory2()->getName(); |
| } |
| } |
| |
| |
| return $productName; |
| return $productName; |
| } |
| } |
| |
| |
| |
| |
| /** |
| /** |
| * 非公開商品の場合、カートから削除 |
| * 非公開商品の場合、カートから削除 |
| * |
| * |
| * @param ProductClass $ProductClass |
| * @param ProductClass $ProductClass |
| * @return bool |
| * @return bool |
| */ |
| */ |
| private function isProductDisplay(ProductClass $ProductClass) |
| private function isProductDisplay(ProductClass $ProductClass) |
| { |
| { |
| |
| |
| if ($ProductClass->getProduct()->getStatus()->getId() !== Disp::DISPLAY_SHOW) { |
| if ($ProductClass->getProduct()->getStatus()->getId() !== Disp::DISPLAY_SHOW) { |
| // 非公開の商品はカートから削除 |
| // 非公開の商品はカートから削除 |
| $this->removeProduct($ProductClass->getId()); |
| $this->removeProduct($ProductClass->getId()); |
| |
| |
| return false; |
| return false; |
| } |
| } |
| |
| |
| return true; |
| return true; |
| } |
| } |
| |
| |
| |
| |
| /** |
| /** |
| * 在庫数と販売制限数のチェック |
| * 在庫数と販売制限数のチェック |
| * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
| * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
| * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
| * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
| * |
| * |
| * @param ProductClass $ProductClass |
| * @param ProductClass $ProductClass |
| * @param $productName |
| * @param $productName |
| * @param $quantity |
| * @param $quantity |
| * @return int チェック後に更新した個数 |
| * @return int チェック後に更新した個数 |
| */ |
| */ |
| private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
| private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
| { |
| { |
| |
| |
| /** |
| /** |
| * 実際の在庫は ProductClass::ProductStock だが、購入時にロックがかかるため、 |
| * 実際の在庫は ProductClass::ProductStock だが、購入時にロックがかかるため、 |
| * ここでは ProductClass::stock で在庫のチェックをする |
| * ここでは ProductClass::stock で在庫のチェックをする |
| */ |
| */ |
| |
| |
| // 在庫数(在庫無制限の場合、null) |
| // 在庫数(在庫無制限の場合、null) |
| $stock = $ProductClass->getStock(); |
| $stock = $ProductClass->getStock(); |
| // 在庫無制限(在庫無制限の場合、1) |
| // 在庫無制限(在庫無制限の場合、1) |
| $stockUnlimited = $ProductClass->getStockUnlimited(); |
| $stockUnlimited = $ProductClass->getStockUnlimited(); |
| |
| |
| // 販売制限数(設定されていなければnull) |
| // 販売制限数(設定されていなければnull) |
| $saleLimit = $ProductClass->getSaleLimit(); |
| $saleLimit = $ProductClass->getSaleLimit(); |
| |
| |
| if ($stockUnlimited) { |
| if ($stockUnlimited) { |
| // 在庫無制限 |
| // 在庫無制限 |
| |
| |
| if ($saleLimit && $saleLimit < $quantity) { |
| if ($saleLimit && $saleLimit < $quantity) { |
| // 販売制限数を超えていれば販売制限数をセット |
| // 販売制限数を超えていれば販売制限数をセット |
| $this->addError('cart.over.sale_limit', $productName); |
| $this->addError('cart.over.sale_limit', $productName); |
| |
| |
| return $saleLimit; |
| return $saleLimit; |
| } |
| } |
| } else { |
| } else { |
| // 在庫制限あり |
| // 在庫制限あり |
| |
| |
| if ($stock < 1) { |
| if ($stock < 1) { |
| // 在庫がなければカートから削除 |
| // 在庫がなければカートから削除 |
| $this->addError('cart.zero.stock', $productName); |
| $this->addError('cart.zero.stock', $productName); |
| $this->removeProduct($ProductClass->getId()); |
| $this->removeProduct($ProductClass->getId()); |
| |
| |
| return 0; |
| return 0; |
| } else { |
| } else { |
| // 在庫数チェックと販売制限数チェックどちらを適用するか設定 |
| // 在庫数チェックと販売制限数チェックどちらを適用するか設定 |
| $message = 'cart.over.stock'; |
| $message = 'cart.over.stock'; |
| if ($saleLimit) { |
| if ($saleLimit) { |
| if ($stock > $saleLimit) { |
| if ($stock > $saleLimit) { |
| // 販売制限数チェック |
| // 販売制限数チェック |
| $limit = $saleLimit; |
| $limit = $saleLimit; |
| $message = 'cart.over.sale_limit'; |
| $message = 'cart.over.sale_limit'; |
| } else { |
| } else { |
| // 在庫数チェック |
| // 在庫数チェック |
| $limit = $stock; |
| $limit = $stock; |
| } |
| } |
| } else { |
| } else { |
| // 在庫数チェック |
| // 在庫数チェック |
| $limit = $stock; |
| $limit = $stock; |
| } |
| } |
| |
| |
| if ($limit < $quantity) { |
| if ($limit < $quantity) { |
| // 在庫数、販売制限数を超えていれば購入可能数までをセット |
| // 在庫数、販売制限数を超えていれば購入可能数までをセット |
| $this->addError($message, $productName); |
| $this->addError($message, $productName); |
| |
| |
| return $limit; |
| return $limit; |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| return $quantity; |
| return $quantity; |
| } |
| } |
| |
| |
| } |
| } |
| |
| |