1
votes

I need to execute some PHP for each product purchased (based on the ID), only on a successful checkout in WooCommerce. Does anyone know if this is possible, and if so - could you point me in the right direction?

Thanks!

1

1 Answers

2
votes

You could run a function on woocommerce_payment_complete. At that time you will have access to the $order_id which you can then use to retrieve the products in the order:

add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
function so_32512552_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item ) {

        if ( $item['product_id'] > 0 ) {
            $_product = $order->get_product_from_item( $item );
            // do something with the product

        }
    }
}