4

I'm hooking into the woocommerce subscription action events and the connection seems to be fine as I am getting a POST request but the payload only contains the following whereas I am expecting a full-bodied payload like when a webhook is fired from when the order is placed. Any ideas?

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

I am competent enough with PHP but I don't know where to start with wordpress. Any help would be appreciated

Update: So I have found the responsible code for creating the payload I am seeing, which is this:

    /**
 * 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;
}

Obviously it isn't smart to edit this code as it will be blown away with the next update to woocomm. How should I go about adding the params I need to the payload?

sanch
  • 696
  • 1
  • 7
  • 21
  • have you found any solution to this? Your post here is the only thing I could find that is similar to my problem. I'm implementing a webhook for "wcs_renewal_order_created" and the payloads inside their are empty as yours aswell. – Jonas Ostergaard Dec 18 '19 at 16:42
  • @JonasOstergaard Unfortunately not. Documentation is poor :( – sanch Dec 29 '19 at 16:21
  • @JonasOstergaard - Did you guys found any solution for this? – Dhiren Patel May 05 '20 at 15:12

2 Answers2

0

I had the same problem, but finally I understood how to do : the $subscription object serialize only the public members of the WC_subscrition object, so if you want to use the private field you must use the getter. In example, if you need the trial period expiration day from the woocommerce_subscription_payment_complete action, this is the way:

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 );

Last tip, keep attention on the number of arguments passed from action ( last parameter in add_action() )

0

To add to Mauro's answer, indeed only public variables in WC_Subscription object are serialized. I was able to include the id (or any other property) in the webhook payload by creating a new temporary public variable inside the WC_Subscription Class using the following code:

function fn_woocommerce_subscription_payment_complete ($subscription)
{
    $subscription->my_id=$subscription->get_id();          
}
add_action( 'woocommerce_subscription_renewal_payment_complete', 'fn_woocommerce_subscription_payment_complete', 10, 1 );