• Resolved bensonkarue

    (@bensonkarue)


    How do I access this data programmatically with the order object from wc_get_order( $order_id ); especially when getting the order items as shown below?

    function send_gift_card_coupon( $order_id ) {
    		// Getting an instance of the order object
    		$order = wc_get_order( $order_id );
    
    		$start = 0;
    		foreach ( $order->get_items() as $item_id => $item ) {
    
    			// HOW DO I ACCESS THE FIELDS HERE PLEASE
    			//????????????????
    
    			 $quantity  = $item->get_quantity();  
    			 $total_amount = $order->get_total();
    			 $user_id = $order->get_user_id();
    
    			 $user = get_userdata( $user_id );
    			 $email = $user->user_email;
    
    			 while( $start < $quantity ){
    			 	$this->wpccb_auto_create_coupon_for_gift_card( $this->wpccb_generate_gift_card_coupon_code(), $email, $total_amount, $order_id );
    			 	$start++;
    			 }
    
    		}
    	}
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author acowebs

    (@acowebs)

    You can use retrieve the data in two ways, directly with the meta key of each field, the key will be the field label.

    $item->get_meta('Field Label');


    If you want it get using field id or field name, It can retrieve from the addon order line item metadata with key _WCPA_order_meta_data, Find an example below

            $orderData = $item->get_meta('_WCPA_order_meta_data');
            if (is_array($orderData)) {
                foreach ($orderData as $sectionKey => $section) {
                    if (isset($section['fields']) && is_array($section['fields'])) {
                        foreach ($section['fields'] as $row) {
                            foreach ($row as $field) {
                                if ($field['name'] == 'Field Name here') {
                                    $requiredText = $field['value']; // you can get  required data here
                                }
                            }
                        }
                    }
                }
            }
    

    It iterates the data and finds the required fields with the field name.

    Thread Starter bensonkarue

    (@bensonkarue)

    Thank you acowebs for your answer

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Access fields with wc order object’ is closed to new replies.