News:

Support the VirtueMart project and become a member

Main Menu

ajax - get virtuemart_product_id for product_sku

Started by sixlegs, August 25, 2019, 12:55:29 PM

Previous topic - Next topic

sixlegs

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

      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:


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

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

Quote from: Studio 42 on August 25, 2019, 17:47:03 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

why not use the base product id?
or have one.

The sku can be dynamic via the custom field

PRO

Quote from: sixlegs on August 25, 2019, 12:55:29 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

sixlegs

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?