php - Wordpress/Woocommerce 数据发送到 Firebase 数据库

标签 php wordpress firebase woocommerce woocommerce-rest-api

我们有一款基于 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

有人对这种类型的集成有任何过期吗?

我们目前不需要双向同步,但一旦我们解决了这个问题,我们就会进行处理。

基本上只需要订单 ID、名字、姓氏、电子邮件和订单日期 + 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 中,您可以使用 Admin SDK 写入所需的 Firebase 数据库( Cloud FirestoreRealtime 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});
    });
  }

});

有关如何开始使用 Cloud Functions 的更多详细信息,请访问 https://firebase.google.com/docs/functions/get-started也可以通过观看视频系列 https://firebase.google.com/docs/functions/video-series (强烈推荐)。

关于php - Wordpress/Woocommerce 数据发送到 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60789334/

相关文章:

javascript - 如何在用户触发刷新按钮时重定向用户?

javascript - ComponentDidMount 在初始挂载后返回 undefined

php - 使用polylang时如何确定wordpress页面的当前语言?

php - MVC - 一个模型可以由其他几个模型组成吗?

php - 如何向默认 WordPress 注册添加密码字段?

java - 如何从 firebase 实时数据库中获取字符串数组

angular - 通过 spy /karma 模拟服务类时无法读取未定义的属性 'doc'

php - 使用 php 从源代码中提取 css 类和 ID

php - 增加标题的宽度

wordpress - 将网站从 wordpress 转换为 Plone 4