• Resolved felixtheratruns

    (@felixtheratruns)


    I have written a computer build plugin that lets you select a computer build. So it will give you the choice of motherboard, processor, ram etc and let you select a compatible combination. My plugin currently adds items to cart individually using this code:

    function addToCart(i,array_of_products,quantity,num_items) {
          console.log('add to cart: ' + array_of_products[i]);
          window.jQuery.get('/grandassembly/?post_type=product&add-to-cart=' + array_of_products[i] +'&quantity='+quantity, function() {
            // success
            console.log('success: ' + array_of_products[i]);
            i++;
            if(i<num_items){
              addToCart(i,array_of_products,quantity,num_items);
            } else {
            //end recursion
            ....
            }
            jQuery('.show_success').show();
          });
        }

    However, if the customer wants to have more than one build in the same order you might not be able to figure out which processor went with which motherboard and so on.

    So I am going to use the composite products plugin to solve this problem, and make each build a separate composite product in an order. I have created a composite product with just 2 components (a motherboard and a processor) to look at how the composite products plugin adds a composite product to cart.

    Looking at fiddler it does a post:

    POST https://localhost/grandassembly/product/test-composite/ HTTP/1.1
    Host: localhost
    Connection: keep-alive
    Content-Length: 848
    Cache-Control: max-age=0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Origin: https://localhost
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
    Content-Type: multipart/form-data; boundary=—-WebKitFormBoundarySvb7MrHWiRVVRKTA
    Referer: https://localhost/grandassembly/product/test-composite/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.8
    Cookie: wp-settings-4=libraryContent%3Dbrowse%26mfold%3Do%26posts_list_mode%3Dlist%26editor%3Dtinymce; wp-settings-time-4=1457632913; wp_woocommerce_session_c01b4709885a2e8d40a3ab0908b5d3cd=4%7C%7C1458152031%7C%7C1458148431%7C%7Cb0ae41f2eee795603dfc9f8606155481; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_c01b4709885a2e8d40a3ab0908b5d3cd=joel%7C1458226897%7CTI6Z1IW9raspLsnQpteajC4Q4JasgfDi5Nhargd3FDM%7C749f33fce938706e2cb22f2a496fd34690b0b9665d2a714c4e093e3abc07d466

    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”wccp_component_selection[1457712016]”

    576
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”wccp_component_quantity[1457712016]”

    1
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”wccp_component_selection[1457712072]”

    624
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”wccp_component_quantity[1457712072]”

    1
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”quantity”

    1
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”add-to-cart”

    776
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA
    Content-Disposition: form-data; name=”wccp_active_scenarios”

    0
    ——WebKitFormBoundarySvb7MrHWiRVVRKTA–

    776 is the product id of the composite product (called “test composite”, 576 is the product id of the motherboard I selected, and 624 is for the processor.

    Now my questions are:
    1 How do I write ajax, or a form, or something, on my plugin’s page that would make this post request on the https://localhost/grandassembly/product/test-composite/ url and add the composite to cart with whatever values I select?
    2 Is there a another better way to go about this?
    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter felixtheratruns

    (@felixtheratruns)

    This form works to add the product and it’s components to cart:

    <form method="post" enctype="multipart/form-data" class="composite_form">
        <p><input name="wccp_component_selection[1457712016]" value="576">
        <p><input name="wccp_component_quantity[1457712016]" value="1">
        <p><input name="wccp_component_selection[1457712072]" value="624">
        <p><input name="wccp_component_quantity[1457712072]" value="1">
        <p><input name="quantity" value="1">
        <p><input name="add-to-cart" value="776">
        <p><input name="wccp_active_scenarios" value="0">
        <button type="submit">Submit</button>
        </form>

    and it generates this request in fiddler.

    POST https://localhost/grandassembly/build-page/ HTTP/1.1
    Host: localhost
    Connection: keep-alive
    Content-Length: 848
    Cache-Control: max-age=0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Origin: https://localhost
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
    Content-Type: multipart/form-data; boundary=—-WebKitFormBoundaryERPPvymf9VYcPxDA
    Referer: https://localhost/grandassembly/build-page/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.8

    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”wccp_component_selection[1457712016]”

    576
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”wccp_component_quantity[1457712016]”

    1
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”wccp_component_selection[1457712072]”

    624
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”wccp_component_quantity[1457712072]”

    1
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”quantity”

    1
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”add-to-cart”

    776
    ——WebKitFormBoundaryERPPvymf9VYcPxDA
    Content-Disposition: form-data; name=”wccp_active_scenarios”

    0
    ——WebKitFormBoundaryERPPvymf9VYcPxDA–

    I noticed it doesn’t even need to be pointed to the composite product url. Now I just have to get the data into the form from my code, make it work with the real composite product with all the components (not just the test composite with just the processor and motherboard) and I need to hide the form so the user can’t see the values. But otherwise I’m done, i’ll see if I have time to post the final solution I come up with.

    Hi.

    I was wonder if “Submit” button run just the javascript code which you paste in first post ?

    And I have few questions:
    <input name="wccp_component_selection[1457712016]" value="576">

    From where did you take this number for wccp_component_selection array ? It’s auto generated ?
    576 is a component product id ?

    <input name="add-to-cart" value="776">
    Is that just composite product id ?

    Thread Starter felixtheratruns

    (@felixtheratruns)

    I was wonder if “Submit” button run just the javascript code which you paste in first post ?

    It was this that ran it:
    <a id="buycode" class="single_add_to_cart_button shop-skin-btn shop-flat-btn alt" href="#" onclick="add_all_to_cart('<?php echo $arr; ?>')">ADD BUILD TO CART</a>

    <input name="wccp_component_selection[1457712016]" value="576">

    From where did you take this number for wccp_component_selection array ? It’s auto generated

    I got it from fiddler, it is also in the html on that composite product’s display page.

    <input name="add-to-cart" value="776">

    Is that just composite product id ?

    Yes correct.

    Thread Starter felixtheratruns

    (@felixtheratruns)

    Actually this is how it works right now. You can see the button that submits the form now, (it is actually the same ADD BUILD TO CART button that I used before to run the javascript in the origin post:

    <a id="buycode" class="single_add_to_cart_button shop-skin-btn shop-flat-btn alt" href="#" onclick="add_all_to_cart('<?php echo $arr; ?>')">ADD BUILD TO CART</a>
        <form id="cart_form" type="hidden" method="post" enctype="multipart/form-data" class="composite_form">
        <p><input type="hidden" name="wccp_component_selection[1456885122]" value="<?php echo $product_ids['motherboard'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885122]"  value="<?php echo $product_ids['motherboard-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885123]" value="<?php echo $product_ids['processor'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885123]"  value="<?php echo $product_ids['processor-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885124]" value="<?php echo $product_ids['ram'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885124]"  value="<?php echo $product_ids['ram-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885267]" value="<?php echo $product_ids['cpu-cooling'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885267]"  value="<?php echo $product_ids['cpu-cooling-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885268]" value="<?php echo $product_ids['power-supply'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885268]"  value="<?php echo $product_ids['power-supply-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885269]" value="<?php echo $product_ids['video-graphics-card-gpu'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885269]"  value="<?php echo $product_ids['video-graphics-card-gpu-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885783]" value="<?php echo $product_ids['blu-ray-dvd-player'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885783]"  value="<?php echo $product_ids['blu-ray-dvd-player-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885784]" value="<?php echo $product_ids['hard-disk-drive'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885784]"  value="<?php echo $product_ids['hard-disk-drive-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885785]" value="<?php echo $product_ids['solid-state-drive'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885785]"  value="<?php echo $product_ids['solid-state-drive-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885786]" value="<?php echo $product_ids['operating-system'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885786]"  value="<?php echo $product_ids['operating-system-q'] ?>">
        <p><input type="hidden" name="wccp_component_selection[1456885787]" value="<?php echo $product_ids['case'] ?>">
        <p><input type="hidden" name="wccp_component_quantity[1456885787]"  value="<?php echo $product_ids['case-q'] ?>">
        <p><input type="hidden" name="quantity" value="1">
        <p><input type="hidden" name="add-to-cart" value="730">
        <p><input type="hidden" name="wccp_active_scenarios" value="0">
        <button style="position: absolute; left: -9999px"; id="submit_button" type="submit">Submit</button>

    This is the script that runs and submits the form, I don’t think the cart needs to be reloaded since the form takes you to the cart page anyway, so I’m going to take that out:

    <script type='text/javascript'> 
    
        function malformedJSON2Object(tar) {
          var obj = {};
          tar = tar.replace(/^\{|\}$/g,'').split(',');
          for(var i=0,cur,pair;cur=tar[i];i++){
              pair = cur.split(':');
              obj['\''+pair[0]+'\''] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1];
          }
          return obj;
        }
    
        Object.size = function(obj) {
          var size = 0, key;
          for (key in obj) {
            if (obj.hasOwnProperty(key) && obj[key] != '') size++;
          }
          return size;
        };    
    
        function add_all_to_cart(str_comp){
          var obj = malformedJSON2Object(str_comp);
    
          var length_obj = Object.size(obj);
    
          if(length_obj < 22 ){
            var r = confirm('You have not selected all the parts, are you sure you want to add build to cart?');
    
            if (r == true) {
            } else {
              return;
            }
          }
          var el = document.getElementById('cart_form');       
    
          el.submit(function( event ) {
            alert( 'Handler for .submit() called.' );
            event.preventDefault();
          });
    
          reloadCart();
        }
    
        function reloadCart() {
          var url = window.location.href
          jQuery('#wpmenucartli').load(url + ' #wpmenucartli .wpmenucart-contents');
          console.log('cart reloaded');
        }
      </script>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How To Integrate My Plugin with Composite Products Plugin’ is closed to new replies.