<p>Close!  This should do it:</p>

<pre><code>{% set products = craft.commerce.products({
    relatedTo: (category), 
    order: 'defaultPrice',
}) %}
</code></pre>

<p>With the quotes it's a string.  Without the quotes, Twig is expecting a variable you've defined called <code>defaultPrice</code> that doesn't exist.</p>

CC BY-SA 3.0
2016-05-06T00:27:45.087Z
2016-05-06T00:27:45.087Z
Brad Bell
<p>Trying to sort products by price. </p>

<p>Since the price of a product in Commerce isn't a textual custom field it's attached to a variant, I'm having trouble working out the syntax to make this happen.</p>

<pre><code>{% set products = craft.commerce.products({
     relatedTo: (category), 
     order: defaultPrice,
}) %}
</code></pre>

<p>This seems close but delivers "Variable "defaultPrice" does not exist"</p>

<p>Appreciate any direction.</p>

<plugin-craftcommerce><order><ecommerce><sort>
Craft Commerce: Sort Products By Price
# Craft Commerce: Sort Products By Price

(Asked by: username_0 on 2016-05-05)

Trying to sort products by price. 
Since the price of a product in Commerce isn't a textual custom field it's attached to a variant, I'm having trouble working out the syntax to make this happen.
{% set products = craft.commerce.products({
     relatedTo: (category), 
     order: defaultPrice,
}) %}

This seems close but delivers "Variable "defaultPrice" does not exist"
Appreciate any direction.

**Tags:** plugin-craftcommerce, order, ecommerce, sort


---

## 1 answers

### Answer by username_1 on 2016-05-06. Score: 5 | Accepted answer

Close!  This should do it:
{% set products = craft.commerce.products({
    relatedTo: (category), 
    order: 'defaultPrice',
}) %}

With the quotes it's a string.  Without the quotes, Twig is expecting a variable you've defined called defaultPrice that doesn't exist.


---

