Viewed   112 times

In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (Like udemy).

When the customer checks out, I would like to send an API request and enroll the user for that particular course. I have tried several WooCommerce hooks but none worked for me.

This is the code that I'm using:

add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);

function enroll_student($order_id)
{
    echo $order_id;
    echo "Hooked";
}

I am writing this code for a plugin and to make it easier, I am currently using Cash on Delivery method.

Can anyone point me out where I am going wrong because when I checkout I cant see the message "hooked" that I am printing nor the $order_id?

It takes me to the success page and doesn't show these two things that I am printing.

 Answers

4

Update 2 Only For Woocommerce 3+ (added restriction to execute the code only once)

add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
    if ( ! $order_id )
        return;

    // Allow code execution only once 
    if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Get the order key
        $order_key = $order->get_order_key();

        // Get the order number
        $order_key = $order->get_order_number();

        if($order->is_paid())
            $paid = __('yes');
        else
            $paid = __('no');

        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {

            // Get the product object
            $product = $item->get_product();

            // Get the product Id
            $product_id = $product->get_id();

            // Get the product name
            $product_id = $item->get_name();
        }

        // Output some data
        echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';

        // Flag the action as done (to avoid repetitions on reload for example)
        $order->update_meta_data( '_thankyou_action_done', true );
        $order->save();
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Related thread:

  • Get Order items and WC_Order_Item_Product in WooCommerce 3
  • How to get WooCommerce order details

The code is tested and works.


Updated (to get the product Id from Orders items as asked in your comment)

May be you could use woocommerce_thankyou hook instead, that will display on order-received page your echoed code, this way:

add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {

    if ( ! $order_id )
        return;

    // Getting an instance of the order object
    $order = wc_get_order( $order_id );

    if($order->is_paid())
        $paid = 'yes';
    else
        $paid = 'no';

    // iterating through each order items (getting product ID and the product object) 
    // (work for simple and variable products)
    foreach ( $order->get_items() as $item_id => $item ) {

        if( $item['variation_id'] > 0 ){
            $product_id = $item['variation_id']; // variable product
        } else {
            $product_id = $item['product_id']; // simple product
        }

        // Get the product object
        $product = wc_get_product( $product_id );

    }

    // Ouptput some data
    echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The code is tested and works.

Then you can use all class WC_Abstract_Order methods on the $order object.

Related:

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in WooCommerce 3
    • How to get Customer details from Order in WooCommerce?
Tuesday, November 8, 2022
4

I have revisited your existing code a bit and added a dropdown filter for the "marketing optin" custom field:

//1. ADD OPT IN OPTION IN CHECKOUT AND SAVE IN THE ORDER

// Add checkbox optin before T&Cs
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'marketing_opting_field' );
function marketing_opting_field() {
    echo '<div id="marketing_opting_field">';
    woocommerce_form_field( 'marketing_opting', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Yes, sign me up'),
        'default'   => 1,
    ),  WC()->checkout->get_value( 'marketing_opting' ) );
    echo '</div>';
}

// Save the optin field as custom order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_create_order', 'action_checkout_update_order_meta', 10, 2 );
function action_checkout_update_order_meta( $order, $data ) {
    if( isset($_POST['marketing_opting']) )
        $order->update_meta_data( '_marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );
}

// Save the optin field as custom user meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer_meta', 10, 2 );
function action_checkout_update_customer_meta( $customer, $data ) {
    if( isset($_POST['marketing_opting']) )
        $customer->update_meta_data( 'marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );
}

// Display the result of the checked optin in the order under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
    if( $order->get_meta( '_marketing_opting' ) === 'yes' )
        echo '<p><strong>Has opted in for marketing purposes.</p>';
}

// 2. SHOW CUSTOM COLUMN FOR THE OPTIN OPTION

// Adding custom column title
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );
function custom_shop_order_column($columns)
{
    $action_column = $columns['order_actions'];
    unset($columns['order_actions']);

    //add the new column "Opt in"
    $columns['order_marketing'] = '<div align="center">' .__("Opted in?") . '</div>'; // title
    $columns['order_actions'] = $action_column;

    return $columns;
}

// Add the data for each order
add_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id ){
    global $post, $the_order;


    if ($column ==='order_marketing') { 
        $value = $the_order->get_meta( '_marketing_opting' );
        $label = $value === 'yes' ? __('Signed Up') : ucfirst($value);
        $color = $value === 'yes' ? 'color:#00cc00;' : 'color:#bbbbbb;';

        echo '<p align="center" style="'.$color.'"><span class="dashicons dashicons-'.$value.'"></span><span style="font-weight:600;">'.$label.'</span></p>';
    }
}

// 3. Make marketing optin meta searchable from search field (can't work very well for 'yes' or 'no' values!)

// Make a custom meta field searchable from the admin order list search field
add_filter( 'woocommerce_shop_order_search_fields', 'marketing_search_fields', 10, 1 );
function marketing_search_fields( $meta_keys ){
    $meta_keys[] = '_marketing_opting';
    return $meta_keys;
}

// 4. Add a dropdown filter to get orders by marketing optin meta value

