Custom js file and action

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Currently, when user clicks on product picture(all over the application) he is redirected to product description page.
Instead of this redirection, i would like to call javascript function for products with attribute "Description=false".

For example, on every page, where product item is rendered, i should include this script:

<script type="text/javascript">
    function productImageClick(){
       alert("Description page required");
    }
</script>

And when user click on product image(to be redirected to description page), function productImageClick() should be executed instead of for the products with attribute "Description=false".

How can I do that? The easiest option is to manually change all view templates but that is not recommended to change the source code.
Can I do some generic function to do that? What is the standard way.

I guess it is with custom plugin - but I don't want to override all view templates, where product image is present.
It must be some other way?
8 years ago
simonxy wrote:
Currently, when user clicks on product picture(all over the application) he is redirected to product description page.
Instead of this redirection, i would like to call javascript function for products with attribute "Description=false".

For example, on every page, where product item is rendered, i should include this script:

<script type="text/javascript">
    function productImageClick(){
       alert("Description page required");
    }
</script>

And when user click on product image(to be redirected to description page), function productImageClick() should be executed instead of for the products with attribute "Description=false".

How can I do that? The easiest option is to manually change all view templates but that is not recommended to change the source code.
Can I do some generic function to do that? What is the standard way.

I guess it is with custom plugin - but I don't want to override all view templates, where product image is present.
It must be some other way?


Hi,

To achieve this you can place the following code only once in a view which is called every time. For example place the code in the end of the _Root.cshtml view.
<script type="text/javascript">
    $(document).ready(function() {
        $('.product-item .picture a').on('click', function (e) {
            // I assume that the Description attribute is on .product-item element
            if ($(this).parents('.product-item').attr('description') === 'false') {
                e.preventDefault();

                // Write your code here
            }
        });
    });
</script>


I hope this helped !
8 years ago
Thank you for your suggestion.

This means I have to include somehow description value to product-item tag?

$(this).parents('.product-item').attr('description') === 'false'

And second:
" place the code in the end of the _Root.cshtml view"

I should do that probably with overriding this view in custom plugin - otherwise upgrade will remove this code.
8 years ago
simonxy wrote:
Thank you for your suggestion.

This means I have to include somehow description value to product-item tag?

$(this).parents('.product-item').attr('description') === 'false'

And second:
" place the code in the end of the _Root.cshtml view"

I should do that probably with overriding this view in custom plugin - otherwise upgrade will remove this code.


Hi,

Yes, you need to modify the views, but I think a whole plugin is too much for only two views - just override them in your theme and keep in mind that they should be merger when the upgrade comes.
8 years ago
Ok thanks for your advice.

Product picture is just inside 2 views? I thought it is everywhere(inside categories, most viewed products, action products, ....)

Just one more? I would like to add bit column to the product table. But if you add attribute to product inside admin page, every attribute is string(in fact xml with string value). That is not optimal. Do you know if it can be done from admin?

Otherwise if I change class and model and similar inside core, every upgrade will also brake this.
8 years ago
simonxy wrote:
Ok thanks for your advice.

Product picture is just inside 2 views? I thought it is everywhere(inside categories, most viewed products, action products, ....)

Just one more? I would like to add bit column to the product table. But if you add attribute to product inside admin page, every attribute is string(in fact xml with string value). That is not optimal. Do you know if it can be done from admin?

Otherwise if I change class and model and similar inside core, every upgrade will also brake this.


Hi,

The product box code on all catalog pages is inside the "_ProductBox.cshtml" file. This is the only view you need to modify.

About the bit column - you can use the attributes but with Control Type: "Checkbox". You can use this as Boolean type.
8 years ago
Hi,

just one more to ask. Product attributes doesn't have control types?
Only checkout attributes has.

So, how can I add checkout attribute to the product? Also, I can't find it in documentation.

br, Simon
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.