本文整理汇总了PHP中ShoppingCart::create方法的典型用法代码示例。如果您正苦于以下问题:PHP ShoppingCart::create方法的具体用法?PHP ShoppingCart::create怎么用?PHP ShoppingCart::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ShoppingCart
的用法示例。
在下文中一共展示了ShoppingCart::create方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: singleton
/**
* Allows access to the cart from anywhere in code.
* @return ShoppingCart Object
*/
public static function singleton()
{
if (!self::$singletoncart) {
self::$singletoncart = ShoppingCart::create();
}
return self::$singletoncart;
}
示例2: doAddItemToCart
public function doAddItemToCart($data)
{
$product = Product::get()->byID($data['ProductID']);
$customisations = array();
foreach ($data as $key => $value) {
if (!(strpos($key, 'customise') === false) && $value) {
$custom_data = explode("_", $key);
if ($custom_item = ProductCustomisation::get()->byID($custom_data[1])) {
$modify_price = 0;
// Check if the current selected option has a price modification
if ($custom_item->Options()->exists()) {
$option = $custom_item->Options()->filter("Title", $value)->first();
$modify_price = $option ? $option->ModifyPrice : 0;
}
$customisations[] = array("Title" => $custom_item->Title, "Value" => $value, "ModifyPrice" => $modify_price);
}
}
}
if ($product) {
$cart = ShoppingCart::create();
$cart->add($product, $data['Quantity'], $customisations);
$cart->save();
// Clear any postage data that has been set
Session::clear("Commerce.PostageID");
$message = _t('Commerce.AddedItemToCart', 'Added item to your shopping cart');
$message .= ' <a href="' . $cart->Link() . '">';
$message .= _t('Commerce.ViewCart', 'View cart');
$message .= '</a>';
$this->controller->setSessionMessage("success", $message);
}
return $this->controller->redirectBack();
}
示例3: init
public function init()
{
parent::init();
// If no shopping cart doesn't exist, redirect to base
if (!ShoppingCart::create()->getItems()->exists()) {
return $this->redirect(ShoppingCart::config()->url_segment);
}
}
示例4: get
/**
* Shortcut for ShoppingCart::create, exists because create()
* doesn't seem quite right.
*
* @return ShoppingCart
*/
public static function get()
{
return ShoppingCart::create();
}
示例5: getCommerceCart
/**
* Return a the current shopping cart
*
* @return ShoppingCart
*/
public function getCommerceCart()
{
return ShoppingCart::create();
}
示例6: getPostageAreas
/**
* Function to find relevent postage rates, based on supplied country and
* zip/postal code data.
*
* We make this a function as part of Commerce Controller, as there are
* several points in the payment/order process that will make use of this
* functionality
*
* @param $country String listing the country to search, this has to be an ISO 3166 code
* @param $zipcode String listing the zip/postage code to filter by
*/
public function getPostageAreas($country, $zipcode)
{
$return = new ArrayList();
$countries = new ArrayList();
$cart = ShoppingCart::create();
$all_rates = $this->SiteConfig()->PostageAreas();
// First find all area's for this country directly (no wildcards)
foreach ($all_rates as $rate) {
if (!(strpos(strtolower($rate->Country), strtolower($country)) === false)) {
$countries->add($rate);
}
}
// If we have no countries in the list specificly, then check for wildcards
if (!$countries->exists()) {
foreach ($all_rates as $rate) {
if ($rate->Country == "*") {
$countries->add($rate);
}
}
}
// If we have a list of countries check them for post codes
foreach ($countries as $rate) {
$rate_codes = explode(",", $rate->ZipCode);
foreach ($rate_codes as $rate_to_check) {
$curr_length = strlen($rate_to_check);
if (strtolower(substr($zipcode, 0, $curr_length)) == strtolower($rate_to_check)) {
$return->add($rate);
}
}
}
// If we still don't have anything to return, check or list of countries
// for a wildcard
if (!$return->exists()) {
foreach ($countries as $rate) {
if ($rate->ZipCode == "*") {
$return->add($rate);
}
}
}
// Now we have a list of locations, start checking for additional
// rules an remove if not applicable.
$total_cost = str_replace(",", "", $cart->SubTotalCost());
$total_weight = str_replace(",", "", $cart->TotalWeight());
$total_items = str_replace(",", "", $cart->TotalItems());
$max_cost = 0;
$max_weight = 0;
$max_items = 0;
// First loop through and find items that are invalid
foreach ($return as $location) {
if ($location->Calculation == "Price" && (double) $total_cost < $location->Unit) {
$return->remove($location);
}
if ($location->Calculation == "Weight" && (double) $total_weight < $location->Unit) {
$return->remove($location);
}
if ($location->Calculation == "Items" && (double) $total_items < $location->Unit) {
$return->remove($location);
}
}
// Now find max values based on units
foreach ($return as $location) {
if ($location->Calculation == "Price" && $location->Unit > $max_cost) {
$max_cost = $location->Unit;
}
if ($location->Calculation == "Weight" && $location->Unit > $max_weight) {
$max_weight = $location->Unit;
}
if ($location->Calculation == "Items" && $location->Unit > $max_items) {
$max_items = $location->Unit;
}
}
// Now loop through again and calculate which brackets each
// Location fits in
foreach ($return as $location) {
if ($location->Calculation == "Price" && $location->Unit < $max_cost) {
$return->remove($location);
}
if ($location->Calculation == "Weight" && $location->Unit < $max_weight) {
$return->remove($location);
}
if ($location->Calculation == "Items" && $location->Unit < $max_items) {
$return->remove($location);
}
}
return $return;
}