Author Topic: ajax - get virtuemart_product_id for product_sku  (Read 327 times)

sixlegs

  • Beginner
  • *
  • Posts: 8
  • VirtueMart Version: 3.4.2
ajax - get virtuemart_product_id for product_sku
« on: August 25, 2019, 12:55:29 pm »
Hi there,

maybe someone can helpme out of a problem.
I'm building a product configurator. It started simple - as always - and is getting more and more complicated as the project is speeding up - as always.

I have a variety of products that are combined by a graphical configurator. Just a few img-carousels that show the options in picture slides.
You build your product by flipping through the slides.

I read productIDs from the active slides and combine them to a product_sku that contains the complete product information like shape, colour, size, lightid and so on.
This is done by a few lines of jquery.

At the end I have a configured product and some accessories that I want to add to the cart by an "add multiple products"-link.
For the configured product that consists of 5 parts I'm trying to get the virtuemart_product_id by an ajax request and I'm completely stuck on this.

In my template I use
Code: [Select]
      function getvmid($psku) {
        $ldvmid  = 'SELECT `virtuemart_product_id` ';
        $ldvmid .=' FROM #__virtuemart_products ';
        $ldvmid .= 'WHERE ( `product_sku` = "'.$psku.'" )';
        $ldvmid .= ';';
        $ldvmidd = JFactory::getDbo();
        $ldvmidd->setQuery($ldvmfd);
        $vmproductid = $ldvmidd->loadResult();
        return $vmproductid;
      }

to get the virtuemart_product_id for my accessories.

My JQuery for the ajax stuff is like this - I know it's wrong:

Code: [Select]
// try to get vmid for skupendel where skupendel is the product_sku
      $ajax({
          type: 'GET',
          url: 'pendelsku.php',
          data: {skupendel:skupendel},
          success: function(data) {
            console.log(data);
          }
        })

pendelsku.php is nothing else than the function getvmid($psku) that I already used in my template. console.log(data) is there to show me the virtuemart_product_id for my product_sku

This drives me crazy. I never used ajax before but now I think I have to and I'm to stupid to understand...

Can't I just call my function, get the virtuemart_product_id and put it in my link to add my products to the cart?
All in one file without reloading the page?

Maybe you can help me on this one - in simple words please, as I'm really desperate  :'(

Greatings from sixlegs

Vm3.4.2 and J3.9.11








Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3978
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: ajax - get virtuemart_product_id for product_sku
« Reply #1 on: August 25, 2019, 17:47:03 pm »
most of your code is bad.
If you use url: 'pendelsku.php' you are not in the Joomla framework so you cannot use JFactory::getDbo();

Joomla url are for eg. index.php?option=com_virtuemart&view=myview&sku=mysku
in this case com_virtuemart is the component, view is the view to call and sku is the sku you need to get informations.
Joomla doc to do ajax plugin here https://docs.joomla.org/Using_Joomla_Ajax_Interface if you dont need a component(for your sample a plugin is the simplest way)
But why using sku ?
sku is not mandatory and can have same value and can be changed. product ID is safe in any case.
To send to cart multiples products, you need to get all customfields, a hard task if you dont know Virtuemart and Joomla (and php)

sixlegs

  • Beginner
  • *
  • Posts: 8
  • VirtueMart Version: 3.4.2
Re: ajax - get virtuemart_product_id for product_sku
« Reply #2 on: August 25, 2019, 21:40:48 pm »
most of your code is bad. -  :'( I know, it started bad and then got worse...
If you use url: 'pendelsku.php' you are not in the Joomla framework so you cannot use JFactory::getDbo(); - I know, but I don't want to use pendel.sku.php - I'm looking for a better way

Joomla url are for eg. index.php?option=com_virtuemart&view=myview&sku=mysku
in this case com_virtuemart is the component, view is the view to call and sku is the sku you need to get informations.
Joomla doc to do ajax plugin here https://docs.joomla.org/Using_Joomla_Ajax_Interface if you dont need a component(for your sample a plugin is the simplest way)
But why using sku ?
sku is not mandatory and can have same value and can be changed. product ID is safe in any case.
To send to cart multiples products, you need to get all customfields, a hard task if you dont know Virtuemart and Joomla (and php)

My product consists of 5 parts each of them having multiple variants. product_sku characterizes these 5 components and is needed for assembly purposes.
Flipping through the variants helps you configure the product in no time and lets you see the product in a live preview.
It works up to the part where I need to get the virtuemart_product_id for my product_sku without page reload.

I'm quite sure it can be done very simply but I have no idea how.
Best for me would be a "copy-paste" solution as I have been working on the other details the whole weekend.
Bad thing if weekends are your only way to work focused on one single project...

@Studio42: Thanks for the Joomla Ajax Interface Link! I will check this as soon as I can

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10358
  • VirtueMart Version: 3+
Re: ajax - get virtuemart_product_id for product_sku
« Reply #3 on: August 26, 2019, 16:54:21 pm »
why not use the base product id?
or have one.

The sku can be dynamic via the custom field
J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10358
  • VirtueMart Version: 3+
Re: ajax - get virtuemart_product_id for product_sku
« Reply #4 on: August 26, 2019, 16:57:48 pm »

pendelsku.php is nothing else than the function getvmid($psku) that I already used in my template. console.log(data) is there to show me the virtuemart_product_id for my product_sku


why not create a class for your functions so you can use them across the site?

http://forum.virtuemart.net/index.php?topic=141917.msg500457#msg500457
J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

sixlegs

  • Beginner
  • *
  • Posts: 8
  • VirtueMart Version: 3.4.2
Re: ajax - get virtuemart_product_id for product_sku
« Reply #5 on: August 26, 2019, 22:52:07 pm »
Thanks for this link PRO!

But I'm having an ajax question:
How can I get my virtuemart_product_id from the table virtuemart_products without reloading the page and pass it to the cart-link.

var product_sku (I have)
  ajax (???)
var virtuemart_product_id  (I need)

I've been trying so many nonsense-solutions...

By the way: what's this "page speed score" in your signature? Google page speed?