I added new custom post type 'giftcard' and extended WooCommerce simple product by adding checkbox 'Gift Card'
Whenever order status changed to 'processing' and it contains product type giftcard, it creates new giftcard post by following code
function status_order_processing( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$is_gift_card = get_post_meta( $item['product_id'], '_woo_giftcard', true );
if($is_gift_card == 'yes'){
$token = base64_encode(openssl_random_pseudo_bytes(32));
$token = bin2hex($token);
$hyphen = chr(45);
$uuid = substr($token, 0, 8).$hyphen
.substr($token, 8, 4).$hyphen
.substr($token,12, 4).$hyphen
.substr($token,16, 4).$hyphen
.substr($token,20,12);
$gift_card = array(
'post_title' => $uuid,
'post_status' => 'publish',
'post_type' => 'giftcard',
);
$gift_card_id = wp_insert_post( $gift_card, $wp_error );
update_post_meta( $gift_card_id, 'woo_gift_card_amount', (int)$item['total'] );
}
}
add_action( 'woocommerce_order_status_processing', 'status_order_processing' );
New post name is a token generated in above code and save item total in meta field 'woo_gift_card_amount'.
Is there any way if I enter giftcard post type token in coupon field and it subtracts amount from Order amount according to meta field 'woo_gift_card_amount' of that post.
Any help would be appreciated.