public function insertItemAction($productId = 0, $providerId = 0) { $productId = intval($productId); $providerId = intval($providerId); $productId = $productId > 0 ? $productId : $this->request->getPost("product_id", "int"); $providerId = $providerId > 0 ? $providerId : $this->request->getPost("provider_id", "int"); if ($productId < 1 || $providerId < 1) { $this->flashJson(500, array(), "非法请求"); exit; } $qty = $this->request->getPost("qty", "int"); if ($qty < 1) { $qty = 1; } $product = ProductModel::findFirst($productId); if (empty($product)) { $this->flashJson(500, array(), '抱歉,该商品不存在'); exit; } $provider = ProviderModel::findFirst("user_id={$providerId} AND product_id={$productId}"); if (empty($provider)) { $this->flashJson(500, array(), '非法请求'); exit; } $displayCart = self::getCart(); if (isset($displayCart[$providerId])) { $cart = $displayCart[$providerId]; } else { $cart = new Cart\Cart(); } if ($cart->hasItem($productId, false)) { $item = $cart->getItem($productId, false); $item->setQty($qty); } else { $item = new Cart\Item(); $item->setId($productId)->setProvider($providerId)->setQty($qty)->setName($product->name)->setCustom("image_url", $product->image_url)->setSku('')->setPrice($provider->price)->setIsTaxable(true)->setIsDiscountable(true); $cart->setItem($item); } $displayCart[$providerId] = $cart; self::putCart($displayCart); if ($this->request->isAjax()) { $ajaxRet = $item->toArray(); $ajaxRet['totals'] = $cart->getTotals(); $this->flashJson(200, $ajaxRet); exit; } else { $this->response->redirect('cart/')->sendHeaders(); exit; } }
require_once __DIR__ . "/bootstrap/cli.php"; registerTask('sample'); use BullSoft\Cart; //create toothbrush $itemA = new Cart\Item(); $itemA->setId(1)->setName('ToothBrush')->setSku('toothbrush-1')->setPrice('1.99')->setQty(3)->setIsTaxable(true)->setIsDiscountable(true); //create toothpaste $itemB = new Cart\Item(); $itemB->setId(2)->setName('ToothPaste')->setSku('toothpaste-2')->setPrice('2.99')->setQty(1)->setIsTaxable(true)->setIsDiscountable(true); $itemC = new Cart\Item(); $itemC->setId(3)->setName('Anniversary Present')->setSku('present-1')->setPrice('99.99')->setQty(1)->setIsTaxable(true)->setIsDiscountable(true); //create a shipment $shipmentA = new Cart\Shipment(); $shipmentA->setId(1)->setVendor('ups')->setMethod('ground')->setPrice('6.95')->setIsDiscountable(true)->setIsTaxable(true); $cart = new Cart\Cart(); $cart->setItem($itemA)->setItem($itemB)->setItem($itemC)->setShipment($shipmentA)->setIncludeTax(true)->setTaxRate('0.07025')->setId(3); //set up a single discount condition, for the shipping method $condition1 = new Cart\DiscountCondition(); $condition1->setName('Shipping: code = ups_ground')->setCompareType(Cart\DiscountCondition::$compareEquals)->setCompareValue('ups_ground')->setSourceEntityType('shipments')->setSourceEntityField('code'); //discount conditions are wrapped by a condition compare object //compare objects are intended for creating trees of conditionals $compare1 = new Cart\DiscountConditionCompare(); $compare1->setOp('or')->setSourceEntityType('shipments')->addCondition($condition1); //set up a single discount condition, for the item sku $condition2 = new Cart\DiscountCondition(); $condition2->setName('Item: sku = toothpaste-2')->setSourceEntityType('items')->setSourceEntityField('sku')->setCompareType(Cart\DiscountCondition::$compareEquals)->setCompareValue('toothpaste-2'); $condition3 = new Cart\DiscountCondition(); $condition3->setName('Item: qty >= 2')->setSourceEntityType('items')->setSourceEntityField('qty')->setCompareType(Cart\DiscountCondition::$compareGreaterThanEquals)->setCompareValue('2'); //set up a single discount condition, for the item sku $condition4 = new Cart\DiscountCondition(); $condition4->setName('Item: sku = toothbrush-1')->setSourceEntityType('items')->setSourceEntityField('sku')->setCompareType(Cart\DiscountCondition::$compareEquals)->setCompareValue('toothbrush-1');