示例#1
0
 /**
  *	@process items for adding to cart
  *	@access public
  *	@param EE_Ticket $ticket
  *	@param int $qty
  *	@return TRUE on success, FALSE on fail
  */
 public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1)
 {
     $datetimes = $ticket->datetimes();
     $event_names = array();
     foreach ($datetimes as $datetime) {
         $event = $datetime->event();
         $event_names[$event->ID()] = $event->name();
     }
     $description_addition = " (For " . implode(", ", $event_names) . ")";
     $full_description = $ticket->description() . $description_addition;
     // add $ticket to cart
     $line_item = EE_Line_Item::new_instance(array('LIN_name' => $ticket->name(), 'LIN_desc' => $full_description, 'LIN_unit_price' => $ticket->price(), 'LIN_quantity' => $qty, 'LIN_is_taxable' => $ticket->taxable(), 'LIN_order' => count($this->_grand_total->children()), 'LIN_total' => $ticket->price() * $qty, 'LIN_type' => EEM_Line_Item::type_line_item, 'OBJ_ID' => $ticket->ID(), 'OBJ_type' => 'Ticket'));
     //now add the sub-line items
     $running_total_for_ticket = 0;
     foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
         $sign = $price->is_discount() ? -1 : 1;
         $price_total = $price->is_percent() ? $running_total_for_ticket * $price->amount() / 100 : $price->amount() * $qty;
         $sub_line_item = EE_Line_Item::new_instance(array('LIN_name' => $price->name(), 'LIN_desc' => $price->desc(), 'LIN_quantity' => $price->is_percent() ? null : $qty, 'LIN_is_taxable' => false, 'LIN_order' => $price->order(), 'LIN_total' => $sign * $price_total, 'LIN_type' => EEM_Line_Item::type_sub_line_item, 'OBJ_ID' => $price->ID(), 'OBJ_type' => 'Price'));
         if ($price->is_percent()) {
             $sub_line_item->set_percent($sign * $price->amount());
         } else {
             $sub_line_item->set_unit_price($sign * $price->amount());
         }
         $running_total_for_ticket += $price_total;
         $line_item->add_child_line_item($sub_line_item);
     }
     $this->_add_item($line_item);
     return $this->save_cart() ? TRUE : FALSE;
 }
 /**
  * Returns the new line item created by adding a purchase of the ticket
  * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
  * @param EE_Ticket $ticket
  * @param int $qty
  * @return EE_Line_Item
  */
 public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
 {
     $datetimes = $ticket->datetimes();
     $event_names = array();
     foreach ($datetimes as $datetime) {
         $event = $datetime->event();
         $event_names[$event->ID()] = $event->name();
     }
     $description_addition = sprintf(__(' (For %1$s)', 'event_espresso'), implode(", ", $event_names));
     $full_description = $ticket->description() . $description_addition;
     $items_subtotal = self::get_items_subtotal($total_line_item);
     // add $ticket to cart
     $line_item = EE_Line_Item::new_instance(array('LIN_name' => $ticket->name(), 'LIN_desc' => $full_description, 'LIN_unit_price' => $ticket->price(), 'LIN_quantity' => $qty, 'LIN_is_taxable' => $ticket->taxable(), 'LIN_order' => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0, 'LIN_total' => $ticket->price() * $qty, 'LIN_type' => EEM_Line_Item::type_line_item, 'OBJ_ID' => $ticket->ID(), 'OBJ_type' => 'Ticket'));
     //now add the sub-line items
     $running_total_for_ticket = 0;
     foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
         $sign = $price->is_discount() ? -1 : 1;
         $price_total = $price->is_percent() ? $running_total_for_ticket * $price->amount() / 100 : $price->amount() * $qty;
         $sub_line_item = EE_Line_Item::new_instance(array('LIN_name' => $price->name(), 'LIN_desc' => $price->desc(), 'LIN_quantity' => $price->is_percent() ? null : $qty, 'LIN_is_taxable' => false, 'LIN_order' => $price->order(), 'LIN_total' => $sign * $price_total, 'LIN_type' => EEM_Line_Item::type_sub_line_item, 'OBJ_ID' => $price->ID(), 'OBJ_type' => 'Price'));
         if ($price->is_percent()) {
             $sub_line_item->set_percent($sign * $price->amount());
         } else {
             $sub_line_item->set_unit_price($sign * $price->amount());
         }
         $running_total_for_ticket += $price_total;
         $line_item->add_child_line_item($sub_line_item);
     }
     return $line_item;
 }