auto update coupon in hotel plugin when user purchase one on pw plugin

Free and Premium WordPress Plugins & Themes Forums WordPress Plugins Hotel Booking auto update coupon in hotel plugin when user purchase one on pw plugin

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1385570
    Mickael Quirin
    Participant

    Hello

    If anyone have the same configuration than me, this piece of code could be useful to you.

    I use pw gift card plugin to allow my users to purchase coupon code.
    The issue is that The coupons with pw gift card are made for woocommerce, but I want to use the coupon system of hotel booking plugin because using the woocommerce coupon system is not show in hotel booking invoice (you follow me :O).
    So I needed to copy manually the coupon code to hotel booking plugin each time.
    This time is over (haha!!!).

    The code below will check each time a woocommerce order is complete, if there is a coupon inside, it’ll add it to the hotel plugin automatically and mark the pw coupon as “already imported” to avoid double.

    important things if you want to use it:
    disable coupon field in woocommerce (in pw settings) otherwise the coupon could be used twice (in hotel and woocommerce).
    modify the code to fit your needs:
    -The product ID of my gift card is 1677, yours is NOT, find it and replace it in the code.
    -I make my coupon valid one year, if your don’t have limit remove the line update_post_meta($new_coupon_id, ‘_mphb_expiration_date’, date(‘Y-m-d’, strtotime(‘+1 year’)));

    <?php
    function add_coupon_on_mphb($order_id, $old_status, $new_status, $order)
    {
        if ($new_status == "completed")
        {
            $order = wc_get_order($order_id);
            $items = $order->get_items();
            foreach ($items as $item)
            {
                if ($item->get_product_id() == 1677 && $item->meta_exists("pw_gift_card_amount") && $item->meta_exists("pw_gift_card_number"))
                {
                    //item is a gift card, mine is 1677 and have the metadata needed, change it to yours
                    if (!$item->meta_exists("pw_is_gift_card_transfered"))
                    { //custom meta to know if card is already on mphb_db
                        $amount = $item->get_meta("pw_gift_card_amount");
                        $code = $item->get_meta("pw_gift_card_number");
                        $item->add_meta_data("pw_is_gift_card_transfered", 'yes');
                        $item->save_meta_data();
                        $new_coupon = array(
                            'post_type' => 'mphb_coupon',
                            'post_title' => $code,
                            'post_status' => 'publish',
                            'post_author' => 1,
                            'post_name' => strtolower($code)
                        );
    
                        if (!get_page_by_path($new_coupon['post_name'], OBJECT, 'page'))
                        { // Check If Page Not Exits
                            $new_coupon_id = wp_insert_post($new_coupon);
                            update_post_meta($new_coupon_id, '_mphb_amount', $amount);
                            update_post_meta($new_coupon_id, '_mphb_expiration_date', date('Y-m-d', strtotime('+1 year')));
                            update_post_meta($new_coupon_id, '_mphb_usage_limit', '1');
                            update_post_meta($new_coupon_id, '_mphb_type', 'per_accomm');
                        }
                    }
                }
            }
        }
    }
    add_action('woocommerce_order_status_changed', 'add_coupon_on_mphb', 99, 4);
    ?>
    

    if you need help don’t mind to ask here.
    As always, don’t modify anything if you’re not comfortable with this.

    Regards

    for using this code I recommand you to install code snippets plugin

    #1386419
    mark gravestock
    Participant

    Worked perfectly for me, thanks for sharing.

    #1386427
    Mickael Quirin
    Participant

    Glad it can help someone.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.