// Add a dropdown to filter orders by Marketing optin
add_action( 'restrict_manage_posts', 'display_admin_shop_order_marketing_opting_filter' );
function display_admin_shop_order_marketing_opting_filter(){
    global $pagenow, $post_type;

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
        $domain    = 'woocommerce';
        $current   = isset($_GET['filter_shop_order_marketing'])? $_GET['filter_shop_order_marketing'] : '';

        echo '<select name="filter_shop_order_marketing">
        <option value="">' . __('Filter Marketing optin', $domain) . '</option>';

        $options = ['yes' => __('Signed Up'), 'no' => __('No')];

        foreach ( $options as $key => $label ) {
            printf( '<option value="%s"%s>%s</option>', $key, 
                $key === $current ? '" selected="selected"' : '', $label );
        }
        echo '</select>';
    }
}

// Process the filter dropdown for orders by Marketing optin
add_filter( 'request', 'process_admin_shop_order_marketing_opting_filter', 99 );
function process_admin_shop_order_marketing_opting_filter( $vars ) {
    global $pagenow, $typenow;

    if ( $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_marketing'] ) 
        && $_GET['filter_shop_order_marketing'] != '' && 'shop_order' === $typenow ) {
        $vars['meta_key']   = '_marketing_opting';
        $vars['meta_value'] = wc_clean( $_GET['filter_shop_order_marketing'] );
    }
    return $vars;
}

Note: I have change the order meta_key to _marketing_opting starting with an underscore as most other existing metakeys...

Also I have added a function that register that "marketing optin" value in user meta data, as it will be used by checkout on WC()->checkout->get_value( 'marketing_opting' ) for customers that have already made an order.

Field validation (optional)

If you make this checkout field required, you will need field validation… Then add the following:

// Custom Checkout field validation
add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation() {
    if ( isset($_POST['marketing_opting']) ) {
        wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong> | '.$_POST['marketing_opting'], 'error' );
    }
}    

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Tuesday, November 15, 2022
 
5

You will replace in your existing code the following block:

add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
    remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
    add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
    add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}

By this code block (keeping the 2 other functions):

add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
    remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );

    add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
    add_action( 'woocommerce_checkout_order_review', 'after_custom_checkout_payment', 9 );
    add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}

function after_custom_checkout_payment() {
    ?>
    <div id="before-order-table" class="woocommerce-checkout-custom-text">
        <h3><?php _e("Your order", "woocommerce"); ?></h3>
        <p><?php _e("I have accepted the terms and bla bla bla", "woocommerce"); ?></p>
    </div>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Saturday, August 6, 2022
2

For a restriction for a specific product per week you could use:

  • Based on the billing email address
  • wc_get_orders provide a standard way of retrieving orders - wc_get_orders and WC_Order_Query
function action_woocommerce_checkout_process() {
    // Only for guests
    if ( is_user_logged_in() ) return;
    
    // Isset
    if ( isset( $_POST['billing_email'] ) ) {
        // NOT empty
        if ( ! empty ( $_POST['billing_email'] ) ) {
            $customer_email = $_POST['billing_email'];  
        }
    }
    
    // Isset
    if ( isset ( $customer_email ) ) {      
        // Time in seconds (1 week)
        $time_in_seconds = 604800;
        
        // Set limit per week
        $limit = 2;
        
        // Specific product id
        $specific_product_id = 30;
        
        // Get orders from last week from customer by email
        $orders_last_week_by_customer_email = wc_get_orders( array(
            'date_created' => '>' . (time() - $time_in_seconds ),
            'customer' => $customer_email,
        ));
        
        // Total (counter)
        $total = 0;
        
        // Iterating through each order
        foreach ( $orders_last_week_by_customer_email as $order ) {
            // Going through order items
            foreach ( $order->get_items() as $item ) {
                // Get product ID
                $product_id = $item->get_product_id();
                
                // Compare
                if ( $specific_product_id == $product_id ) {
                    // Get quantity
                    $quantity = $item->get_quantity();
                    
                    // Add to total
                    $total += $quantity;
                }
            }
        }

        // Show error when total >= limit
        if ( $total >= $limit ) {
            wc_add_notice( sprintf( __( 'Guest users are not allowed to buy more than %d pieces of product with ID = %d in one week', 'woocommerce' ), $limit, $specific_product_id ), 'error' );
        }       
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
Sunday, September 25, 2022
 
bzlm
 
3

You can use a custom function hooked in woocommerce_checkout_order_processed action hook.
Since woocommerce 3.0+ version, here Is the corresponding core code located in process_checkout() function.

// Since WooCommerce version 3.0+
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

and below WooCommerce 3.0 version:

// Since WooCommerce version 2.1+ (before version 3.0+)
do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted );

So there is 2 cases depending which version of woocommerce you are using:

Since WooCommerce 3.0+ you can use 2 additional arguments in your hooked function and you will not need to create an instance of the order object as you get $order already as an argument.
You will be able also to access the posted data directly through $posted_data argument.

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 3);
function action_checkout_order_processed( $order_id, $posted_data, $order ) {
   // Do something
}

Since WooCommerce 2.1+ (Before WooCommerce 3.0), you have only the $order_id as argument, so you might be need to get an instance of $order object with wc_get_order() function:

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 1);
function action_checkout_order_processed( $order_id ) {
   // get an instance of the order object
   $order = wc_get_order( $order_id );

   // Do something
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Thursday, August 18, 2022
 
pesla
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :