Title: Custom PHP action on sell
Last modified: June 14, 2018

---

# Custom PHP action on sell

 *  Resolved [g3x123](https://wordpress.org/support/users/g3x123/)
 * (@g3x123)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/custom-php-action-on-sell/)
 * Hi again, another question…
 * Is it possible to run an extra PHP action on sell, like include an external PHP
   file? I have to store the gift card ID to an external database.
 * Kind regards,
    Kevin
    -  This topic was modified 7 years, 10 months ago by [g3x123](https://wordpress.org/support/users/g3x123/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [pimwick](https://wordpress.org/support/users/pimwick/)
 * (@pimwick)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/custom-php-action-on-sell/#post-10400790)
 * Use the WooCommerce hook “woocommerce_order_status_completed”. Then to identify
   if the order contains a gift card, you can loop over the items like this (note,
   this hasn’t been tested so you may have to tweak it):
 *     ```
       function store_gift_card_in_external_database( $order_id, $order ) {
           foreach ( $order->get_items( 'line_item' ) as $order_item_id => $order_item ) {
               // Get the product.
               $product_id = absint( $order_item['product_id'] );
               if ( !( $product = wc_get_product( $product_id ) ) ) {
                   continue;
               }
   
               // We're only interested in gift cards.
               if ( !is_a( $product, 'WC_Product_PW_Gift_Card' ) ) {
                   continue;
               }
   
               // Get the gift card numbers for this order line item. The customer can purchase more than 1 card at a time, which is why this is an array.
               $gift_card_numbers = (array) wc_get_order_item_meta( $order_item_id, PWGC_GIFT_CARD_NUMBER_META_KEY, false );
   
               //
               // Do whatever you want with the $gift_card_numbers array.
               //
   
           }
       }
       add_filter( 'woocommerce_order_status_completed', 'store_gift_card_in_external_database', 30, 2 );
       ```
   
 *  Plugin Author [pimwick](https://wordpress.org/support/users/pimwick/)
 * (@pimwick)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/custom-php-action-on-sell/#post-10427369)
 * Haven’t heard back so marking this as “Resolved” but just let us know if you 
   need anything else!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom PHP action on sell’ is closed to new replies.

 * ![](https://ps.w.org/pw-woocommerce-gift-cards/assets/icon-256x256.png?rev=1809373)
 * [PW WooCommerce Gift Cards](https://wordpress.org/plugins/pw-woocommerce-gift-cards/)
 * [Support Threads](https://wordpress.org/support/plugin/pw-woocommerce-gift-cards/)
 * [Active Topics](https://wordpress.org/support/plugin/pw-woocommerce-gift-cards/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pw-woocommerce-gift-cards/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pw-woocommerce-gift-cards/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [pimwick](https://wordpress.org/support/users/pimwick/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/custom-php-action-on-sell/#post-10427369)
 * Status: resolved