ggggino/skuskucart-bundle

This bundle aim to be a slim cart manager

2.1.1 2020-02-27 15:58 UTC

README

Total Downloads Latest Stable Version Travis (.org) Scrutinizer Code Quality

Highly customizable cart management bundle for Symfony. The archievement of this cart manager is to do something like:

Add this thing to a cart

License

License

Installation

1 Add bundle to your vendor

composer require ggggino/skuskucart-bundle

2 Register the bundle in app/AppKernel.php

$bundles = array(
    // ...
    
    // Multi step form
    new Craue\FormFlowBundle\CraueFormFlowBundle(),
    
    // Payment bundle
    new Payum\Bundle\PayumBundle\PayumBundle(),
    
    // Cart bundle
    new GGGGino\SkuskuCartBundle\GGGGinoSkuskuCartBundle(),
);

3 Create at least one currency

bin/console ggggino_skusku:currency:create

4 Set default locale and currency

parameters:
    locale: it
    currency: EUR

Configuration

Bundle complete configuration

# config.yml
ggggino_skuskucart:
    allow_anonymous_shop: false
    cart_mode: 'single_page'
    stepform:
        cart:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep1FormType
            label: Step 1
        chosePayment:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep2FormType
            label: Step 2
        payment:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep3FormType
            label: Step 3
    templates:
        cart_layout: 'GGGGinoSkuskuCartBundle::cart_page.html.twig'
        done_layout: 'xxxBundle:xxx:xxx.html.twig'

Extra configs

# config.yml
parameters:
    locale: it
    currency: EUR

Add the target entities that replace the interfaces

# config.yml
doctrine:
    orm:
        resolve_target_entities:
              GGGGino\SkuskuCartBundle\Model\SkuskuProductInterface: GGGGino\SkuskuCartBundle\Entity\SkuskuProduct
              GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface: GGGGino\SkuskuCartBundle\Entity\SkuskuUser
              GGGGino\SkuskuCartBundle\Model\SkuskuCurrencyInterface: GGGGino\SkuskuCartBundle\Entity\SkuskuCurrency
              GGGGino\SkuskuCartBundle\Model\SkuskuLangInterface: GGGGino\SkuskuCartBundle\Entity\SkuskuLanguage

Add the basics routes

# routing.yml
skusku:
    resource: "@GGGGinoSkuskuCartBundle/Controller/"
    type:     annotation

Use resolve_target_entities to replace the interface entities with the concrete ones

doctrine:
    orm:
        resolve_target_entities:
              GGGGino\SkuskuCartBundle\Model\SkuskuProductInterface: AppBundle\Entity\Product
              GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface: AnotherBundle\Entity\User
              GGGGino\SkuskuCartBundle\Model\SkuskuCurrencyInterface: My\CustomBundle\Entity\Currency
              GGGGino\SkuskuCartBundle\Model\SkuskuLangInterface: ExtraBundle\Entity\Language

Every class used must implements the right interface.

  1. Currency

    use GGGGino\SkuskuCartBundle\Model\SkuskuCurrencyInterface;
    
    class Currency implements SkuskuCurrencyInterface
    {
    }
  2. User

    use GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface;
    
    class User implements SkuskuCustomerInterface
    {
    }
  3. Product

    use GGGGino\SkuskuCartBundle\Model\SkuskuProductInterface;
    
    class Product implements SkuskuProductInterface
    {
    }
  4. Language

    use GGGGino\SkuskuCartBundle\Model\SkuskuLangInterface;
    
    class Lang implements SkuskuLangInterface
    {
    }

If you want an prebuilt entity you can extend their own base class.

  1. Currency
    use GGGGino\SkuskuCartBundle\Model\SkuskuCurrencyBase;
    class Currency extends SkuskuCurrencyBase
    {
    }

Twig functions

Print the cart preview

{{ render_preview_cart() }}

Print the language choice block

{{ render_lang_cart() }}

Print the currency choice block

{{ render_currency_cart() }}

CartManager API

Cart manager

Get the cart manager instance

use GGGGino\SkuskuCartBundle\Service\CartManager;
.
.
.
$cartManager = $this->get(CartManager::class);

CartManager::persistCart(SkuskuCart $cart)

Add the cart to EntityManager

use GGGGino\SkuskuCartBundle\Model\SkuskuCart;
.
.
.
/** @var SkuskuCart $finalCart */
$finalCart = ...

$cartManager->persistCart($finalCart);

CartManager::flushCart(SkuskuCart $cart);

Flush the cart

$cartManager->flushCart($finalCart);

CartManager::addProductToCart(SkuskuProductInterface $product, int $quantity)

Add some product to the cart

$quantity = 20;
$cartManager->addProductToCart($product, $quantity);

CartManager::createNewCart(SkuskuCustomerInterface $customer = null)

Create new Cart from a given customer, if the customer is not passed is taken from the session

$cartManager->createNewCart($customer);

CartManager::createNewOrderFromCart(SkuskuCart $cart)

Build a new Order from a given cart. Used for example when the payment gone good

$cartManager->createNewOrderFromCart($cart);

Cart page

/cart

Commands

bin/console ggggino_skusku:cart:clear

Clear all the skuskutables

bin/console ggggino_skusku:currency:create

Create a row of the given entity - DEV

bin/console ggggino_skusku:doctor:db

Check if the installation procedure was successful

Configuration details

You can decide if even the anonymous user can shop

# config.yml
ggggino_skuskucart:
    allow_anonymous_shop: false

Chose between use the default steps and create new ones, remember that for the "cart|chosePayment|payment" you can only override configs

# config.yml
ggggino_skuskucart:
    stepform:
        cart:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep1FormType
            label: Step 1
        chosePayment:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep2FormType
            label: Step 2
        payment:
            form_type: GGGGino\SkuskuCartBundle\Form\CartFlowType\CartStep3FormType
            label: Step 3

If you need more customization in the formstep you can override it. Your CartFlow needs only to inherit

# config.yml
ggggino_skuskucart:
    stepform_class: GGGGino\SkuskuCartBundle\Form\CartFlow

If you need to change the templates

# config.yml
ggggino_skuskucart:
    templates:
        cart_layout: 'GGGGinoSkuskuCartBundle::cart_page.html.twig'
        done_layout: 'xxxBundle:xxx:xxx.html.twig'

Events

Templates

TODO

  • API for creating cart
  • Ordering stepform items

Test taken from: https://github.com/nelmio/NelmioApiDocBundle