public function setProductQuantity($ProductClass, $quantity)
{
if (!$ProductClass instanceof ProductClass) {
$ProductClass = $this->entityManager->getRepository('Eccube\\Entity\\ProductClass')->find($ProductClass);
if (!$ProductClass) {
throw new CartException('cart.product.delete');
}
}
if ($ProductClass->getProduct()->getStatus()->getId() !== Disp::DISPLAY_SHOW) {
$this->removeProduct($ProductClass->getId());
throw new CartException('cart.product.not.status');
}
$productName = $ProductClass->getProduct()->getName();
if ($ProductClass->hasClassCategory1()) {
$productName .= " - " . $ProductClass->getClassCategory1()->getName();
}
if ($ProductClass->hasClassCategory2()) {
$productName .= " - " . $ProductClass->getClassCategory2()->getName();
}
// 商品種別に紐づく配送業者を取得
$deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType());
if (count($deliveries) == 0) {
// 商品種別が存在しなければエラー
$this->removeProduct($ProductClass->getId());
$this->addError('cart.product.not.producttype', $productName);
throw new CartException('cart.product.not.producttype');
}
$this->setCanAddProductType($ProductClass->getProductType());
if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) {
if (!$this->canAddProduct($ProductClass->getId())) {
// 複数配送対応でなければ商品種別が異なればエラー
throw new CartException('cart.product.type.kind');
}
} else {
// 複数配送の場合、同一支払方法がなければエラー
if (!$this->canAddProductPayment($ProductClass->getProductType())) {
throw new CartException('cart.product.payment.kind');
}
}
$tmp_subtotal = 0;
$tmp_quantity = 0;
foreach ($this->getCart()->getCartItems() as $cartitem) {
$pc = $cartitem->getObject();
if ($pc->getId() != $ProductClass->getId()) {
// まず、追加された商品以外のtotal priceをセット
$tmp_subtotal += $cartitem->getTotalPrice();
}
}
for ($i = 0; $i < $quantity; $i++) {
$tmp_subtotal += $ProductClass->getPrice02IncTax();
if ($tmp_subtotal > $this->app['config']['max_total_fee']) {
$this->setError('cart.over.price_limit');
break;
}
$tmp_quantity++;
}
$quantity = $tmp_quantity;
$tmp_quantity = 0;
/*
* 実際の在庫は ProductClass::ProductStock だが、購入時にロックがかかるため、
* ここでは ProductClass::stock で在庫のチェックをする
*/
if (!$ProductClass->getStockUnlimited() && $quantity > $ProductClass->getStock()) {
if ($ProductClass->getSaleLimit() && $ProductClass->getStock() > $ProductClass->getSaleLimit()) {
$tmp_quantity = $ProductClass->getSaleLimit();
$this->addError('cart.over.sale_limit', $productName);
} else {
$tmp_quantity = $ProductClass->getStock();
$this->addError('cart.over.stock', $productName);
}
}
if ($ProductClass->getSaleLimit() && $quantity > $ProductClass->getSaleLimit()) {
$tmp_quantity = $ProductClass->getSaleLimit();
$this->addError('cart.over.sale_limit', $productName);
}
if ($tmp_quantity) {
$quantity = $tmp_quantity;
}
$CartItem = new CartItem();
$CartItem->setClassName('Eccube\\Entity\\ProductClass')->setClassId((string) $ProductClass->getId())->setPrice($ProductClass->getPrice02IncTax())->setQuantity($quantity);
$this->cart->setCartItem($CartItem);
return $this;
}