从 Woocommerce 获取 Webhook 列表 [英] Get list of Webhooks from Woocommerce

查看:21
本文介绍了从 Woocommerce 获取 Webhook 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个 Wordpress 插件,其中包括在激活时创建多个 Woocommerce Webhook.这是使用内部 API 类和函数完成的,如下所示:

I have built a Wordpress plugin that among other things, creates several Woocommerce Webhooks upon activation. This is done using internal API classes and functions, as per below:

function createWebhook($userID,$topic,$secret,$deliveryURL,$status)
{
    $webhook = new WC_Webhook();
    $webhook->set_user_id($userID); // User ID used while generating the webhook payload.
    $webhook->set_topic( $topic ); // Event used to trigger a webhook.
    $webhook->set_secret( $secret ); // Secret to validate webhook when received.
    $webhook->set_delivery_url( $deliveryURL ); // URL where webhook should be sent.
    $webhook->set_status( $status ); // Webhook status.
    $save = $webhook->save();
    return $save;
}

效果很好.

我想要做的是在停用插件后删除这些 Webhooks.有什么方法可以通过内部 Wordpress 或 Woocommerce API 获取 Woocommerce Webhooks,以便我可以遍历并删除相关的 Webhook?

What I want to be able to do is remove these Webhooks upon deactivation of the plugin. Is there any way to fetch the Woocommerce Webhooks via the internal Wordpress or Woocommerce API, so I can loop through and remove the relevant ones?

我只想删除所有传递 URL 的域为 xyz.com 的 Webhook.这部分很简单,我只是不知道如何获取 Webhooks.

I would just remove all Webhooks where the delivery URL has a domain of xyz.com. This part is straight-forward, I just don't know how to fetch the Webhooks.

我不想使用需要 API 密钥和 HTTP 请求的外部 Woocommerce API.

I don't want to use the external Woocommerce API, which requires an API key and HTTP requests.

谢谢

推荐答案

您可以通过以下方式获取所有 webhook ID 的数组:

You can get an array of all webhook IDs with the following:

$data_store = WC_Data_Store::load( 'webhook' );
$webhooks   = $data_store->search_webhooks();

这就是 WooCommerce 在构建表格列表时所做的:

That's what WooCommerce does when building the table list:

https://github.com/woocommerce/woocommerce/blob/master/includes/admin/class-wc-admin-webhooks-table-list.php

这篇关于从 Woocommerce 获取 Webhook 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