{% extends "_market/_layouts/main" %} {% set currentVendor = market.vendors.getCurrentVendor() %} {% if productTypeHandle is not defined %}{% exit 403 %}{% endif %} {% set productType = craft.commerce.productTypes.getProductTypeByHandle(productTypeHandle) %} {% if not productType %}{% exit 404 %}{% endif %} {% set site = craft.app.getSites().currentSite %} {% set title = 'Create new {productType} product'|t('market', {productType: productType.name}) %} {% set sidebarFields = market.plugin.getSettings().productSidebarFieldHandles %} {% set productTabsToExclude = market.plugin.getSettings().productTabsToExclude %} {# Sort out the product model #} {% if productId is defined and productId != null %} {% if product is not defined %} {% set product = craft.commerce.products.getProductById(productId) %} {% endif %} {% if not product %}{% exit 404 %}{% endif %} {% set title = product.title %} {% elseif product is not defined %} {% set product = create({ class: 'craft\\commerce\\elements\\Product', typeId: productType.id, taxCategoryId: productType.getTaxCategories()|keys|first, shippingCategoryId: productType.getShippingCategories()|keys|first, enabled: true, siteId: site.id, promotable: true, availableForPurchase: true, freeShipping: false }) %} {% do product.setFieldValue('vendor', [currentVendor.id]) %} {% endif %} {# TODO: permissions check #} {% set breadcrumbs = [ { url: url('market/products'), label: 'Products'|t('market') }, { url: craft.app.request.getUrl, label: title } ] %} {% block content %}
{{ csrfInput() }} {{ actionInput('commerce/products/save-product') }} {{ hiddenInput('typeId', productType.id) }} {% if product is defined and product.id %}{{ hiddenInput('productId', product.id) }}{% endif %} {# Header #}

{{ title }}

{# Actions #}
{# Save #}
{# Main #} {% set fl = productType.getFieldLayout() %} {% set tabs = fl.getTabs() %} {% set tabErrors = [] %} {% set excludedFldElements = [ 'craft\\fieldlayoutelements\\Heading', 'craft\\fieldlayoutelements\\HorizontalRule', 'craft\\fieldlayoutelements\\Template', 'craft\\fieldlayoutelements\\Tip', ] %} {% set tabContent %} {% for tab in tabs if tab.name not in productTabsToExclude %}
{% for element in tab.elements %} {% if className(element) not in excludedFldElements %} {% if product is defined and product.getErrors(element.attribute) %} {% set tabErrors = tabErrors|merge([tab.id]) %} {% endif %} {% if element.attribute == 'variants' %} {% for v in product.variants %} {% if v.getErrors() %} {% set tabErrors = tabErrors|merge([tab.id]) %} {% endif %} {% endfor %} {% endif %} {% include '_market/_includes/fld-element' with { uielement: element, element: product is defined ? product : null, excludedFieldHandles: sidebarFields, assetsFolderId: currentVendor.filesFolderId } only %} {% endif %} {% endfor %}
{% endfor %} {% endset %}
{# Tab nav #}
{# Mobile #}
{# Larger #}
{# Tab content #} {{ tabContent }}
{# Core attributes #}
{% include '_market/_includes/fields/slug.html' with { element: product is defined ? product : null } %} {% include '_market/_includes/fields/datetime.html' with { label: 'Post Date'|t('app'), attribute: 'postDate', element: product is defined ? product : null } %} {% include '_market/_includes/fields/datetime.html' with { label: 'Expiry Date'|t('app'), attribute: 'expiryDate', element: product is defined ? product : null } %} {# Available for purchase #} {% include '_market/_includes/fields/lightswitch.html' with { label: 'Available for purchase'|t('commerce'), attribute: 'availableForPurchase', element: product is defined ? product : null } %} {# Enabled switch #} {% include '_market/_includes/fields/lightswitch.html' with { label: 'Enabled'|t('app'), attribute: 'enabled', element: product is defined ? product : null } %} {# NOTE: no free shipping or promotable here yet #}
{# Single variant product, so we have the required fields in the sidebar #} {% if not productType.hasVariants %} {% set variant = product.id ? product.variants[0] : false %} {% set variantKey = variant and variant.id ? variant.id : 'new1' %}
{{ hiddenInput('variants['~variantKey~'][isDefault]', 1) }} {% include '_market/_includes/variant-fields.html' with { singleVariant: true, vendorCode: currentVendor.code } %}
{% endif %} {% if sidebarFields %}
{% for handle in sidebarFields %} {% set field = craft.app.getFields().getFieldByHandle(handle) %} {% if field %} {% include '_market/_includes/field' with { element: product is defined ? product : null } %} {% endif %} {% endfor %}
{% endif %}
{{ 'Date Created'|t('app') }}
{{ product.dateCreated|datetime('short')|replace(',','') }}
{{ 'Date Updated'|t('app') }}
{{ product.dateUpdated|datetime('short')|replace(',','') }}
{# NOTE: This is not very performant ... #} {% set variantIds = product.variants|map(v => v.id) %} {% set orders = craft.orders() .isCompleted() .hasPurchasables(variantIds) .all() %} {% set numberSold = 0 %} {% for order in orders %} {% for lineItem in order.lineItems %} {% if lineItem.snapshot.purchasableId in variantIds %} {% set numberSold = numberSold + lineItem.qty %} {% endif %} {% endfor %} {% endfor %}
{{ 'Quantity Sold'|t('market') }}
{{ numberSold }}
{# Delete action #} {% if product.id %}
{{ sprig('_market/_components/delete-product', { _productId: product.id }) }}
{% endif %}
{% endblock %}