• Stars
    star
    110
  • Rank 315,905 (Top 7 %)
  • Language
    PHP
  • License
    Other
  • Created over 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Shopping cart composer package

Shopping Cart Package

Build Status

The Moltin shopping cart composer package makes it easy to implement a shopping basket into your application and store the cart data using one of the numerous data stores provided. You can also inject your own data store if you would like your cart data to be stored elsewhere.

Installation

Download and install composer from http://www.getcomposer.org/download

Add the following to your project composer.json file

{
    "require": {
        "moltin/cart": "dev-master"
    }
}

When you're done just run php composer.phar install and the package is ready to be used.

Usage

Below is a basic usage guide for this package.

Instantiating the cart

Before you begin, you will need to know which storage and identifier method you are going to use. The identifier is how you store which cart is for that user. So if you store your cart in the database, then you need a cookie (or some other way of storing an identifier) so we can link the user to a stored cart.

In this example we're going to use the cookie identifier and session for storage.

use Moltin\Cart\Cart;
use Moltin\Cart\Storage\Session;
use Moltin\Cart\Identifier\Cookie;

$cart = new Cart(new Session, new Cookie);

Inserting items into the cart

Inserting an item into the cart is easy. The required keys are id, name, price and quantity, although you can pass over any custom data that you like.

$cart->insert(array(
    'id'       => 'foo',
    'name'     => 'bar',
    'price'    => 100,
    'quantity' => 1
));

Setting the tax rate for an item

Another key you can pass to your insert method is 'tax'. This is a percentage which you would like to be added onto the price of the item.

In the below example we will use 20% for the tax rate.

$cart->insert(array(
    'id'       => 'foo',
    'name'     => 'bar',
    'price'    => 100,
    'quantity' => 1,
    'tax'      => 20
));

Updating items in the cart

You can update items in your cart by updating any property on a cart item. For example, if you were within a cart loop then you can update a specific item using the below example.

foreach ($cart->contents() as $item) {
    $item->name = 'Foo';
    $item->quantity = 1;
}

Removing cart items

You can remove any items in your cart by using the remove() method on any cart item.

foreach ($cart->contents() as $item) {
    $item->remove();
}

Destroying/emptying the cart

You can completely empty/destroy the cart by using the destroy() method.

$cart->destroy()

Retrieve the cart contents

You can loop the cart contents by using the following method

$cart->contents();

You can also return the cart items as an array by passing true as the first argument

$cart->contents(true);

Retrieving the total items in the cart

$cart->totalItems();

By default this method will return all items in the cart as well as their quantities. You can pass true as the first argument to get all unique items.

$cart->totalItems(true);

Retrieving the cart total

$cart->total();

By default the total() method will return the total value of the cart as a float, this will include any item taxes. If you want to retrieve the cart total without tax then you can do so by passing false to the total() method

$cart->total(false);

Check if the cart has an item

$cart->has($itemIdentifier);

Retreive an item object by identifier

$cart->item($itemIdentifier);

Cart items

There are several features of the cart items that may also help when integrating your cart.

Retrieving the total value of an item

You can retrieve the total value of a specific cart item (including quantities) using the following method.

$item->total();

By default, this method will return the total value of the item plus tax. So if you had a product which costs 100, with a quantity of 2 and a tax rate of 20% then the total returned by this method would be 240.

You can also get the total minus tax by passing false to the total() method.

$item->total(false);

This would return 200.

Check if an item has options

You can check if a cart item has options by using the hasOptions() method.

if ($item->hasOptions()) {
    // We have options
}

Remove an item from the cart

$item->remove();

Output the item data as an array

$item->toArray();

More Repositories

1

laravel-cart

Laravel Facade and Service Provider for Moltin\Cart
PHP
276
star
2

react-demo-store

Moltin + React powered online store
JavaScript
195
star
3

js-sdk

JavaScript & Node.js SDKs for the Elastic Path Commerce Cloud eCommerce API
TypeScript
173
star
4

currency

Handles currency calculations, storage etc
PHP
108
star
5

nextjs-demo-store

🛍 Moltin powered Next.js storefront
JavaScript
101
star
6

gatsby-demo-store

Elastic Path + Gatsby powered online store
JavaScript
93
star
7

php-sdk

Community built SDK for using Moltin with PHP
PHP
47
star
8

ios-sdk

