diff --git a/examples/Carts.php b/examples/Carts.php index 5073892..dba38ca 100644 --- a/examples/Carts.php +++ b/examples/Carts.php @@ -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 . ")", @@ -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(); diff --git a/src/Entities/Cart.php b/src/Entities/Cart.php index 5db9625..1396385 100644 --- a/src/Entities/Cart.php +++ b/src/Entities/Cart.php @@ -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 *