Archived topic
How to add this code
3 replies · Started by Russell on May 1, 2018
I am trying to use add to cart buttons from my cart software - they generate this code - that SHOULD work... however, the cart people say that there is an issue with the theme
Sample code below for add to cart
<!-- Button/Link for aMember Shopping Cart -->
<script type="text/javascript">
if (typeof cart == "undefined")
document.write("<scr" + "ipt src='/membership/application/cart/views/public/js/cart.js'></scr" + "ipt>");
</script>
<input type="button" onclick="cart.addCheckoutExternal(this,20); return false;" value="Add to Cart">
<!-- End Button/Link for aMember Shopping Cart -->
Sample code for view basket:
<!-- Basket for aMember Shopping Cart -->
<script type="text/javascript">
if (typeof(cart) == "undefined")
document.write("<scr" + "ipt src='/membership/application/cart/views/public/js/cart.js'></scr" + "ipt>");
</script>
<script type="text/javascript">
jQuery(function(){cart.loadOnly();});
</script>
<!-- End Basket for aMember Shopping Cart -->
Each time I have tried to add the above... the page has been wiped of all HTML
I have tried the standard WP Editor - which USUALLY works.
I am using EPro to create the store pages.. and with that the page is wiped completely.
I have been advise by my cart people that it would be best to add the following code:
I suggest to add cart.js to header of your theme so:
<script src='/membership/application/cart/views/public/js/cart.js'></script>
Then you can put to elementor only html code ie.:
<input type="button" onclick="cart.addCheckoutExternal(this,4); return false;" value="Add To Basket">
I have no idea how to add to the header and if I do add to the header, does that mean I need to now use a child theme etc?
Or is there an easier fix to ensure the above code works with GP?
Thanks in advance
Hi there,
You can add the code into the wp_head hook using GP Hooks (Appearance > GP Hooks).
Then you should be able to add the other JS inside Elementor.
Let me know :)
When I try to add the code to the wp-head hook I get a big 403 error come up on screen
403
Forbidden
Access to this resource on the server is denied!
That means your server isn't allowing you to insert javascript into the database. This is a security measure some hosts implement to prevent bad scripts from being injected.
Instead, you can add a function like this:
add_action( 'wp_head', 'tu_add_head_script' );
function tu_add_head_script() {
?>
Your scripts here
<?php
}
Adding PHP: https://docs.generatepress.com/article/adding-php/