How To Integrate My Plugin with Composite Products Plugin
-
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.
- The topic ‘How To Integrate My Plugin with Composite Products Plugin’ is closed to new replies.