• Resolved Xmod08

    (@xmod08)


    Hi, I’m creating a headless WordPress store. How do I expose the extra options for the REST API? Do you have a guide or documentation for this? Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Dina S.

    (@themehighsupport)

    Kindly be informed that we are saving our Extra Product Options data as order item meta data.
    ?
    You can retrieve the value using the below function or from the WooCommerce REST API.
    ?
    wc_get_order_item_meta($item_id, $field_name, true);

    You can refer to the below link for more details.

    https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

    Thank you!

    Thread Starter Xmod08

    (@xmod08)

    I guess you are referring to the order meta data.


    What I would like to achieve is in our headless WC store, I will be displaying the product details via API, I would like to display also the extra fields that I created using your plugin. How do I expose those extra fields data in API so I can use them on my headless store app?

    Dina S.

    (@themehighsupport)

    You can use the below code in your child theme’s functions.php file, you will get “Extra product options” field details in REST API product response with the key “wepo_options“.

    add_filter( 'woocommerce_rest_prepare_product_object', 'wc_app_add_custom_data_to_product', 10, 3 );
    add_filter( 'woocommerce_rest_prepare_product_variation_object', 'wc_app_add_custom_data_to_product', 20, 3 );
    // filter the product response here
    function wc_app_add_custom_data_to_product( $response, $post, $request ) {
    $product_id = $post->get_id();
    $sections = display_fields_in_front_end($product_id);
    $response->data['thwepof_options'] = $sections;
    }
    function display_fields_in_front_end($product_id){
    $all_sections = array();
    $product = wc_get_product($product_id);
    $product_type = $product->get_type();

    $categories = THWEPOF_Utils::get_product_categories($product_id);
    $tags = THWEPOF_Utils::get_product_tags($product_id);

    $sections = THWEPOF_Utils::get_sections();
    $section_fields = array();

    if($sections && is_array($sections) && !empty($sections)){
    foreach($sections as $section_name => $section){
    $section = THWEPOF_Utils_Section::get_fields($section);
    $all_sections[] = $section;
    }
    }
    return $all_sections;
    }

    Please be informed that in the case of our extra product options plugin, the product page is rendering all extra product option fields. Then while clicking the add to cart button, it takes the extra product option data and stores it on a key named as?thwepof_options?in the cart. You can use this method on your app.

    However, there are some limitations in WooCommerce to add additional product options to the cart using API.?

    Hope this helps.

    Thread Starter Xmod08

    (@xmod08)

    Thank you for your reply. The code above worked. With regards to sending the extra product options through API when I click add to cart, do you have a guide or code on how to do that?

    Thanks

    Plugin Author ThemeHigh

    (@themehigh)

    Thank you for your reply. We’re glad to hear the code worked for you.

    Regarding sending the extra product options through the API when adding a product to the cart, unfortunately, there is no specific guide available at this time for the current version of our plugin.

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.