'A standardized way to create these objects, with sensible defaults filled in.
Will update the cost if called on an order that already carries the course.
Returns the order item'

@classmethod
@transaction.atomic
def add_to_order(cls, order, course_id, qty, mode_slug=CourseMode.DEFAULT_SHOPPINGCART_MODE_SLUG, cost=None, currency=None):

    course = modulestore().get_course(course_id)
    if (not course):
        log.error('User    {}    tried    to    add    non-existent    course    {}    to    cart    id    {}'.format(order.user.email, course_id, order.id))
        raise CourseDoesNotExistException
    if cls.contained_in_order(order, course_id):
        log.warning('User    {}    tried    to    add    PaidCourseRegistration    for    course    {},    already    in    cart    id    {}'.format(order.user.email, course_id, order.id))
        raise ItemAlreadyInCartException
    if CourseEnrollment.is_enrolled(user=order.user, course_key=course_id):
        log.warning('User    {}    trying    to    add    course    {}    to    cart    id    {},    already    registered'.format(order.user.email, course_id, order.id))
        raise AlreadyEnrolledInCourseException
    course_mode = CourseMode.mode_for_course(course_id, mode_slug)
    if (not course_mode):
        course_mode = CourseMode.DEFAULT_SHOPPINGCART_MODE
    if (not cost):
        cost = course_mode.min_price
    if (not currency):
        currency = course_mode.currency
    super(CourseRegCodeItem, cls).add_to_order(order, course_id, cost, currency=currency)
    (item, created) = cls.objects.get_or_create(order=order, user=order.user, course_id=course_id)
    item.status = order.status
    item.mode = course_mode.slug
    item.unit_cost = cost
    item.list_price = cost
    item.qty = qty
    item.line_desc = _(u'Enrollment    codes    for    Course:    {course_name}').format(course_name=course.display_name_with_default_escaped)
    item.currency = currency
    order.currency = currency
    item.report_comments = item.csv_report_comments
    order.save()
    item.save()
    log.info('User    {}    added    course    registration    {}    to    cart:    order    {}'.format(order.user.email, course_id, order.id))
    return item
