Skip to content

Commit

Permalink
Support file upload line item properties on Product Forms and Cart No…
Browse files Browse the repository at this point in the history
…tifications (#745)
  • Loading branch information
LucasLacerdaUX committed Oct 15, 2021
1 parent 3359544 commit 2d1c832
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
1 change: 1 addition & 0 deletions assets/component-cart-items.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@

.product-option {
font-size: 1.4rem;
word-break: break-all;
line-height: calc(1 + 0.5 / var(--font-body-scale));
}

Expand Down
18 changes: 0 additions & 18 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,6 @@ function debounce(fn, wait) {
};
}

const serializeForm = form => {
const obj = {};
const formData = new FormData(form);

for (const key of formData.keys()) {
const regex = /(?:^(properties\[))(.*?)(?:\]$)/;

if (regex.test(key)) {
obj.properties = obj.properties || {};
obj.properties[regex.exec(key)[2]] = formData.get(key);
} else {
obj[key] = formData.get(key);
}
}

return JSON.stringify(obj);
};

function fetchConfig(type = 'json') {
return {
method: 'POST',
Expand Down
13 changes: 7 additions & 6 deletions assets/product-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (!customElements.get('product-form')) {
onSubmitHandler(evt) {
evt.preventDefault();
const submitButton = this.querySelector('[type="submit"]');
if (submitButton.classList.contains('loading')) return;
if (submitButton.classList.contains('loading')) return;

this.handleErrorMessage();
this.cartNotification.setActiveElement(document.activeElement);
Expand All @@ -23,11 +23,12 @@ if (!customElements.get('product-form')) {

const config = fetchConfig('javascript');
config.headers['X-Requested-With'] = 'XMLHttpRequest';
config.body = JSON.stringify({
...JSON.parse(serializeForm(this.form)),
sections: this.cartNotification.getSectionsToRender().map((section) => section.id),
sections_url: window.location.pathname
});
delete config.headers['Content-Type'];

const formData = new FormData(this.form);
formData.append('sections', this.cartNotification.getSectionsToRender().map((section) => section.id));
formData.append('sections_url', window.location.pathname);
config.body = formData;

fetch(`${routes.cart_add_url}`, config)
.then((response) => response.json())
Expand Down
31 changes: 24 additions & 7 deletions sections/cart-notification-product.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,33 @@
{% endif %}
<div>
<h3 class="cart-notification-product__name h4">{{ item.product.title | escape }}</h3>
{%- unless item.product.has_only_default_variant -%}
<dl>
{%- for option in item.options_with_values -%}
<div class="product-option">
<dt>{{ option.name }}: </dt>
<dd>{{ option.value }}</dd>
</div>
{%- unless item.product.has_only_default_variant -%}
{%- for option in item.options_with_values -%}
<div class="product-option">
<dt>{{ option.name }}: </dt>
<dd>{{ option.value }}</dd>
</div>
{%- endfor -%}
{%- endunless -%}
{%- for property in item.properties -%}
{%- assign property_first_char = property.first | slice: 0 -%}
{%- if property.last != blank and property_first_char != '_' -%}
<div class="product-option">
<dt>{{ property.first }}: </dt>
<dd>
{%- if property.last contains '/uploads/' -%}
<a href="{{ property.last }}" class="link" target="_blank">
{{ property.last | split: '/' | last }}
</a>
{%- else -%}
{{ property.last }}
{%- endif -%}
</dd>
</div>
{%- endif -%}
{%- endfor -%}
</dl>
{%- endunless -%}
{%- if item.selling_plan_allocation != nil -%}
<p class="product-option">{{ item.selling_plan_allocation.selling_plan.name }}</p>
{%- endif -%}
Expand Down
3 changes: 1 addition & 2 deletions sections/main-cart-items.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{{ 'component-cart.css' | asset_url | stylesheet_tag }}
{{ 'component-cart-items.css' | asset_url | stylesheet_tag }}
{{ 'component-totals.css' | asset_url | stylesheet_tag }}
{{ 'component-price.css' | asset_url | stylesheet_tag }}
{{ 'component-discounts.css' | asset_url | stylesheet_tag }}
Expand Down Expand Up @@ -83,7 +82,7 @@
<dt>{{ property.first }}: </dt>
<dd>
{%- if property.last contains '/uploads/' -%}
<a href="{{ property.last }}" target="_blank">
<a href="{{ property.last }}" class="link" target="_blank">
{{ property.last | split: '/' | last }}
</a>
{%- else -%}
Expand Down

0 comments on commit 2d1c832

Please sign in to comment.