我们有一个基于Firebase的应用程序,我们的客户通过我们的网站购买了我们应用程序的订阅。
我们理想的情况是下订单后,发送以下内容:
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
任何人都不会使用这种集成吗?
目前,我们不需要2种方式的同步,但是一旦我们解决了这一点,稍后将进行处理。
基本上只需要order_id,first_name,last_name,email和order_date + 365天的到期时间-在完成订单/谢谢您的页面时将其发送到数据库。
谢谢,凯尔
编辑:
我想做这样的事情-但以前没有写过CURL-如果完全错误的话,我们深表歉意。
<?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 .'
}
}
?>
您可以使用Woocommerce webhooks来呼叫HTTPS Cloud Function。
您可以以接收POST HTTP请求的方式编写Cloud Function,并在Request的主体中传递所需的数据。
然后,在Cloud Function中,您使用Admin SDK以便写入所需的Firebase数据库(Cloud Firestore或Realtime Database)。
遵循以下内容的技巧将达到目的(未经测试):
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});
});
}
});
有关如何从https://firebase.google.com/docs/functions/get-started处的Cloud Functions开始以及通过观看https://firebase.google.com/docs/functions/video-series处的视频系列的更多详细信息(强烈推荐)。>