Swift SDK for the Moltin eCommerce API
Swift
37
star
9

shopkit

Powerful, embeddable cart and checkout.
JavaScript
30
star
10

vue-demo-store

JavaScript
27
star
11

ios-swift-example

A Swift demo app using the Moltin iOS SDK.
Swift
23
star
12

gatsby-source-moltin

🚀 Gatsby source plugin for building Elastic Path Commerce Cloud powered eCommerce websites
JavaScript
21
star
13

framework

Lightweight store framework built using Slim, Twig, Bootstrap and the Moltin SDK.
CSS
19
star
14

moltin-request

🎮 Minimal Elastic Path Commerce Cloud API request library for Node
TypeScript
17
star
15

ruby-sdk

Moltin Ruby SDK
Ruby
16
star
16

examples

⚡️ Collection of examples using the Moltin API
JavaScript
16
star
17

terraform-stack

A group of more formulated Terraform modules
HCL
13
star
18

nextjs-tutorial-code

JavaScript
12
star
19

react-redux-moltin-boilerplate

boilerplate setup for a moltin store built in react + redux
JavaScript
12
star
20

moltin-micro-checkout

💳 One-click moltin purchase with Stripe
JavaScript
10
star
21

redis-scripts

Script to maintain Redis databases
Shell
9
star
22

react-microsite

🛍 Moltin powered microsite built with Create React App & Shopkit
JavaScript
9
star
23

swift-demo-app

An eCommerce example App in swift
Swift
7
star
24

menagerie

Serverless image resizing, manipulation, and optimzation S3 proxy
TypeScript
7
star
25

terraform-rancher-ha

Terraform Rancher HA cluster in AWS
HCL
7
star
26

python-sdk

Moltin python SDK
Python
6
star
27

android-sdk

Java
6
star
28

lua-multipart-parser

A simple HTTP multipart encoder/decoder for Lua
Lua
6
star
29

ios-objc-example

An Objective-C demo app using the Moltin iOS SDK.
Objective-C
6
star
30

terraform-modules

A group of base Terraform modules
HCL
6
star
31

docs.moltin.com

6
star
32

ios-swift-tutorial

A getting started with Swift tutorial for the Moltin iOS eCommerce SDK.
Swift
5
star
33

android-example

Java
5
star
34

integration-server-example-php

A small example of an integration server written in PHP to demonstrate the usage of moltin integration webhooks
PHP
5
star
35

tax

Tax calculations for Moltin\Cart
PHP
4
star
36

progressive-web-app

Example commerce app using Ionic and Angular 2
JavaScript
3
star
37

developers.moltin.com

3
star
38

moltin-next-checkout

⚡️ Next.js demo for one-click moltin purchasing
JavaScript
3
star
39

csharp-sdk

C# API interface and SDK
C#
2
star
40

node-demo-store

JavaScript
2
star
41

applepayexample-completed

This is the a completed project for the apple pay demo guide. https://developers.moltin.com/guides/apple-google-pay
Swift
2
star
42

moltin-product-importer

Import products from a CSV into Moltin though the API
JavaScript
2
star
43

cordova-square-reader

A Cordova plugin to interface with the native Square Reader SDK libraries.
Swift
2
star
44

transfer-flow-data

Serverless function to pass flow data from cart items on to order items
JavaScript
1
star
45

apple-pay-swift-tutorial

A getting started with Apple Pay tutorial for the Moltin iOS eCommerce SDK, written in Swift.
Swift
1
star
46

tf_azure_public_ip

A Terraform module to provide a Public IP on Microsoft Azure.
HCL
1
star
47

v2-import

A quick n dirty PHP import script for products & categories in v2
PHP
1
star
48

ansible-rancher-agent

Ansible role to register host to our Rancher server
Shell
1
star
49

moltin-stripe-paymentintents

💳 SCA compliant payments with Moltin and Stripe
JavaScript
1
star
50

moltin-paymentrequest-api

💳 Demoing the Payment Request API with moltin
JavaScript
1
star
51

moltin-shippo-rates

📦 Get shipping rates for orders by Shippo for your moltin store
JavaScript
1
star
52

ios-getting-started-tutorial

A getting started tutorial for the Moltin iOS eCommerce SDK.
Objective-C
1
star
53

moltin-shippo-tracking

📦 Update orders via a webhook with a delivery status by Shippo
JavaScript
1
star