示例#1
0
 /**
  * Model definition.
  */
 function define()
 {
     $this->auto_increment_start = 10000;
     // Fields.
     $this->fields = array('id', 'items', 'order', 'coupon_code', 'discounts', 'taxes', 'order_id', 'account_id', 'session_id', 'date_created', 'date_updated', 'items' => function ($cart) {
         return get("/products", array(':with' => $cart['items']));
     }, 'account' => function ($cart) {
         return get("/accounts/{$cart['account_id']}");
     }, 'shipping_methods' => function ($cart) {
         return Carts::get_shipping_methods($cart);
     }, 'taxes' => function ($cart) {
         return Carts::get_taxes($cart);
     }, 'sub_total' => function ($cart) {
         return Carts::get_sub_total($cart);
     }, 'sub_discount' => function ($cart) {
         return Carts::get_sub_total($cart, array('discount' => true));
     }, 'shipping_total' => function ($cart) {
         return Carts::get_shipping_total($cart);
     }, 'shipping_discount' => function ($cart) {
         return Carts::get_shipping_total($cart, array('discount' => true));
     }, 'tax_total' => function ($cart) {
         return Carts::get_tax_total($cart);
     }, 'discount_total' => function ($cart) {
         return Carts::get_discount_total($cart);
     }, 'grand_total' => function ($cart) {
         return Carts::get_grand_total($cart);
     }, 'credit_total' => function ($cart) {
         return Carts::get_credit_total($cart);
     }, 'billing_total' => function ($cart) {
         return Carts::get_billing_total($cart);
     }, 'product_cost' => function ($cart) {
         return Carts::get_sub_total($cart, array('cost' => true));
     }, 'quantity' => function ($cart) {
         return Carts::get_quantity($cart);
     }, 'weight' => function ($cart) {
         return Carts::get_weight($cart);
     }, 'abandoned' => function ($cart) {
         return Carts::abandoned($cart);
     });
     // Search fields.
     $this->search_fields = array('id', 'order.name', 'order.email');
     // Indexes.
     $this->indexes = array('id' => 'unique');
     // Validate.
     $this->validate = array('order', ':items' => array('required' => array('id', 'price', 'quantity')));
     // Cart item quantity limit.
     $this->item_quantity_limit = 99999999;
     // Event binds.
     $this->binds = array('GET' => function ($event) {
         $data =& $event['data'];
         // Reset cart session?
         if ($data[':reset']) {
             $data['items'] = null;
             $data['order'] = null;
             $data['discounts'] = null;
             return put("/carts/{$event['id']}", $data);
         }
     }, 'POST' => function ($event) {
         $data =& $event['data'];
         // Post item into cart (shortcut to POST.items)
         if ($event['id'] && $data['item']) {
             post("/carts/{$event['id']}/items", $data['item']);
             return get("/carts/{$event['id']}");
         }
     }, 'POST.items' => function ($event) {
         $data =& $event['data'];
         // Filter the item for update.
         $data = array_pop(Orders::get_items_for_update(array($data)));
         if ($cart = get("/carts/{$event['id']}")) {
             $product = get("/products/{$data['id']}", array('pricing' => array('roles' => $cart['account']['roles'], 'quantity' => $data['quantity'] ?: 1)));
             // Item has options?
             if ($data['options']) {
                 foreach ((array) $data['options'] as $key => $option) {
                     if (is_scalar($option)) {
                         $data['options'][$key] = $option = array('name' => $option);
                     }
                     // Matches product option?
                     if (is_array($product['options']) && is_array($product['options'][$key])) {
                         $data['options'][$key]['name'] = $option['name'] ?: $product['options'][$key]['name'];
                         $data['options'][$key]['price'] = $option['price'] ?: $product['options'][$key]['price'];
                     }
                 }
             }
             // Item variant?
             if ($product['variants']) {
                 $vid = $data['variant_id'];
                 if ($variant = $product['variants'][$vid]) {
                     $data['variant'] = $variant;
                     $data['price'] = $data['price'] ?: $variant['price'];
                 } else {
                     // Variant not found.
                     $model->error('not found', 'variant_id');
                 }
             } else {
                 $data['variant'] = null;
                 $data['variant_id'] = null;
             }
             // Prevent two of the same item in cart.
             foreach ((array) $cart['items'] as $key => $item) {
                 // Exists in cart?
                 if ($data['id'] == $item['id'] && $data['options'] == $item['options'] && $data['variant'] == $item['variant']) {
                     // Update quantity.
                     $item['quantity'] += $data['quantity'];
                     return put("{$cart}/items/{$key}", $item);
                 }
             }
         }
         // Defaults.
         $data['price'] = $data['price'] ?: $product['price'];
         $data['quantity'] = round($data['quantity'] ?: 1);
     }, 'PUT.items' => function ($event, $model) {
         $data =& $event['data'];
         // Defaults.
         if (isset($data['quantity'])) {
             $data['quantity'] = round($data['quantity'] ?: 1);
             // Upper limit on item quantity.
             if ($data['quantity'] > $model->item_quantity_limit) {
                 $data['quantity'] = $model->item_quantity_limit;
             }
         }
     }, 'PUT' => function ($event, $model) {
         $data =& $event['data'];
         // Cart already exists?
         if ($data && ($cart = get("/carts/{$event['id']}"))) {
             // Update items collection?
             if ($data['items']) {
                 $cart_items = $cart['items'];
                 foreach ((array) $data['items'] as $item_id => $item) {
                     // Update existing item quantity.
                     if (isset($item['quantity']) && $cart_items[$item_id]) {
                         $cart_items[$item_id]['quantity'] = round($item['quantity']);
                         // Upper limit on item quantity.
                         if ($cart_items[$item_id]['quantity'] > $model->item_quantity_limit) {
                             $cart_items[$item_id]['quantity'] = $model->item_quantity_limit;
                         }
                         // Check product for pricing update?
                         $product = get("/products/{$cart_items[$item_id]['id']}", array('pricing' => array('roles' => $cart['account']['roles'], 'quantity' => $cart_items[$item_id]['quantity'] ?: 1)));
                         // Update pricing?
                         if ($product['pricing']) {
                             $cart_items[$item_id]['price'] = $product['price'];
                         }
                     }
                     // Remove item?
                     if ($data['items'][$item_id]['quantity'] <= 0) {
                         unset($cart_items[$item_id]);
                     }
                 }
                 $data['items'] = $cart_items;
             }
             // Removed coupon code?
             if (isset($data['coupon_code']) && !$data['coupon_code']) {
                 // Remove coupon.
                 $data['discounts'] = $cart['discounts'];
                 unset($data['discounts']['coupon']);
             } elseif ($data['coupon_code'] && $data['coupon_code'] != $cart['discount']['coupon']['code']) {
                 $discount = get("/discounts", array('code' => $data['coupon_code'], 'is_valid' => true));
                 if ($discount === false) {
                     $model->error('already used', 'coupon_code');
                 } else {
                     if (!$discount) {
                         $model->error('invalid', 'coupon_code');
                     }
                 }
                 // Remember code.
                 $data['coupon_code'] = $discount['code'];
                 // Update items from coupon.
                 foreach ((array) $discount['rules'] as $rule) {
                     if ($rule['add'] && $rule['product_id']) {
                         // Exists in cart?
                         $exists = false;
                         foreach ((array) $cart['items'] as $item_id => $item) {
                             if ($item['id'] == $rule['product_id']) {
                                 $exists = $item_id;
                                 break;
                             }
                         }
                         // Update item quantity?
                         if ($exists) {
                             put("{$cart}/items/{$exists}", array('quantity' => $rule['quantity']));
                         } else {
                             // Post new item to cart.
                             post("{$cart}/items", array('id' => $rule['product_id'], 'quantity' => $rule['quantity']));
                         }
                     }
                 }
                 $data['discounts'] = $cart['discounts'];
                 $data['discounts']['coupon'] = $discount;
             } else {
                 // Don't update coupon code directly.
                 unset($data['coupon_code']);
             }
             // Extra discount updates.
             if ($data['discounts']) {
                 foreach ((array) $data['discounts'] as $did => $discount) {
                     // Unset some discount details.
                     unset($data['discounts'][$did]['codes']);
                     unset($data['discounts'][$did]['codes_used']);
                     unset($data['discounts'][$did]['code_history']);
                     unset($data['discounts'][$did]['conditions']);
                 }
             }
             // Update order data? Merge.
             if (isset($data['order']) && is_array($cart['order'])) {
                 if ($data['order']) {
                     if ($data['order']['shipping']) {
                         $data['order']['shipping'] = array_merge((array) $cart['order']['shipping'], (array) $data['order']['shipping']);
                     }
                     $data['order'] = $cart['order'] = array_merge($cart['order'], (array) $data['order']);
                 }
             }
             // Update shipping total?
             if ($data['order'] || $data['items']) {
                 // @TODO: Make sure we don't need this.
                 //$data['shipping_total'] = Carts::get_shipping_total($cart);
             }
             // Use credit?
             if ($data['credit_total']) {
                 // Validate.
                 $data['credit_total'] = Carts::get_credit_total(merge($cart, $data));
             }
         }
     }, 'validate:order' => function ($order, $field, $params, $model) {
         // Validate with orders collection.
         $order[':validate'] = true;
         $result = get("/orders", $order);
         // Apply errors to cart model?
         foreach ((array) $result['errors'] as $field => $error) {
             $model->error($error, 'order', $field);
         }
     });
 }