/** * _adjust_line_item_quantity * * @access protected * @param EE_Line_Item $line_item * @param int $quantity * @param string $action * @return EE_Line_Item|null */ protected function adjust_line_item_quantity($line_item, $quantity = 1, $action = 'add') { if ($line_item instanceof EE_Line_Item) { //EEH_Debug_Tools::printr( $line_item->code(), '$line_item->code()', __FILE__, __LINE__ ); //EEH_Debug_Tools::printr( $line_item->type(), '$line_item->type()', __FILE__, __LINE__ ); //EEH_Debug_Tools::printr( $line_item->OBJ_type(), '$line_item->OBJ_type()', __FILE__, __LINE__ ); //EEH_Debug_Tools::printr( $line_item->OBJ_ID(), '$line_item->OBJ_ID()', __FILE__, __LINE__ ); //EEH_Debug_Tools::printr( $quantity, '$quantity', __FILE__, __LINE__ ); //EEH_Debug_Tools::printr( $action, '$action', __FILE__, __LINE__ ); $quantity = (int) $quantity; if ($quantity === 0 && $action == 'update') { $_REQUEST['ticket'] = $line_item->OBJ_ID(); $_REQUEST['line_item'] = $line_item->code(); $this->delete_ticket(false); return $line_item; } else { if ($quantity > 0) { $additional = 'An additional'; $added_or_removed = 'added'; } else { $additional = 'A '; $added_or_removed = 'removed'; } } $quantity = $action == 'update' ? $quantity : $line_item->quantity() + $quantity; // update quantity $line_item->set_quantity($quantity); //it's "proper" to update the sub-line items quantities too, but core can actually fix it if we don't anyways if (method_exists('EEH_Line_Item', 'update_quantity')) { EEH_Line_Item::update_quantity($line_item, $quantity); } else { $line_item->set_quantity($quantity); } //EEH_Debug_Tools::printr( $line_item, '$line_item', __FILE__, __LINE__ ); $saved = $line_item->ID() ? $line_item->save() : $line_item->quantity() == $quantity; if ($saved) { if ($action != 'update') { $msg = sprintf(__('%1$s item was successfully %2$s for this event.', 'event_espresso'), $additional, $added_or_removed); } else { $msg = __('The quantities were successfully updated for this event.', 'event_espresso'); } // something got added if (apply_filters('FHEE__EED_Multi_Event_Registration__display_success_messages', false)) { EE_Error::add_success($msg, __FILE__, __FUNCTION__, __LINE__); } } else { if ($line_item->quantity() != $quantity) { // nothing added EE_Error::add_error(sprintf(__('%1$s item was not %2$s for this event. Please refresh the page and try it again.', 'event_espresso'), $additional, $added_or_removed), __FILE__, __FUNCTION__, __LINE__); return null; } } } return $line_item; }
/** * Updates the line item and its children's quantities to the specified number. * Does NOT save them or recalculate totals. * @param EE_Line_Item $line_item * @param int $new_quantity */ public static function update_quantity(EE_Line_Item $line_item, $new_quantity) { if (!$line_item->is_percent()) { $line_item->set_quantity($new_quantity); $line_item->set_total($line_item->unit_price() * $new_quantity); } foreach ($line_item->children() as $child) { if ($child->is_sub_line_item()) { EEH_Line_Item::update_quantity($child, $new_quantity); } } }