bookingactivities / booking-activities

Wordpress plugin booking system
GNU General Public License v3.0
28 stars 8 forks source link

Woocommerce-rest-api: Booking Activities Event Structure #117

Open yabdali opened 2 years ago

yabdali commented 2 years ago

Hi, We are trying to setup a middleware system to interface with Woocommerce for pulling certain data into an ERP system. However, we are facing a problem in getting the breakdown of the booking activities event metadata as shown in bold below. If you look at the next node which includes a different (simple product: Kayaking) you can see the difference and how easy to parse the json output. Our only option currently to be able to pull the event dates/time is to setup another integration node to connect to the webhost MySql database which has its own challenges as we have to allow remote access to it and performance can be deterred. Also, is there any API access in your roadmap, I believe it might help as well! Thanks for your continuous support and efforts. Reference: https://woocommerce.github.io/woocommerce-rest-api-docs/#order-meta-data-properties

"line_items": [ { "id": 19, "name": "Al Khayran Tour", "product_id": 116, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "100.000", "subtotal_tax": "0.000", "total": "100.000", "total_tax": "0.000", "taxes": [ ], "meta_data": [ { "id": 203, "key": "bookacti_bookings", "value": "[{"id":154,"type":"single"}]", "display_key": "Booking", "display_value": "Booking ID:154Status:PendingEvent:Morning Tour - Friday, November 12th, 2021 8:00 AM → 11:00 AM x1Actions:Cancel | Reschedule" } ], "sku": "CO-ADV-02", "price": 100, "parent_name": null }, { "id": 20, "name": "Kayaking", "product_id": 726, "variation_id": 735, "quantity": 1, "tax_class": "", "subtotal": "10.000", "subtotal_tax": "0.000", "total": "10.000", "total_tax": "0.000", "taxes": [ ], "meta_data": [ { "id": 213, "key": "pa_kayak-type", "value": "single-kayak", "display_key": "Kayak Type", "display_value": "Single" }, { "id": 214, "key": "pa_duration", "value": "1-hour", "display_key": "Duration", "display_value": "1 Hour" } ],

yoancutillas commented 2 years ago

Hello, Booking Activities has an API, but no REST API, and is not integrated to WooCommerce REST API unfortunately. You can get the bookings meta associated to order items with functions such as bookacti_wc_get_order_items_bookings $order_items_bookings = bookacti_wc_get_order_items_bookings( $order_id, array( 'fetch_meta' => true ) ); (examples of uses), or fetch the data in the database directly.

yabdali commented 2 years ago

Hello, Booking Activities has an API, but no REST API, and is not integrated to WooCommerce REST API unfortunately. You can get the bookings meta associated to order items with functions such as bookacti_wc_get_order_items_bookings $order_items_bookings = bookacti_wc_get_order_items_bookings( $order_id, array( 'fetch_meta' => true ) ); (examples of uses), or fetch the data in the database directly.

@yoancutillas Thanks for your inputs. Would it be possible to provide some guidance on how to achieve the same results regarding the booking>event details similar to the one listed in the link below? Appreciate your help and support, thanks. https://stackoverflow.com/a/66440898

yoancutillas commented 2 years ago

Sorry I cannot provide support for custom code unfortunately. You can use bookacti_wc_get_order_items_bookings to retrieve the bookings info bound to a WC order, then you can format them and use them how / where ever you need.

yabdali commented 2 years ago

Sorry I cannot provide support for custom code unfortunately. You can use bookacti_wc_get_order_items_bookings to retrieve the bookings info bound to a WC order, then you can format them and use them how / where ever you need.

@yoancutillas Thanks... I find easier to to use javascript on the integration tool to transform the items the way I want since I want to avoid having custom code which may get broken when some updates change the underlying functionality.

yoancutillas commented 2 years ago

I am glad to read that, I also recommend thinking up the maintenance before developing a custom code.

yabdali commented 2 years ago

Hello, Booking Activities has an API, but no REST API, and is not integrated to WooCommerce REST API unfortunately. You can get the bookings meta associated to order items with functions such as bookacti_wc_get_order_items_bookings $order_items_bookings = bookacti_wc_get_order_items_bookings( $order_id, array( 'fetch_meta' => true ) ); (examples of uses), or fetch the data in the database directly.

@yoancutillas I tried your suggestion to use the bookacti_wc_get_order_items_bookings, however, the output produced by the function seems to be having issues. Below is the a node that I added to WC output (bookingdetails), under it there's an item which is supposed to be the value (60) for the order line item sequence. However, the key is missing and the value is inplace of the key (supposedly ID). Also, I am getting an error parsing the node, its related to the emitted structure, the use of the square brackets [] and the curly brackets {} seems to be out of place or it could be due to the element (60) being the index. I am also providing below the output for the lineitems node to compare the structure output by WC. Appreciate your feedback.

  "bookingdetails": {
    "60": [
      {
        "id": 207,
        "type": "single",
        "bookings": [
          {
            "id": "207",
            "user_id": "2",
            "order_id": "1447",
            "form_id": "13",
            "group_id": null,
            "event_id": "94",
            "event_start": "2021-12-17 08:00:00",
            "event_end": "2021-12-17 14:00:00",
            "state": "pending",
            "payment_status": "owed",
            "creation_date": "2021-11-23 18:21:57",
            "expiration_date": "2021-11-23 18:49:05",
            "quantity": "1",
            "active": "1",
            "unique_group_id": "207",
            "event_title": "Morning Tour",
            "event_active": "1",
            "activity_id": "8",
            "activity_title": "Morning Tour",
            "activity_active": "1",
            "template_id": "8",
            "template_title": "Daymaniyat Calendar",
            "template_active": "1",
            "group_date": null,
            "event_group_id": null,
            "group_state": null,
            "group_payment_status": null,
            "group_user_id": null,
            "group_order_id": null,
            "group_form_id": null,
            "group_active": null,
            "category_id": null,
            "group_title": null,
            "event_group_active": null,
            "availability": "1"
          }
        ]
      }
    ],
    "59": [
      {
        "id": 205,
        "type": "single",
        "bookings": [
          {
            "id": "205",
            "user_id": "2",
            "order_id": "1447",
            "form_id": "6",
            "group_id": null,
            "event_id": "75",
            "event_start": "2021-12-18 18:00:00",
            "event_end": "2021-12-19 08:00:00",
            "state": "pending",
            "payment_status": "owed",
            "creation_date": "2021-11-23 18:19:05",
            "expiration_date": "2021-11-23 18:49:05",
            "quantity": "1",
            "active": "1",
            "unique_group_id": "205",
            "event_title": "Camping Trip",
            "event_active": "1",
            "activity_id": "10",
            "activity_title": "Camping Trip",
            "activity_active": "1",
            "template_id": "5",
            "template_title": "Camping Calendar",
            "template_active": "1",
            "group_date": null,
            "event_group_id": null,
            "group_state": null,
            "group_payment_status": null,
            "group_user_id": null,
            "group_order_id": null,
            "group_form_id": null,
            "group_active": null,
            "category_id": null,
            "group_title": null,
            "event_group_active": null,
            "availability": "1"
          }
        ]
      }
    ]
  }

Line Items output

  "line_items": [
    {
      "id": 59,
      "name": "Al Khayran Camping",
      "product_id": 683,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "280.000",
      "subtotal_tax": "0.000",
      "total": "280.000",
      "total_tax": "0.000",
      "taxes": [],
      "meta_data": [
        {
          "id": 590,
          "key": "bookacti_bookings",
          "value": "[{\"id\":205,\"type\":\"single\"}]",
          "display_key": "Booking",
          "display_value": "Booking ID:205Status:PendingEvent:Camping Trip - Saturday, December 18th, 2021 6:00 PM → Sunday, December 19th, 2021 8:00 AM x1Actions:Cancel | Reschedule"
        }
      ],
      "sku": "CO-ADV-01",
      "price": 280,
      "parent_name": null,
      "cover_image": "https:\/\/static.capitano.om\/wp-content\/uploads\/2021\/08\/campingnight.jpg"
    },
    {
      "id": 60,
      "name": "Daymaniyat Islands",
      "product_id": 684,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "250.000",
      "subtotal_tax": "0.000",
      "total": "250.000",
      "total_tax": "0.000",
      "taxes": [],
      "meta_data": [
        {
          "id": 600,
          "key": "bookacti_bookings",
          "value": "[{\"id\":207,\"type\":\"single\"}]",
          "display_key": "Booking",
          "display_value": "Booking ID:207Status:PendingEvent:Morning Tour - Friday, December 17th, 2021 8:00 AM → 2:00 PM x1Actions:Cancel | Reschedule"
        }
      ],
      "sku": "CO-ADV-06",
      "price": 250,
      "parent_name": null
    }
  ]

Code snippet used to generate the Metadata

function get_order_bookings( $response, $object, $request ) {

    if( empty( $response->data ) )
        return $response;
    $order_id= $response->data['id'];

    $order_items_bookings = bookacti_wc_get_order_items_bookings( $order_id, array( 'fetch_meta' => true ) );
    $response->data['bookingdetails'] =$order_items_bookings;
    return $response;
} 

add_filter( "woocommerce_rest_prepare_shop_order_object", "get_order_bookings", 10, 3 );
yoancutillas commented 2 years ago

This function allows you to retrieve the bookings data bound to an order in a format specific to that function. Then, you are free to build another array with these data in the required format for your needs.

yabdali commented 2 years ago

This function allows you to retrieve the bookings data bound to an order in a format specific to that function. Then, you are free to build another array with these data in the required format for your needs.

@yoancutillas No worries, I was sharing my situation and asking for guidance since the key names are usually constants and not variables. I had rebuilt the structure to suit my needs. Thanks..