0

It has not been long since I started developing in woocommerce. I may not know the limitations of woocommerce so bear with me when I explain my problem.

I want to create a product customizer. For products(furniture) that are available upon request only. So this is the frontend I've done. https://skmfurniture.com.au/KAALIKA/product/blow-me-away/

What I've created is a product that changes color with SVG(because its variation had more than 2k colors creating images was not enough). It has a price, leather/item type, main color, accent color, size, mattress price, and warranty price.

PROBLEM: You can fire up the console and watch the javascript variable that I successfully logged. I changed the javascript variable to PHP variable with ajax and then tried to insert it into the cart. Since it was all done in a modal in the single-product page of woocomerce shows "Please choose product option" message when I submit values.

This is the ajax code used:

    // adding to cart
    $('#addToCart a').click(function(e){ 
    
     $.ajax({
            type: "POST",
            data: {                             
                chase: chase,
                item : item,
                price: price,
                size: size,
                mainHex: mainHex,
                accentHex: accentHex,
                mattress: mattress,
                warranty: warranty,
                action: 'your_ajax_function'  
            },
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            dataType: "text",
            success: function (msg) {
                console.log("Chase", chase);
                console.log("Price", price);
                console.log("Item", item);
                console.log("Size", size);
                console.log("Main Color", mainHex);
                console.log("Accent Color", accentHex);
                console.log("Matt", mattress);
                console.log("Warr", warranty);
            },
            error: function (errormessage) {
                  console.log( "ERROR in ajazx" );
            }
      });
    
     window.location.replace("https://skmfurniture.com.au/KAALIKA/cart/");
    });
    

Here is how function.php file looks like:

// getting values from ajax into php
add_action('wp_ajax_nopriv_your_ajax_function','your_ajax_function'); // Ajax Function should be hooked like this for unauthenticated users
add_action('wp_ajax_your_ajax_function','your_ajax_function'); 

  function your_ajax_function(){

    $price = $_POST['price'];
    $chase = $_POST['chase'];
    $item = $_POST['item'];
    $price = $_POST['price'];
    $size = $_POST['size'];
    $mainHex = $_POST['mainHex'];
    $accentHex = $_POST['accentHex'];
    $mattress = $_POST['mattress'];
    $warranty = $_POST['warranty'];

    echo $chase;
    echo $item;


  }
 

I have written functions to place them in the cart, then checkout, and then finally to order items in the backend but unsure if I'm already wrong in these steps. I'm just printing out the if variables are passed or not but sure why I don't get anyting.

If you could just look and guide me in the right direction would be much appreciated.

xtru1990
  • 59
  • 8

1 Answers1

1

Problem

You are trying to echo the values on the server that your are posting with ajax but do not see any output.

Reason

In your success function, you are console.log(chase) instead of msg. msg is the parameter that you are passing into the success function so it is the data in scope.

// Wrong Way!

success: function (msg) {
   console.log("Chase", chase);
},

Example

This should show you the string returned from the server.

// Right Way!

success: function (msg) {
   console.log("Chase", msg);
}

Pro Tip

If you can not see the value in the DOM as expected, always check the Network tab in Chrome Dev Tools. You can click Network -> Response to see the response from the server. At least then you will know if the data is being returned or not. That will help you track down the problem.

wuno
  • 9,547
  • 19
  • 96
  • 180
  • thank you for the pro tip.. i got whole html document as response.. is there a way i could just get the variables that i want? – xtru1990 Nov 24 '20 at 10:04
  • Do two things, can you remove the second `echo` so you are only returning "chase". Also, inside the PHP function, can you do `mail("youremail", "test", chase);` to verify that the function is actually running and the value "chase" is received. – wuno Nov 24 '20 at 17:07
  • Also, to be clear, that sounds like it is working. You are posting to SELF since you are not providing a URL. So technically, you would see the HTML page in the response. You just need to make sure you are accessing the values you are counting on. Use msg in your console log and verify you can see them. Also, you have to return the data from the server, you cant just assign it to variables. Best case would be to create JSON so msg is a JSON object holding all of your key value pairs returned from server. – wuno Nov 24 '20 at 17:11
  • I added `ajaxurl = ''` as url and watching network tab I can see the response perfectly as needed. Thank you so much for your help. – xtru1990 Nov 24 '20 at 23:55
  • Amazing and great work. If you got it figured out, please select one of the answers. And you are welcome! – wuno Nov 25 '20 at 13:53