php - WooCommerce 订阅 webhook 有效负载为空

标签 php wordpress webhooks hook-woocommerce woocommerce-subscriptions

我正在连接 woocommerce 订阅操作事件,并且连接似乎很好,因为我得到了 POST请求,但有效负载仅包含以下内容,而我期待一个完整的有效负载,例如从下订单时触发 webhook 时。有任何想法吗?

{
   "action": "woocommerce_checkout_subscription_created",
   "arg": {
             "order_type": "shop_subscription"
   }
 }

我对 PHP 有足够的能力,但我不知道从哪里开始使用 wordpress。任何帮助,将不胜感激

更新:
所以我找到了负责创建我所看到的有效负载的代码,即:
    /**
 * Get WP API integration payload.
 *
 * @since  3.0.0
 * @param  string $resource    Resource type.
 * @param  int    $resource_id Resource ID.
 * @param  string $event       Event type.
 * @return array
 */
private function get_wp_api_payload( $resource, $resource_id, $event ) {
    $rest_api_versions = wc_get_webhook_rest_api_versions();
    $version_suffix    = end( $rest_api_versions ) !== $this->get_api_version() ? strtoupper( str_replace( 'wp_api', '', $this->get_api_version() ) ) : '';

    switch ( $resource ) {
        case 'coupon':
        case 'customer':
        case 'order':
        case 'product':
            $class      = 'WC_REST_' . ucfirst( $resource ) . 's' . $version_suffix . '_Controller';
            $request    = new WP_REST_Request( 'GET' );
            $controller = new $class();

            // Bulk and quick edit action hooks return a product object instead of an ID.
            if ( 'product' === $resource && 'updated' === $event && is_a( $resource_id, 'WC_Product' ) ) {
                $resource_id = $resource_id->get_id();
            }

            $request->set_param( 'id', $resource_id );
            $result  = $controller->get_item( $request );
            $payload = isset( $result->data ) ? $result->data : array();
            break;

        // Custom topics include the first hook argument.
        case 'action':
            $payload = array(
                'action' => current( $this->get_hooks() ),
                'arg'    => $resource_id,
            );
            break;

        default:
            $payload = array();
            break;
    }

    return $payload;
}

显然编辑此代码并不明智,因为它会在 woocomm 的下一次更新中被吹走。我应该如何将我需要的参数添加到有效负载中?

最佳答案

我遇到了同样的问题,但最后我明白了该怎么做:$subscription 对象只序列化 WC_subscition 对象的公共(public)成员,所以如果你想使用私有(private)字段,你必须使用 getter。例如,如果您需要 woocommerce_subscription_payment_complete 操作中的试用期到期日,则方法如下:

function fn_woocommerce_subscription_payment_complete ($subscription)
{
    'subscription_trial_end' => $subscription->get_date( 'trial_end' );
    //do whatever you want
}
add_action( 'woocommerce_subscription_payment_complete', 'fn_woocommerce_subscription_payment_complete', 10, 1 );
最后提示,注意从 Action 传递的参数数量( add_action() 中的最后一个参数)

关于php - WooCommerce 订阅 webhook 有效负载为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046292/

相关文章:

php - 有什么方法可以在 PHP 中安全地重新声明一个类?

php - 使用 MySQL 查询在 PHP 中计算时间

javascript - 计算一个 div 中的 div 数量,在一页 wordpress 帖子上有多个父 div

javascript - 为上一个和下一个分配图像源

java - Ngrok 上的 Web Hook ?

webhooks - 如何更改 MS 团队消息卡的文本颜色

github - CodeBuild <> GitHub - Hook 损坏

php - 是否写 $x;在 PHP 中意味着什么?

php - 优化大型Mysql数据库

css - 用于在 wordpress 上发布的上一个和下一个按钮