2

Let me apologize right now that my inexperience with Craft is likely the issue here and that my question might be more general to Craft's routing features and not so specific to Craft Commerce.

I'm playing with a clean Craft/Craft Commerce installation and using the included templates in /templates/commerce. I wanted to customize the path however to /store/. I changed the directory name, adjusted the "commerce" where it was hardcoded in the templates, and changed the Product URL Format and Product Template within the Product Types to use store/ instead. Everything seems to look okay until I try to add/remove/update a product into the cart.

When that happens I get a routing error:

CHttpException
Unable to resolve the request "store/cart/updateCart".
/path/to/craft/app/framework/web/CWebApplication.php(286)

Line 286 is:

throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".'

Should I be leaving these paths alone and adjusting the URL in a different way? Any help and direction would be greatly appreciated. Thank you!

2 Answers 2

5

You'll need to keep any action URLs as commerce/, as these point to controller actions inside the Commerce plugin, not templates in your store directory. So if you find:

<input type="hidden" name="action" value="store/cart/updateCart">

That will need to be restored to:

<input type="hidden" name="action" value="commerce/cart/updateCart">
0
2

There may be better ways but here was our approach to setting it up in a way that's easy to duplicate for the next project. This method will allow you to use the existing /commerce folder for your templates, while using any replacement word you want for "commerce", in your URIs.

In /config/general.php, create an enviromentVariable using your desired replacement for "commerce":

'environmentVariables' => array(
  'baseUrl'   => 'http://commerce.dev',
  'basePath'  => getcwd(),
  'shopPrefix' => 'shop' //<- add this line
)

In /templates/ create a folder of the same name as your prefix. Inside that folder, create a twig template file, such as _entry.twig. Place the following in that file:

{% set shopPrefix = craft.config.environmentVariables.shopPrefix %}
{% set segmentUri = "/" %}
{% for segment in craft.request.segments() %}
    {% set segmentUri = segmentUri ~ "/" ~ segment|replace(shopPrefix, 'commerce') %}
{% endfor %}

{% include segmentUri %}

Next, open /templates/commerce/_layouts/main.html (or whatever is your primary layout for Commerce pages). After {% set cart = craft.commerce.cart %}, add:

{% set shopPrefix = craft.config.environmentVariables.shopPrefix %} 

In your various templates, replace any commerce links or input fields used for redirects with {{shopPrefix}}/cart, e.g:

<a href="{{shopPrefix}}/cart">Link</a>
<input type="hidden" name="redirect" value="{{shopPrefix}}/cart"/>

Any redirect URIs part of Twig functions should be replaced as well. Example:

{% if cart.isEmpty %}
    {% redirect shopPrefix ~ '/myProductsPage' %}
{% endif %}

NOTE: as mentioned above, never replace Commerce action URIs. They are used as controller actions.

The benefit of this method is that next time, if a client wants to use "buy" instead of "shop", you can reuse all these templates, just change the enviromentVariable and one folder name, and you're done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.