Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #65 from moltin/feature/custom-cart-items
Browse files Browse the repository at this point in the history
Adds support for custom line items
  • Loading branch information
andrew-waters authored Oct 11, 2017
2 parents 17b20d8 + 7d6433b commit 9571d9a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
47 changes: 44 additions & 3 deletions examples/Carts.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function printCartItemsTable($items) {
foreach($items as $item) {
$table->addRow([
$item->id,
$item->product_id,
empty($item->product_id) ? 'custom' : $item->product_id,
$item->quantity,
$item->unit_price->amount . " (" . $item->unit_price->currency . ")",
$item->value->amount . " (" . $item->value->currency . ")",
Expand Down Expand Up @@ -44,21 +44,62 @@ function printOrderTable($order) {
// known cart id (usually stored in cookie - we're setting it here because we're on the CLI)
$cartID = '95597f65a5ea7e907a4dcbe4eb6b4435';

$stamp = time();

// add a product to the cart
$productOneID = 'a8a40abb-5357-4df4-83a0-35ccbd1d15ab';
$productOne = $moltin->products->create([
'type' => 'product',
'name' => 'My Great Product',
'slug' => 'my-great-product-' . $stamp,
'sku' => 'my.great.product.' . $stamp,
'manage_stock' => false,
'description' => 'My Great Product is awesome',
'price' => [
[
'amount' => 5000,
'currency'=> 'USD',
'includes_tax' => true
]
],
'status' => 'live',
'commodity_type' => 'physical',
]);
$productOneID = $productOne->data()->id;
$productQuantity = 2;
echo "\n\nAdding " . $productQuantity . " x " . $productOneID . " to your cart. ";
$cart = $moltin->cart($cartID)->addProduct($productOneID, $productQuantity);
echo "Cart value (with tax) is now: " . $cart->meta()->display_price->with_tax->formatted . "\n";
printCartItemsTable($cart->data());

$productTwoID = 'ac964ac0-3740-458d-933a-8647041032a3';
$productTwo = $moltin->products->create([
'type' => 'product',
'name' => 'My Even Greater Product',
'slug' => 'my-even-greater-product-' . $stamp,
'sku' => 'my.even.greater.product.' . $stamp,
'manage_stock' => false,
'description' => 'My Even Greater Product is even more awesome',
'price' => [
[
'amount' => 10000,
'currency'=> 'USD',
'includes_tax' => true
]
],
'status' => 'live',
'commodity_type' => 'physical',
]);
$productTwoID = $productTwo->data()->id;
$productTwoQuantity = 3;
echo "\n\nAdding " . $productTwoQuantity . " x " . $productTwoID . " to your cart.";
$cart = $moltin->cart($cartID)->addProduct($productTwoID, $productTwoQuantity);
echo " Cart value (with tax) is now: " . $cart->meta()->display_price->with_tax->formatted . "\n";
printCartItemsTable($cart->data());

// add a custom item
$cart = $moltin->cart($cartID)->addCustomItem("Custom Item", "custom.item", "An example custom item", 100, 2);
echo " Cart value (with tax) is now: " . $cart->meta()->display_price->with_tax->formatted . "\n";
printCartItemsTable($cart->data());

// get the cart items manually
$items = $moltin->cart($cartID)->items()->data();

Expand Down
27 changes: 27 additions & 0 deletions src/Entities/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ public function addProduct($id, $qty = 1, $spec = [])
return $this->call('POST', $body, $this->getReference() . '/items');
}

/**
* Add a Custom Item to the Cart
*
* @param string $name the custom item name
* @param string $sku the custom item sku
* @param string $description the custom item description
* @param int $price
* @param int $qty the quantity to add to the cart
* @return Moltin\Response
*/
public function addCustomItem($name, $sku, $description, $price, $qty = 1)
{
$body = [
'data' => [
'type' => 'custom_item',
'name' => $name,
'sku' => $sku,
'description' => $description,
'quantity' => $qty,
'price' => [
'amount' => $price
]
]
];
return $this->call('POST', $body, $this->getReference() . '/items');
}

/**
* Get the items in a cart
*
Expand Down

0 comments on commit 9571d9a

Please sign in to comment.