Jump to content

ajax add to cart in module


Recommended Posts

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 by banditbirds (see edit history)
Link to comment
Share on other sites

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)

  • Like 1
Link to comment
Share on other sites

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

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

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

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

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

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

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 :/

 

  1. Request URL:
  1. controller:
    cart
  2. add:
    1
  3. ajax:
    true
  4. qty:
    1
  5. id_product:
    data-id-product_95
  6. token:
    78324173a44f0692704f811c31e2c8f1

 

Thanks!!

Link to comment
Share on other sites

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

  • 3 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...