$npx -y skills add softspark/ai-toolkit --skill ecommerce-patternsE-commerce: cart, checkout, payments (Stripe/Adyen), order state, inventory, promos, tax. Triggers: cart, checkout, SKU, payment, Stripe, Shopify, Medusa, Magento, coupon, refund.
| 1 | # E-commerce Patterns Skill |
| 2 | |
| 3 | ## Platform Selection |
| 4 | |
| 5 | | Scenario | Platform | |
| 6 | |----------|----------| |
| 7 | | Enterprise B2B | OroCommerce | |
| 8 | | Enterprise B2C | Magento 2 | |
| 9 | | Mid-market | Shopify Plus | |
| 10 | | Small business | WooCommerce, PrestaShop | |
| 11 | | Custom/Headless | Medusa, Saleor | |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## Magento 2 Patterns |
| 16 | |
| 17 | ### Module Structure |
| 18 | ``` |
| 19 | app/code/Vendor/Module/ |
| 20 | ├── Api/ |
| 21 | │ └── Data/ |
| 22 | │ └── EntityInterface.php |
| 23 | ├── Block/ |
| 24 | ├── Controller/ |
| 25 | │ └── Index/ |
| 26 | │ └── Index.php |
| 27 | ├── etc/ |
| 28 | │ ├── module.xml |
| 29 | │ ├── di.xml |
| 30 | │ ├── routes.xml |
| 31 | │ └── frontend/ |
| 32 | ├── Model/ |
| 33 | │ └── ResourceModel/ |
| 34 | ├── Setup/ |
| 35 | ├── view/ |
| 36 | │ └── frontend/ |
| 37 | │ ├── layout/ |
| 38 | │ └── templates/ |
| 39 | ├── registration.php |
| 40 | └── composer.json |
| 41 | ``` |
| 42 | |
| 43 | ### Dependency Injection |
| 44 | ```xml |
| 45 | <!-- etc/di.xml --> |
| 46 | <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> |
| 47 | <preference for="Vendor\Module\Api\ServiceInterface" type="Vendor\Module\Model\Service"/> |
| 48 | <type name="Vendor\Module\Model\Service"> |
| 49 | <arguments> |
| 50 | <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument> |
| 51 | </arguments> |
| 52 | </type> |
| 53 | </config> |
| 54 | ``` |
| 55 | |
| 56 | ### GraphQL |
| 57 | ```graphql |
| 58 | type Query { |
| 59 | customProducts( |
| 60 | search: String |
| 61 | pageSize: Int = 20 |
| 62 | currentPage: Int = 1 |
| 63 | ): CustomProductOutput @resolver(class: "Vendor\\Module\\Model\\Resolver\\Products") |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | --- |
| 68 | |
| 69 | ## Cart & Checkout Patterns |
| 70 | |
| 71 | ### Cart State Machine |
| 72 | ``` |
| 73 | Empty → Has Items → Checkout Started → Payment → Order Placed |
| 74 | ↑ ↓ |
| 75 | └──── Abandoned ←────┘ |
| 76 | ``` |
| 77 | |
| 78 | ### Checkout Flow |
| 79 | ``` |
| 80 | 1. Cart Review |
| 81 | 2. Shipping Address |
| 82 | 3. Shipping Method |
| 83 | 4. Payment Method |
| 84 | 5. Order Review |
| 85 | 6. Place Order |
| 86 | 7. Confirmation |
| 87 | ``` |
| 88 | |
| 89 | ### Price Calculation |
| 90 | ``` |
| 91 | Base Price |
| 92 | - Catalog Discounts |
| 93 | = Adjusted Price |
| 94 | × Quantity |
| 95 | = Line Total |
| 96 | + Tax (if exclusive) |
| 97 | = Line Total with Tax |
| 98 | Σ All Lines |
| 99 | = Subtotal |
| 100 | + Shipping |
| 101 | - Cart Discounts |
| 102 | = Grand Total |
| 103 | ``` |
| 104 | |
| 105 | --- |
| 106 | |
| 107 | ## Inventory Patterns |
| 108 | |
| 109 | ### Stock Management |
| 110 | ```python |
| 111 | class InventoryService: |
| 112 | def reserve_stock(self, product_id: str, qty: int) -> bool: |
| 113 | """Reserve stock during checkout.""" |
| 114 | with transaction.atomic(): |
| 115 | stock = Stock.select_for_update().get(product_id=product_id) |
| 116 | if stock.available >= qty: |
| 117 | stock.reserved += qty |
| 118 | stock.available -= qty |
| 119 | stock.save() |
| 120 | return True |
| 121 | return False |
| 122 | |
| 123 | def confirm_stock(self, reservation_id: str): |
| 124 | """Confirm reservation after order placed.""" |
| 125 | reservation.status = 'confirmed' |
| 126 | reservation.save() |
| 127 | |
| 128 | def release_stock(self, reservation_id: str): |
| 129 | """Release stock if order cancelled.""" |
| 130 | # Return reserved qty to available |
| 131 | ``` |
| 132 | |
| 133 | ### Multi-Warehouse |
| 134 | ```sql |
| 135 | -- Stock per warehouse |
| 136 | SELECT |
| 137 | p.sku, |
| 138 | w.name as warehouse, |
| 139 | s.quantity, |
| 140 | s.reserved |
| 141 | FROM stock s |
| 142 | JOIN products p ON s.product_id = p.id |
| 143 | JOIN warehouses w ON s.warehouse_id = w.id |
| 144 | WHERE p.sku = 'SKU-123'; |
| 145 | ``` |
| 146 | |
| 147 | --- |
| 148 | |
| 149 | ## Search & Catalog |
| 150 | |
| 151 | ### Product Indexing |
| 152 | ``` |
| 153 | Product → Indexer → Search Index (Elasticsearch/OpenSearch) |
| 154 | → Price Index |
| 155 | → Category Index |
| 156 | → Stock Index |
| 157 | ``` |
| 158 | |
| 159 | ### Faceted Search |
| 160 | ```json |
| 161 | { |
| 162 | "aggs": { |
| 163 | "categories": { |
| 164 | "terms": { "field": "category_ids" } |
| 165 | }, |
| 166 | "price_ranges": { |
| 167 | "range": { |
| 168 | "field": "price", |
| 169 | "ranges": [ |
| 170 | { "to": 50 }, |
| 171 | { "from": 50, "to": 100 }, |
| 172 | { "from": 100 } |
| 173 | ] |
| 174 | } |
| 175 | }, |
| 176 | "brands": { |
| 177 | "terms": { "field": "brand" } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | ``` |
| 182 | |
| 183 | --- |
| 184 | |
| 185 | ## Performance |
| 186 | |
| 187 | ### Caching Layers |
| 188 | | Layer | Cache | |
| 189 | |-------|-------| |
| 190 | | Page | Varnish, CDN | |
| 191 | | Block | Redis | |
| 192 | | Query | MySQL Query Cache | |
| 193 | | Full Page | Redis FPC | |
| 194 | |
| 195 | ### Optimization Checklist |
| 196 | - [ ] Enable production mode |
| 197 | - [ ] Enable full page cache |
| 198 | - [ ] Optimize images (WebP) |
| 199 | - [ ] Minify JS/CSS |
| 200 | - [ ] Enable flat tables |
| 201 | - [ ] Configure CDN |
| 202 | - [ ] Index optimization |
| 203 | |
| 204 | ## Rules |
| 205 | |
| 206 | - **MUST** model order lifecycle as an explicit state machine (pending → authorized → captured → fulfilled → refunded) — ad-hoc booleans produce inconsistent states on retry |
| 207 | - **MUST** separate payment capture from inventory decrement — reserve stock on order placement, commit stock on payment success. Coupling them causes stuck-stock bugs during payment retries. |
| 208 | - **NEVER** store CVV, full PAN, or track data — ever. PCI scope expands to any system that sees them, even "temporarily in memory". |
| 209 | - **NEVER** use floating-point for money calculations. Use `decimal.Decimal`, `BigDecimal`, or integer minor units (cents). Float rounding produces $0.01 discre |