Search code examples
phprestwoocommercegravity-forms-pluginwoocommerce-rest-api

Add plugin metadata on woocommerce product creation using the REST API


Looking at the Woocommerce documentation, very little is said about adding custom metadata. Apart from some key value addition that doesn't seem to relate to plugins.

I have a plugin called "gravity forms" which also has its own extensive REST Api, however I cannot work out how to automatically assign a particular gravity form to a product when I create it. (their rest api seems to deal primarily with managing the forms, not assigning them to products)

When I pull a specific product with an assigned gravity form it has this custom metadata:

"metadata": [...,{ {"id"=>24866, "key"=>"_gravity_form_data", "value"=> {"id"=>"6", "display_title"=>false, "display_description"=>false, "disable_woocommerce_price"=>"no", "price_before"=>"", "price_after"=>"", "disable_calculations"=>"no", "disable_label_subtotal"=>"no", "disable_label_options"=>"no", "disable_label_total"=>"no", "disable_anchor"=>"no", "label_subtotal"=>"Subtotal", "label_options"=>"Options", "label_total"=>"Total", "use_ajax"=>"no"}}]

however, when I try and autoamtically assign that metadata on product for example, as metadata: [{_gravity_form_data: { id: 6...}}], it doesn't automatically assign the gravity form to the product.

Question 1) what ways are there to automatically assign the gravity form to a product?

Question 2) if it can be done after product creation, how do I take the product id and assign that gravity form to it? Do i have to use a post creation hook - I'd prefer to stick primarily to REST.


Solution

  • Installed the WC API Custom Metadata Plugin, which meant I could pull a previously modified product with the new gravity form, but this will work for any metadata object. I could then edit my POST request with:

    meta_data: [ { key: "_gravity_form_data", value: { id: "6", display_title: false, display_description: false, disable_woocommerce_price: "no", price_before: "", price_after: "", disable_calculations: "no", disable_label_subtotal: "no", disable_label_options: "no", disable_label_total: "no", disable_anchor: "no", label_subtotal: "Subtotal", label_options: "Options", label_total: "Total", use_ajax: "no" } } ]

    And it will automatically give it an ID and auto assign the gravity form to the product!