Create an order in WooCommerce using S2Members API and wc_create_order

I’m trying to use S2Members ‘Payment Notification API’ to create an order in WooCommerce when a payment is processed.

I’m going to be batch shipping my product once a month to all members who’ve paid for access that month, I plan on exporting a CSV from WooCommerce and shipping to all of the orders for that month. I figure this is the easiest way to track my paid orders and ensure all paying subscribers receive their product.

Read More

I’m using this as a base for my work:
http://www.s2member.com/kb/building-an-api-notification-handler/

In my S2Member notification URL I’ve got the following:

http://www.MYURL.co.uk/?s2_payment_notification=yes&first_name=%%first_name%%&last_name=%%last_name%%&payer_email=%%payer_email%%&address_one=%%address_one%%&address_two=%%address_two%%&address_three=%%address_three%%&postcode=%%postcode%%

In my mu-plugin I’ve got the following PHP

<?php
add_action('init', 's2_payment_notification'); function s2_payment_notification()
{
    if(!empty($_GET['s2_payment_notification'])) 
                {
            if(!empty($_GET['first_name']) && !empty($_GET['last_name']) && !empty($_GET['payer_email']) && !empty($_GET['address_one']) && !empty($_GET['address_two']) && !empty($_GET['address_three']) && !empty($_GET['postcode'])) 
            { 

    $address = array(
        'first_name' => '%%first_name%%',
        'last_name'  => '%%last_name%%',
        'company'    => '',
        'email'      => '%%payer_email%%',
        'phone'      => '',
        'address_1'  => '%%address_one%%',
        'address_2'  => '%%address_two%%', 
        'city'       => '%%address_three%%',
        'state'      => '',
        'postcode'   => '%%postcode%%',
        'country'    => ''
    );

    $order = wc_create_order();
    $order->add_product( get_product( '99' ), 1 );
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    $order->calculate_totals();        
            }

            exit; 
                    }
}

The custom fields are set up in S2Member for the address and postcode and I have a product that’s got the ID ’99’

From what I can see, this should be adding my payees details to a new woocommerce order for 1 of product ’99’ when a payment notification is received from Stripe…but I can’t seem to get it working

What am I missing?

Related posts

Leave a Reply

1 comment

  1. Essentially what I wasn’t doing here was actually telling the array what the variables were. Create the variables by using GET to parse the URL and this works perfectly.