Create an order in WooCommerce using the S2Members API and wc_create_order

I am trying to use the S2Members Payment Notification API to create an order in WooCommerce while processing a payment.

I am going to ship my product once a month to all members who have paid for access this month, I am planning to export CSV from WooCommerce and send all orders for this month. I find this to be the easiest way to track my paid orders and ensure that all paying subscribers receive their product.

I use this as a basis for my work: http://www.s2member.com/kb/building-an-api-notification-handler/

In my S2Member notification url, I have 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 have 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; 
                    }
}

      

Custom fields are configured in S2Member for address and postal code and I have a product that got the id "99"

From what I can see it should be adding my recipient details to a new woocommerce order for 1 product "99" when a receipt notification is received from Stripe ... but I can't seem to get it to work

What am I missing?

+3


source to share


1 answer


Essentially, what I didn't do here was actually telling the array what variables are. Create variables using GET to parse the url and this works great.



0


source







All Articles