Custom column to first order
-
Hi,
Is there anyway to write custom hook to make custom column and flag it with 0 or 1 if its the first order for that user for specific product?Thanks
-
hi
Yes, you can add new product field a described
https://algolplus.com/plugins/code-samples/#add_fieldsget all orders ( except current one) for this customer and check purchased items.
But I have no time to code it.
thanks, AlexThank you will try to do it 🙂
you can contact me via helpdesk if you need extra help
have a good weekend
hi
Did you solve this problem ?
Hi,
Yes I have fixed it.I have created custom fields for order (update_post_meta) and then I added it there by using Add field. Its a bit custom becoz I used subscription plugin with woo-commerce.Here is the code, hope it will help who need 🙂
add_action( 'woocommerce_renewal_order_payment_complete', 'add_custom_fields_order_data', 10, 2 ); add_action( 'woocommerce_thankyou', 'add_custom_fields_order_data', 10, 2 ); function add_custom_fields_order_data( $order_id ){ // If Current order is subscription renewal order if ( wcs_order_contains_subscription( $order_id, 'renewal' ) ) { // Set meta to 1 for renewal orders update_post_meta( $order_id, '_wc_acof_4', 1); update_post_meta( $order_id, '_wc_acof_5', 1); update_post_meta( $order_id, '_wc_acof_6', 1); }else{ update_post_meta( $order_id, '_wc_acof_4', 0); update_post_meta( $order_id, '_wc_acof_5', 0); update_post_meta( $order_id, '_wc_acof_6', 0); } // If Current order is subscription parent order = The first order from customer. if ( wcs_order_contains_subscription( $order_id, 'parent' ) ) { $products_to_check = array('2755','2678','2672'); // Check specific products by id $customer_bought = array(); $get_order = wc_get_order( $order_id ); $items = $get_order->get_items(); $user_id = $get_order->get_user_id(); $user_email = $get_order->get_billing_email(); // Current order // Save current products from current order. foreach ( $items as $item ) { $order_product_ids[] = $item['product_id']; } // Remove other products from order_product and keep just ones to check 2755,2678,2672 foreach ($order_product_ids as $key => $order_product_id) { if(!in_array($order_product_id, $products_to_check)) { unset($order_product_ids[$key]); } } // Old orders // Get all customer orders with meta_key _wc_acof_2 = 1 $customer_orders = wc_get_orders( array( 'limit' => -1, 'status' => array( 'wc-completed','wc-processing' ), 'customer' => $user_id, 'email' => $user_email, 'exclude' => array( $order_id ), // Exclude the current order. 'meta_key' => '_wc_acof_1', // use one meta _wc_acof_2 as all have same value. _wc_acof_2,_wc_acof_3,_wc_acof_4 'meta_value' => '1' )); if(count($customer_orders) > 0) { foreach($customer_orders as $customer_order) { //Check only if old order is Subscription type. if ( wcs_order_contains_subscription( $customer_order->ID, 'parent' ) ) { $c_order = wc_get_order($customer_order->ID); $c_items = $c_order->get_items(); foreach ( $c_items as $c_item ) { if(in_array($c_item['product_id'], $order_product_ids)) { $customer_bought[] = $c_item['product_id']; } } } } } // Already bought same subscription product. flag field = 0 if(!empty($customer_bought)){ update_post_meta( $order_id, '_wc_acof_1', 0); update_post_meta( $order_id, '_wc_acof_2', 0); update_post_meta( $order_id, '_wc_acof_3', 0); }else{ // Did not buy this subscription before. flag field = 1 update_post_meta( $order_id, '_wc_acof_1', 1 ); update_post_meta( $order_id, '_wc_acof_2', 1 ); update_post_meta( $order_id, '_wc_acof_3', 1 ); } }else{ // Not Subscription Product. flag field = 0 update_post_meta( $order_id, '_wc_acof_1', 0 ); update_post_meta( $order_id, '_wc_acof_2', 0 ); update_post_meta( $order_id, '_wc_acof_3', 0 ); } }wow, thank you.
No problem 🙂 thanks for your help too
The topic ‘Custom column to first order’ is closed to new replies.