0

I am trying to add a custom variable to Woocommerce Follow-ups plugin. In the documentation they have created a sample function for this witch can be viewed here: https://gist.github.com/75nineteen/883d392e508a7745ab6c#file-custom_variables_two-php

What I am trying to do is create a variable from data created in the orders from anther plugin called “WooCommerce Admin Custom Order Fields”. In their documentation they give you a query line to get this data, and it states:

Once you know the id, you can use get_post_meta( $order_id, '_wc_acof_{id}', true ) to get the value of that custom field for a particular order.

As the custom order field I am trying to pull into the example has an id of 4, my get request ended up looking like this get_post_meta( $order_id, '_wc_acof_4', true )

So, I have ended up with the following code under here. This is supposed to create the variable {leverandor}. The sample script seems to work a little, as when I use this variable in the email template, I do at least get the value “13221”, witch I guess is for orders that dont have any data in this field. But there seems to be something wrong with the query line for pulling the custom order data in.

Is there anything obvious I am doing wrong here? 🙂

add_action( 'fue_before_variable_replacements', 'fue_register_variable_replacements', 10, 4);

function fue_register_variable_replacements( $var, $email_data, $fue_email, $queue_item ) { 
    $variables = array(
        'leverandor'    => ''
    );

    if ( isset( $email_data['test'] ) && $email_data['test'] ) {
        $variables['leverandor'] = '13221';

        if ( !empty( $email_data['order_id'] ) ) {
            $order = wc_get_order( $email_data['order_id'] );
            $leverandor = get_post_meta( $order_id, '_wc_acof_4', true );

            if ( !empty( $leverandor ) ) {
                $variables['leverandor'] = $leverandor;
            }
        }
    } else {
        if ( !empty( $queue_item->order_id ) ) {
            $order = wc_get_order( $queue_item->order_id );
            $leverandor = get_post_meta( $order_id, '_wc_acof_4', true );

            if ( !empty( $leverandor ) ) {
                $variables['leverandor'] = $leverandor;
            }
        }
    }

    $var->register( $variables );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.