skip to Main Content

We have an app built on Firebase and our customers purchase a subscription to our app via our website.

What we would ideally like to do is once the order is placed, send the following:

ID - primary key. This is generated using the order number from WooCommerce
card_number - string. This is the order number prefixed with #VIP (e.g #VIP12345). This is needed because of a legacy setup when we first launched. Hopefully it'll be phased out soon.
forename - string
surname - string
user_id - always an empty string. This is completed when the user first signs in.
email - string
expiry - int. The cards expiry date formatted as an epoch
business_reward_admin - always the string 'FALSE'
business_reward_user - always the string 'FALSE'
business_reward_id - always an empty string

Does anyone have any expirence with this type of integration?

We don’t need 2 way syncing at the moment, but will be working on that later on once we work this bit out.

Basically just need the order_id, first_name, last_name, email and the order_date + 365 days for the expiry – to be sent to the database when the order is completed/thank you page.

Thanks, Kyle

EDIT:

I would like to do something like this – but haven’t written CURL before – so apologies if completely wrong.

<?php

add_action('woocommerce_payment_complete', 'bnd_firebase_realtime_put', 10, 1);

function bnd_firebase_realtime_put ($order_id) {

    $order = new WC_Order( $order_id );

// Loop through cart items
    foreach( $order->get_items() as $item ){
        // Get the Product ID
        $order_id = trim( str_replace( '#', '', $order->get_id() ) );
        // Get Users First Name
        $firstname = $order->get_billing_first_name();
        // Get Users Last Name
        $lastname = $order->get_billing_last_name();
        // Get Users Email Address
        $email = $order->billing_email;
        //Epoch Time  + 365 days in Epoch time
        $expiry = time() + 31556926;
        //Get Product ID Number
        $product_id = $item->get_product_id();
        //Get Firebase Project ID number from Product Meta
        $project_id = get_post_meta($product_id,'project_id', true);
        //Get Firebase Access Token from Product Meta
        $access_token = get_post_meta($product_id,'access_token', true);


        curl -X PUT -d '{ "ID" : "'. $order_id . '", "card_number" : "#VIP'. $order_id . '" , "forename" : "' . $firstname .'" , "lastname" : "'. $lastname .'" , "email" : "' . $email . '" , "expiry" : "' . $expiry .'" }'  'https://'. $project_id .'.firebaseio.com/users.json?access_token=' . $access_token .'
    }
}

?>

2

Answers


  1. You could use Woocommerce webhooks in order to call an HTTPS Cloud Function.

    You can write your Cloud Function in such a way it receives POST HTTP Requests and you pass the desired data in the body of the Request.

    Then, in the Cloud Function, you use the Admin SDK in order to write to the desired Firebase database (Cloud Firestore or the Realtime Database).

    Something along the following lines should do the trick (untested):

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    
    exports.wooWebhook = functions.https.onRequest((req, res) => {
    
      if (req.method !== 'POST') {
        console.error('Request method not allowed.');
        res.status(405).send('Method Not Allowed');
      } else {
    
        const wooData = req.body.data;
    
        return admin.firestore().collection('wooOrders').add(wooData)
        .then(docRef => {
           res.status(200).send({result: 'ok'});
        })
        .catch(err => {
           res.status(500).send({error: err});
        });
      }
    
    });
    

    More details on how to start with Cloud Functions at https://firebase.google.com/docs/functions/get-started and also by watching the video series at https://firebase.google.com/docs/functions/video-series (highly recommended).

    Login or Signup to reply.
  2. you were right to mention this documentation: https://firebase-wordpress-docs.readthedocs.io/en/latest/save-data-realtime-firestore.html

    There is a Restful API under that plugin.

    curl --location --request POST 'http:/your-clouds-path.net/api-database/v1/createDoc' 
    --header 'api-token: the-api-token' 
    --header 'Content-Type: application/json' 
    --header 'source: dashboard' 
    --data-raw '{
        "dbType": "firestore",
        "collection": "ordersCollection",
        "docId": "your-order-id",
        "data": {
            "ID": "111",
            "card_number": "abc",
            "forename": "First Name"
        }
    }'
    

    It will help you to save your order under “ordersCollection” name in Firestore.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search