Search code examples
phpwordpresswoocommerceadvanced-custom-fieldsorders

Product ACF value is empty in additional column on WooCommerce My Account Orders


As a newcomer, I hope I'm posting this in the correct format. I'm familiar with WordPress but very new to PHP.

The Concept: A page link is added to a product using a custom field (course_content_page_url). Once a purchase is made and the order is generated on the Account page, this field gets added to the order page. A column then displays this URL, allowing the user to access it from the dashboard.

I've created a WooCommerce product with a custom field using ACF. This custom field data needs to be displayed on the Account page under the orders tab in a new column. I've managed to create the column and link it to my custom field, but it's not displaying the data from the field and defaults to "no url given".

Here's the article I followed but it doesn't seem to be working for me.

add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    $order_actions  = $columns['order-actions']; // Save Order actions
    unset($columns['order-actions']); // Remove Order actions

    // Add your custom column key / label
    $columns['course_content_page_url'] = __( 'Course URL', 'woocommerce' );

    // Add back previously saved "Order actions"
    $columns['order-actions'] = $order_actions;

    return $columns;
}

// Display a custom field value from order metadata
add_action( 'woocommerce_my_account_my_orders_column_course_content_page_url', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
    // Example with a custom field
    if ( $value = $order->get_meta( 'course_content_page_url' ) ) {
        esc_html_e( $value );
    } else {
        printf( '<small>%s</small>', __("no url given") );
    }
}

The screenshot: Account dashboard with no value showing

Is there any way to then add this custom field data to order emails as well?


Solution

  • As you are using Advanced custom fields on products, there is not any related metadata in the order itself, you need to get through order items, to get the product and then get the ACF value from this product.

    In the 2nd function, you will have to set the correct key in ACF get_field() function.

    Try the following:

    add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
    function add_account_orders_column( $columns ){
        $order_actions  = $columns['order-actions']; // Save Order actions
        unset($columns['order-actions']); // Remove Order actions
    
        // Add your custom column key / label
        $columns['course_url'] = __( 'Course URL', 'woocommerce' );
    
        // Add back previously saved "Order actions"
        $columns['order-actions'] = $order_actions;
    
        return $columns;
    }
    
    // Display a custom field value from order metadata
    add_action( 'woocommerce_my_account_my_orders_column_course_url', 'add_account_orders_column_rows' );
    function add_account_orders_column_rows( $order ) {
        $course_urls = array(); // Initialize
    
        // Loop though order items
        foreach ( $order->get_items() as $item ) {
            // Set the correct key For the ACF field below
            if ( $url = get_field( 'your-acf-key', $item->get_product_id() ) ) {
                $course_urls[] = sprintf( '<a href="%s">%s</a>', esc_url($url), __('Link') );
            }
        }
        // Display
        if ( count($course_urls) > 0 ) {
            echo implode('<br>', $course_urls);
        } else {
            printf( '<small>%s</small>', __("no url given") );
        }
    }
    

    It could work.