banditbirds Posted June 6, 2016 Share Posted June 6, 2016 (edited) Hi, I'm trying to use the ajax add to cart function to add a Add to Cart button in my module. I've used the same code from homefeatured.tpl and it generates the correct url: http://www.banditbirds.co.uk/cart?qty=1&id_product=89&token=78324173a44f0692704f811c31e2c8f1&add But this doesn't do anything. Do I need to include the ajax js somehow in my module? blockcart is in the header and I can see the js included in the page's header: <script type="text/javascript" src="/modules/blockcart/ajax-cart.js"></script> Anyone have any ideas? Or ideas how I can debug? <a class="exclusive ajax_add_to_cart_button" rel="ajax_id_product_{$banditQuotes[nr].quote_product_link}" href="{$link->getPageLink('cart')}?qty=1&id_product={$banditQuotes[nr].quote_product_link}&token={$static_token}&add" title="{l s='Add to cart' mod='banditquote'}">{l s='Add to cart' mod='banditquote'}</a> Thanks!! Edited June 6, 2016 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
NemoPS Posted June 6, 2016 Share Posted June 6, 2016 Depending on your theme, it might not be using the rel attribute. Prestashop's default one is not anymore actually, it's using data-id-product.Add this class to the buttonajax_add_to_cart_buttonSee if that plus the data one do it. If not, create a custom js that triggers on your class, and then use ajaxCart.add() (see that ajax-cart.js file for the exact parameters) 1 Link to comment Share on other sites More sharing options...
banditbirds Posted June 6, 2016 Author Share Posted June 6, 2016 Thanks for the advice. I assume you mean 'data_id_product' ? not 'data-id-product'? I've tried the following class and rel that you suggested: <a class="ajax_add_to_cart_button" rel="data_id_product_{$banditQuotes[nr].quote_product_link}" The reason i took 'exclusive ajax_add_to_cart_button' from homefeatured.tpl is because of the css which draws the button. Without the 'exclusive' I'll have to create some more css. The above didn't work either, so i'll try and work out how to write the js. Link to comment Share on other sites More sharing options...
banditbirds Posted June 6, 2016 Author Share Posted June 6, 2016 Depending on your theme, it might not be using the rel attribute. Prestashop's default one is not anymore actually, it's using data-id-product. Add this class to the button ajax_add_to_cart_button See if that plus the data one do it. If not, create a custom js that triggers on your class, and then use ajaxCart.add() (see that ajax-cart.js file for the exact parameters) I hope I haven't misunderstood.. I've created a function in the page (based on ajax-cart.js): function addProduct(product_id) { ajaxCart.add(product_id, null, false, this); } and called it with the html onclick: <a class="exclusive ajax_add_to_cart_button" title="{l s='Add to cart' mod='banditquote'}" onclick="addProduct({$banditQuotes[nr].quote_product_link})">{l s='Add to cart' mod='banditquote'}</a> But this doesn't work... I don't know how else to trigger my new js function from the class :/ Hope you can help again? Thanks!! Link to comment Share on other sites More sharing options...
banditbirds Posted June 6, 2016 Author Share Posted June 6, 2016 (edited) I looked at the network tab when i click my button, and it looks to be looking at the correct js, but still not working :/ Seems to get as far as SEND in jquery-1.11.0.min.js Does anyone know how to debug this part? Edited June 7, 2016 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted June 7, 2016 Author Share Posted June 7, 2016 This is also an error showing in ajax-cart.js : Uncaught TypeError: Cannot read property 'left' of undefined Which seems to be a this line? Maybe it's because I don't have a picture for this product.... pictureOffsetOriginal.right = $(window).innerWidth() - pictureOffsetOriginal.left - $element.width(); Link to comment Share on other sites More sharing options...
NemoPS Posted June 8, 2016 Share Posted June 8, 2016 actually just remove the onclick event and add data-id-product as I mentioned 1 Link to comment Share on other sites More sharing options...
banditbirds Posted June 8, 2016 Author Share Posted June 8, 2016 actually just remove the onclick event and add data-id-product as I mentioned Thank you!! That's the closest I've got to it working! I now get the error: Product not found I suspect because of processChangeProductInCart ?? Which uses $this->id_product elseif (!$this->id_product) $this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax')); How can I populate $this with the product id? Thanks again! Link to comment Share on other sites More sharing options...
banditbirds Posted June 9, 2016 Author Share Posted June 9, 2016 Sorry (again) for my lack of understanding. Am I correct thinking that CartController can't get the product id, because in my module, there is no context with product data? Do I need to override the controller or something, and write in a getProduct function in my module controller that returns $this->id_product? Thank you again for your help! Link to comment Share on other sites More sharing options...
NemoPS Posted June 10, 2016 Share Posted June 10, 2016 Actually it should get it, if you add the data info. Is the product id being sent over ajax? Like if you inspect the headers for your ajax call, in the network tab, there is a list of the parameters being sent over Link to comment Share on other sites More sharing options...
banditbirds Posted June 10, 2016 Author Share Posted June 10, 2016 Actually it should get it, if you add the data info. Is the product id being sent over ajax? Like if you inspect the headers for your ajax call, in the network tab, there is a list of the parameters being sent over Yeah, it looks like it :/ Request URL: https://www.banditbirds.co.uk/?rand=1465552636817 controller: cart add: 1 ajax: true qty: 1 id_product: data-id-product_95 token: 78324173a44f0692704f811c31e2c8f1 Thanks!! Link to comment Share on other sites More sharing options...
NemoPS Posted June 11, 2016 Share Posted June 11, 2016 No, that's wrong, the id product should be 95 only, how is your data setup? 1 Link to comment Share on other sites More sharing options...
banditbirds Posted June 11, 2016 Author Share Posted June 11, 2016 I have a new DB table that has custom orders, it has an int field called quote_product_link that links to a product, in this case with the value of 95. The page gets all the quotes for a specific customer from $banditQuotes in my controller: $this->context->smarty->assign('banditQuotes', BanditQuotes::getBanditQuotes((int)Context::getContext()->customer->id, (int)Context::getContext()->language->id)); And loops through them all, with this add to cart button passing the above quote_product_link value: rel="data-id-product_{$banditQuotes[nr].quote_product_link}" href="{$link->getPageLink('cart')}?qty=1&id_product={$banditQuotes[nr].quote_product_link}&token={$static_token}&add" Does that explain enough? Thanks!! Link to comment Share on other sites More sharing options...
banditbirds Posted June 11, 2016 Author Share Posted June 11, 2016 (edited) Progress!! I changed the rel to: rel="{$banditQuotes[nr].quote_product_link}" It now adds it!!! But there's no price :/ It's adding it as 0.00 even though I have the 'price' value in the product DB as 25.00 Edited June 11, 2016 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted June 11, 2016 Author Share Posted June 11, 2016 (edited) Fixed the price problem...was a mispelling. Edited June 12, 2016 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted June 12, 2016 Author Share Posted June 12, 2016 Thank you for all your help! Link to comment Share on other sites More sharing options...
Zions1 Posted November 5, 2019 Share Posted November 5, 2019 Any idea how to solve this problem in PS 1.7? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now