1

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!

NotAnExpert
  • 35
  • 1
  • 8
  • Its surely is possible, you can even do it via API, check there docs http://docs.woothemes.com/documentation/plugins/woocommerce/ http://docs.woothemes.com/document/woocommerce-rest-api/ http://docs.woothemes.com/document/hooks/ – silver Sep 10 '15 at 22:29

1 Answers1

2

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

        }
    }
}
Moax6629
  • 378
  • 4
  • 14
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Helga, thank you for the submission! It was a great step in the right direction. I am currently getting an error using this code - unmodified. The first line is causing this error: **Fatal error: Using $this when not in object context in /home/trick/public_html/wp-content/themes/canvas/functions.php** – NotAnExpert Sep 11 '15 at 00:05
  • Also, would this code work inside of there? $howmany = $item['qty']; $physical = $item['_physical-inventory']; $newphysical = $physical - $howmany; update_post_meta ( $item['product_id'], , '_physical-inventory', $newphysical ); – NotAnExpert Sep 11 '15 at 00:09
  • Can this execute at the same time that the stock quantity is reduced? – NotAnExpert Sep 11 '15 at 01:10
  • Sorry there was some rogue copy/paste code. That first line didn't belong. I've removed it. It appears that there is a hook that fires when the [stock is set](https://github.com/woothemes/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L248) however, I think it would fire every time the stock is manipulated and not necessarily only on checkout completed. – helgatheviking Sep 11 '15 at 01:45
  • Not sure about your code. It would depend on whether `$item['_physical-inventory']; ` is included in `$item`. I wouldn't think it would be there by default, but if you have added it via [`wc_add_order_item_meta()`](https://docs.woothemes.com/wc-apidocs/function-wc_add_order_item_meta.html) on the `woocommerce_add_order_item_meta` hook then you could retrieve it via `wc_get_order_item_meta()` – helgatheviking Sep 11 '15 at 01:51
  • Finally, I have it up to the point of doing the math using these lines: $physical = array_shift( wc_get_product_terms( $item['product_id'], 'pa_physical-inventory', array( 'fields' => 'names' ) ) ); $howmany = $item['qty']; $newphysical = $physical - $howmany; – NotAnExpert Sep 11 '15 at 03:12
  • I just need to figure out how to update the post meta - only once on the thank you page. Can I hook this to stock reduction somehow? That would be ideal for me. – NotAnExpert Sep 11 '15 at 03:12
  • **update_post_meta( $item['product_id'], '_physical-inventory', $newphysical );** is not working :( – NotAnExpert Sep 11 '15 at 03:31
  • **wp_set_object_terms($item['product_id'], $newphsyical, $key, false);** Will not work with variable $newphysical - but when I manually single quote a number in it - it updates fine. With Variable it updates blank :( – NotAnExpert Sep 11 '15 at 16:23
  • **wp_set_object_terms($item['product_id'], intval($newphsyical), $key, false);** not working either. I am going to lose my mind! This is the final last step to bringing this to completion and it's taking the longest lol. – NotAnExpert Sep 11 '15 at 17:16
  • I'm not really following where you are going with all of these comments. Can you edit your question to make it more clear. Alternatively I sometimes take premium support projects if you want to get in touch. – helgatheviking Sep 11 '15 at 19:34
  • All I want to do is update pa_physical-inventory by removing the number of items ordered. That's it. Where can I submit a premium ticket? – NotAnExpert Sep 11 '15 at 21:22
  • Helga, I tried multiple times to email you on your site but it will not submit :( – NotAnExpert Sep 12 '15 at 02:10
  • Really? Man, my website is annoying sometimes. I presume `pa_physical-inventory` is an attribute taxonomy? I'm unclear why you need to do a separate stock tally when WooCommerce already reduces stock? – helgatheviking Sep 12 '15 at 14:30
  • We are trying to have multiple stock locations - managed from inside WordPress. All of the solutions we found were all outside warehouse solutions which do not fit our needs. So we are doing custom attributes. Yes - it is a custom attribute. Can I contact you directly somehow? – NotAnExpert Sep 12 '15 at 16:10
  • I presume you tried my [contact page](http://kathyisawesome.com/contact)?then lets try this [SO chat](http://chat.stackoverflow.com/rooms/info/89479/so-32512552). I'm not actually sure how to start the SO chat, so hopefully that works. My contact form is preferred. You have to wait until the timer counts down and the send button is enabled. – helgatheviking Sep 12 '15 at 19:05