shopp_add_order_line()

add a purchased line-item to an existing order.

shopp_add_order_line ( $order, $data )

@param int $order (required) the order id to add the line item to
@param array|Item $data data to create the free-form item from (see $item_fields for allowed data), or alternately an Item object
@return bool|Purchased item object - false on failure, new order line item on success.

Description

add a purchased line-item to an existing order.

In addition to adjusting the order subtotal, tax, and shipping, and order total figures, this function will also issue an invoiced order event to add to the invoiced total on the order.

Examples

Constructing a free-form item

You can add an item to the order that may or may not correspond to a product in the catalog. This gives you an additional measure of control to add an item, with full control over how it will appear on the order.

Add a free-form item not in the product catalog

// build a shipped item
$shipped_item = array(
    // product type for this line item
    // can be Shipped, Virtual, Download, Donation
    'type' => 'Shipped',

    // product id of line item, if applicable
    'product' => 0,

    // variant id of line item, if applicable
    'price' => 0,

    // name of item
    'name' => 'Clothing Item',

    // description of item
    'description' => 'This product does not exist in the product catalog',

    // string label of variant combination of this item
    'optionlabel' => 'Blue,Large',

    // quantity of items on this line
    'quantity' => 2,

    // unit price not including tax
    'unitprice' => 10.00,

    // unit tax
    'unittax' => 1.00,

    // total additional shipping cost added to the order total shipping
    'shipping' => 0.00,

    // line item total added to subtotal without tax
    // equal to unitprice * quantity
    'total' => 20.00,

    // array of key value pairs associated with the purchased item line
    'data' => array( 'test'=>'value' )
);

// Added shipped item to order 410
$ShippedItemPurchased = shopp_add_order_line( 410, $shipped_item);

Add a free-form item that exists in the product catalog

// Get the Variant object for My Album product
// This album has track variants, selecting the '02 My Favorite Song' option.
$option = array( 'Track' => '02 My Favorite Song' );
$Track2 = 
    shopp_product_variant(
        array(
            'product' => shopp_product( 'My Album', 'name' )->id,
            'option' => $option,
        ),
        'variant'
    );

// build the order line, and override what I want to make it a free download
$music_dl_item = array(
    // product type for this line item
    // can be Shipped, Virtual, Download, Donation
    'type' => 'Download',

    // product id of download
    'product' => $Track2->product,

    // variant id of line item
    'price' => $Track2->price,

    // download asset id of the line item
    'download' => $Track2->download->id,

    // download key string required to access this download
    // key is used in the download URL on the order
    // some sort of unique hash string recommended
    'dkey' => 'af8f2571bc7053d5af1436130192c77b',

    // sku - Stock Keeping Unit if applicable
    'sku' => $Track2->sku,

    // name of item
    'name' => 'My Album',

    // description of item
    'description' => 'My greatest hits',

    // string label of variant combination of this item
    'optionlabel' => $Track2->label,

    // variation = the variant option selected
    'variation' = $option,

    // quantity of items on this line
    'quantity' => 1,

    // unit price not including tax
    'unitprice' => 0.00,

    // unit tax
    'unittax' => 0.00,

    // line item total added to subtotal without tax
    // equal to unitprice * quantity
    'total' => 0.00,
);

// Added downloadable item to order 410
$DownloadItemPurchased = shopp_add_order_line( 410, $shipped_item);

Add an line item from an Item object

If you just need to add a product or product variant to the order, you can also pass an Item object.

Add a product variant to an existing order

// Get the Variant object for My Album product
// This album has track variants, selecting the '02 My Favorite Song' option.
$option = array( 'Track' => '02 My Favorite Song' );
$Track2 = 
    shopp_product_variant(
        array(
            'product' => shopp_product( 'My Album', 'name' )->id,
            'option' => $option,
        ),
        'variant'
    );

// convert a product variant to an item
$Item = shopp_product_variant_to_item ( $Track2 );

// add these line items to order 410
$PurchasedItem = shopp_add_order_line( 410, $Item );

Add a non-variant product to an existing order

// Add item from Product 15
$ProductPrice = shopp_product_variant( 
    array( 'product' => 15 ),
    'product'
);

// convert a product price to an item
$Item = shopp_product_variant_to_item ( $ProductPrice );

// add these line items to order 550
$PurchasedItem = shopp_add_order_line( 550, $Item );

See Also

You must be logged in to post a comment.

© Ingenesis Limited. Shopp™ is a registered trademark of Ingenesis Limited.

Skip to toolbar