I’m trying to adapt a piece of code, but so far the only thing I was able to do was to break woocommerce’s workflow.

So, I got a code that should send a user to one of my Sendy’s list when he completed a purchase on my website. Then I found out that whenever someone got to the end of my checkout process, this person would be added to the list, no matter if he still was on “pending payment” status.

This is the piece of code I think matters here:

    function __construct() {
    /**
     * Check if WooCommerce is active
     * */
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        add_action( 'woocommerce_review_order_before_submit', array( $this, 'add_optin_checkbox' ), 99 );
        add_action( 'woocommerce_checkout_order_processed', array( $this, 'get_lists_after_checkout' ), 10, 2 );
    }

}
function get_lists_after_checkout( $order_id, $posted ) {
        $settings=get_option( 'woocommerce_sendy_settings' );
        $lists =array();
        $email = $posted['billing_email'];
        $name=$posted['billing_first_name'].' '.$posted['billing_last_name'];

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {

        $product_id = $item['product_id'];
        $list_ids = get_post_meta( $product_id, 'sendy_product_lists', true );
        if ( !empty( $list_ids ) ) {
            $list_array = explode( ',', $list_ids );
            $lists = array_merge( $lists, $list_array );
        }
    }
    if ( !empty( $lists ) ) {
        $lists = array_unique( $lists );
        $this->add_user_to_sendy( $name, $email, $lists );
    }

}


function add_user_to_sendy( $name, $email, $listids, $custom_fields =array( '' ) ) {

if ( ! empty( $email ) ) {
    $settings=get_option( 'woocommerce_sendy_settings' );


    $sendy_url=rtrim( $settings['url'], '/' ).'/subscribe';

    $body= array( 'name' => $name, 'email' => $email, 'boolean' => 'true' );

    if ( !empty( $custom_fields ) ) {
        foreach ( $custom_fields as $key => $value ) {
            $body[$key]= $value;
        }
    }

    foreach ( $listids as $list_id ) {
        $body['list'] = $list_id;
        $response= wp_remote_post( $sendy_url, array( 'body' => $body ) );
    }

    if ( is_wp_error( $response ) ) {

        //return $error_message = $response->get_error_message();

    } else {

    //  return $response['body'];
    }

So, I guess that my problem was on the hook woocommerce_checkout_order_processed . And then I tried to change it to some hook that was later on the checkout process, as woocommerce_payment_complete_order_status , but not only the list was not fed, as apparently orders stopped completing upon payment received.

So, I’m guessing I’m doing something stupid, and I apologize if it’s too noob for this forum. Any help or guidelines would be really appreciated.

BR,

Diego

Read more here: Woocommerce hook – Completed Status Hook


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: