Jump to content

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)

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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!!

Share this post


Link to post
Share on other sites

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

 

post-770181-0-33949400-1465215104_thumb.png

 

Seems to get as far as SEND in jquery-1.11.0.min.js

 

Does anyone know how to debug this part?

Edited by banditbirds (see edit history)

Share this post


Link to post
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();

Share this post


Link to post
Share on other sites

actually just remove the onclick event and add data-id-product as I mentioned

  • Like 1

Share this post


Link to post
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!

Share this post


Link to post
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!

Share this post


Link to post
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

Share this post


Link to post
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!!

Share this post


Link to post
Share on other sites

No, that's wrong, the id product should be 95 only, how is your data setup?

  • Like 1

Share this post


Link to post
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!!

Share this post


Link to post
Share on other sites

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 by banditbirds (see edit history)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More