• Resolved g3x123

    (@g3x123)


    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 6 years, 4 months ago by g3x123.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author pimwick

    (@pimwick)

    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

    (@pimwick)

    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.