Skip to content

Product Controller

The product controller manages the product detail page — variant selection, gallery interaction, pricing updates, and add-to-cart functionality.

Source: src/js/controllers/product-controller.js (~800 lines)

Targets

TargetElementPurpose
pricePrice displayUpdated when variant changes
addButtonAdd to cart buttonDisabled when out of stock
qtyQuantity inputQuantity selector
galleryImage gallery containerGallery image swapping
mainImageMain product imageUpdated on variant/swatch selection
thumbnailGallery thumbnailsActive state management
skuSKU displayUpdated on variant selection
stockStock indicatorIn/out of stock messaging
optionSelectOption dropdowns/swatchesConfigurable product options

Values

ValueTypeDescription
variantsString (JSON)Configurable product variants array
skuStringBase product SKU
typeStringProduct type (simple, configurable, bundle, grouped)
productIdNumberProduct entity ID
mediaGalleryString (JSON)Gallery image URLs

Actions

ActionTriggerBehavior
addToCartClick add buttonPOST to cart API, open cart drawer
selectOptionChange option select/click swatchFilter available variants, update price/image
updateQtyClick +/- or input changeUpdate quantity value
selectThumbnailClick thumbnailSwitch main gallery image
zoomImageClick main imageOpen full-size image overlay

Variant Selection Flow

Configurable Product Handling

For configurable products, the controller:

  1. Parses the variants value (JSON array of all variant combinations)
  2. On each option change, filters to find the matching variant
  3. Updates price (variant may have a different price than the parent)
  4. Swaps the gallery image if the variant has a unique image
  5. Checks stock quantity for the selected variant
javascript
// Variant data structure
{
  id: 123,
  sku: "PROD-RED-M",
  price: 49.95,
  finalPrice: 39.95,
  stockQty: 5,
  attributes: { color: "Red", size: "M" },
  imageUrl: "/media/catalog/product/red-variant.jpg"
}
  • Thumbnail click → swaps main image with smooth transition
  • Main image click → opens zoom overlay (pinch-zoom on mobile)
  • Variant selection → auto-scrolls to the variant's image in the gallery
  • Keyboard navigation → arrow keys cycle through gallery images

Add to Cart

The add-to-cart flow:

  1. Validates all required options are selected
  2. Ensures quantity > 0 and within stock limits
  3. POSTs to the Maho API via api.post('/cart/items', { sku, qty, options })
  4. On success: dispatches cart:updated custom event, opens cart drawer
  5. On error: displays inline error message

Source: src/js/controllers/product-controller.js