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; } }
$discountB->setId(2)->setName('Buy 2 ToothBrush, Get 1 ToothPaste free')->setTo(Cart\Discount::$toSpecified)->setValue('1.00')->setMaxQty(1)->setIsMaxPerItem(false)->setAs(Cart\Discount::$asPercent)->setIsPreTax(true)->setPreConditionCompare($compare3)->setTargetConditionCompare($compare2); //apply the automatic discount, if pre-conditions validate if ($compare1->isValid($shipmentA)) { $discountA->setShipment($shipmentA); $cart->setDiscount($discountA); } //check pre-conditions and target conditions if ($compare3->isValid($itemA) && $compare2->isValid($itemB)) { $discountB->setItem($itemB); $cart->setDiscount($discountB); } echo print_r($cart->getTotals(), 1); echo "==============" . PHP_EOL; echo print_r($cart->getDiscountedTotals(), 1); echo print_r($cart->getDiscountGrid()); $cart2 = new Cart\Cart(); $cart2->importJson($cart->toJson()); echo "\n{$cart2}\n"; echo print_r($cart2->getTotals(), 1); echo print_r($cart2->getDiscountedTotals(), 1); /** * @backupGlobals disabled */ class testSample extends PHPUnit_Framework_TestCase { public function testDefault() { $expected = true; $actual = false; $this->assertEquals($expected, $actual); }