Setup & Configuration
I. Add Configurable Options Field to Product Class
- Open
EcommerceProductclass - Add field in section
price_and_unitsand name itconfigurableOptions(Title:Configurable Options). Choose type from"OpenDXP Ecommerce" -> "Configurable Options" - Save Product Class
II. Extend Product Class
- Change parent of
EcommerceProductclass (See code below) to allow configurable options - Save
EcommerceProductClass
Product Class
<?php
namespace App\Ecommerce\Model;
use OpenDxp\Bundle\EcommerceConfigurableOptionsBundle\Model\Traits\ConfigurableOptionsTrait;
class Product extends \OpenDxp\Ecommerce\Component\Core\Model\Product implements ProductInterface, ConfigurableOptionsAwareInterface
{
use ConfigurableOptionsTrait;
}
Product Interface
<?php
namespace App\Ecommerce\Model;
interface ProductInterface extends \OpenDxp\Ecommerce\Component\Core\Model\ProductInterface
{
}
III. Install Configurable Option Field Collection
Automatic
$ bin/console opendxp-ecommerce-configurable-options:install
Manually
- Create a field collection with name
EcommerceConfigurableOption - Download EcommerceConfigurableOption_export.json
- Import class and save your field collection
IV. Add Field Collection to Order Item
- Open
EcommerceOrderItemclass - Add field collection and name it
configurableOptions. You can add it right afterunitin OrderItem - Tick
not editablecheckbox - Choose
EcommerceConfigurableOptioninAllowed Typesselect box. - Save class
V. Add some Front-End Translations
| Name | Translation EN | Translation DE |
|---|---|---|
opendxp_ecommerce.configurable_options.custom_text.range_length | Up to %d Letters | Bis %d Zeichen |
opendxp_ecommerce.form.configurable_options.choice.please_select | -- Please select -- | -- Bitte wählen -- |
opendxp_ecommerce.form.configurable_options.checkbox.yes | Yes | Ja |
opendxp_ecommerce.form.configurable_options.each | each | à |
opendxp_ecommerce.form.configurable_options.one_time_base_price | + %price% one-time charge | + %price% einmaliger Grundpreis |
opendxp_ecommerce.form.configurable_options.item_price.product | Product | Produkt |
opendxp_ecommerce.form.configurable_options.item_price.options | Including Options | Davon Optionen |
opendxp_ecommerce.form.configurable_options.close | Coses | Schließen |
opendxp_ecommerce.form.configurable_options.edit_button | Edit Options | Optionen bearbeiten |
opendxp_ecommerce.form.configurable_options.refresh | Refresh | Aktualisieren |
opendxp_ecommerce.form.configurable_options.edit_header | Edit Options | Optionen bearbeiten |
VI. Front-End Templates
Finally, we need to add some mark-up to the existing shop layout.
Product/_addToCart.html.twig
Right after {{ form_errors(form) }} add:
{% if form.cartItem.configurableOptions is defined %}
<div class="alert alert-success my-3 w-100">
<h3>{{ 'opendxp_ecommerce.available_configurable_options'|trans }}</h3>
{{ form_row(form.cartItem.configurableOptions) }}
</div>
{% endif %}
Cart/Summary/_item.html.twig
Options: Right after {% if item.getIsGiftItem %} [...] {% endif %} add:
{% if item|has_configurable_options %}
<div class="alert alert-success my-3 w-100">
{{ get_configurable_options_preview(item, true) }}
</div>
{% include '@EcommerceConfigurableOptions/Modal/form.html.twig' with {form: form, item_id: item.id} only %}
{% endif %}
Item Price: Instead of <span class="price-new">[...]</span> use the macro:
<td class="[...] cart-item-price">
{# old: <span class="price-new">{{ currency.convertAndFormat(price) }}</span> [...] #}
{% import '@EcommerceConfigurableOptions/Macro/item_price.html.twig' as custom_options_item_price %}
{{ custom_options_item_price.create_item_price_list(item, true) }}
</td>
Checkout/steps/summary/_item.html.twig
Options: Right after {% if item.getIsGiftItem %} [...] {% endif %} add:
{% if item|has_configurable_options %}
<div class="alert alert-success my-3 w-100">
{{ get_configurable_options_preview(item, true) }}
</div>
{% endif %}
Item Price: Instead of <span class="price-new">[...]</span> use the macro:
<td class="[...] cart-item-price">
{# old: <span class="price-new">{{ currency.convertAndFormat(price) }}</span> [...] #}
{% import '@EcommerceConfigurableOptions/Macro/item_price.html.twig' as custom_options_item_price %}
{{ custom_options_item_price.create_item_price_list(item, true) }}
</td>
Common/order_detail.html.twig
Options: Right after {% if item.getIsGiftItem %} [...] {% endif %} add:
{% if item|has_configurable_options %}
<div class="alert alert-success my-3 w-100">
{{ get_configurable_options_preview(item, true) }}
</div>
{% endif %}
Item Price: Instead of <span class="price-new">[...]</span> use the macro:
<td class="[...] cart-item-price">
{# old: <span class="price-new">{{ currency.format(price, order.getCurrency.isoCode) }}</span> [...] #}
{% import '@EcommerceConfigurableOptions/Macro/item_price.html.twig' as custom_options_item_price %}
{{ custom_options_item_price.create_item_price_list(item, true, false, order.getCurrency.isoCode) }}
</td>
Mail/order-confirmation.html.twig
There is no item list in ecommerce default mail layout. However, if you want to add this info:
{% if item|has_configurable_options %}
{{ get_configurable_options_email_preview(item, true) }}
{% endif %}
Item Price
If you need a single calculated item price + options price, you may want to use our simple twig helper:
{% import '@EcommerceConfigurableOptions/Macro/item_price.html.twig' as custom_options_item_price %}
{# old: item.getItemPrice() #}
{{ custom_options_item_price.create_item_price(item, true, false, order.getCurrency.isoCode) }}