I am new to OCMod and trying to tidy up my Opencart 3.x store modifications. In many places I need to replace multiple lines of code and I can't seem to get offset to work. Following Digicart's solution: Replace admin TPL files with OCMOD I have the following code.
3 LINES OF CODE TO REPLACE:
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
<button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>
<button type="button" data-toggle="tooltip" title="{{ button_compare }}" onclick="compare.add('{{ product.product_id }}');"><i class="fa fa-exchange"></i></button>
OCMod CODE:
<file path="catalog/view/theme/default/template/product/category.twig" name="">
<operation info="Move and change add to cart icon and remove text">
<search offset="3"><![CDATA[<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>]]></search>
<add position="replace"><![CDATA[
{% if 1 %}
<button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>
<button type="button" data-toggle="tooltip" title="Add to Cart" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-cart-plus"></i></button>
<button type="button" data-toggle="tooltip" title="{{ button_compare }}" onclick="compare.add('{{ product.product_id }}');"><i class="fa fa-exchange"></i></button>
{% endif %}
]]></add>
</operation>
</file>
Please ignore the redundant "if", actual conditions are removed for testing. The problem is that offset="3" appears to be totally ignored and only the search string is replaced. I end up with 5 buttons instead of 3 re-ordered and modified buttons.
What am I doing wrong?
Please move offset
to add
tag and use 2 instead of 3 (starting from zero):
<add position="replace" offset="2">
I tested with OpenCart 3.0.2.0 and it worked.
For a quicker check, you can upload your file with install.ocmod.xml
name in the system/
folder, and then clear the ocmod
cache.
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>test123456</name>
<code>test123456</code>
<version>1.0</version>
<author>test123456</author>
<link>http://www.opencart.com</link>
<file path="catalog/view/theme/default/template/product/category.twig" name="">
<operation info="Move and change add to cart icon and remove text">
<search><![CDATA[<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>]]></search>
<add position="replace" offset="2"><![CDATA[
{% if 1 %}
<button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>
<button type="button" data-toggle="tooltip" title="Add to Cart" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-cart-plus"></i></button>
<button type="button" data-toggle="tooltip" title="{{ button_compare }}" onclick="compare.add('{{ product.product_id }}');"><i class="fa fa-exchange"></i></button>
{% endif %}
]]></add>
</operation>
</file>
</modification>