WP All Export conditional fields with function

1k Views Asked by At

I use WP All Export to export invoice data to my accounting program, I would need to create a "Customer" field containing the _billing_company field in case I have data or $_billing_first_name". ".$_billing_last_name in case there is no company name.

The function is as follows:

<?php
function company_conditioning($_billing_company, $_billing_first_name, $_billing_last_name) {
    if(strlen($_billing_company) < 2) {
        return $_billing_first_name." ".$_billing_last_name;
    } else {
        return $_billing_company;
    }
}
?>

Add Field for Export

When I save the function, everything is OK, but when I execute the results, WP All Export gives "An unknown error occured" error

I've noticed what happens when I put more than one variable into function

For example, this function works perfectly, (If there is a company name it, shows it, and if not it shows OK)

<?php
function company_conditioning($_billing_company) {
    if(strlen($_billing_company) < 2) {
        return 'ok';
    } else {
        return $_billing_company;
    }
}
?>

I need to export it in .xls (it does not work in XML)

As I indicate below, the problem is that WP All import does not handle two variables in the functions. So I find big problems when trying to export coded data.

The problem is that you have to call two fields that are in the wp_postmeta table. I tried calling $post_id and then calling the two fields with get_post, but I couldn't get it to work.

<?php
function cliente_jr( $post_id ) {
    $empresa_jr = get_post( $post_id, 'billing_company', true );
    $nombrefac_jr = get_post( $post_id, 'billing_first_name', true );
    $apellidofac_jr = get_post( $post_id, 'billing_last_name', true );
    if(strlen($empresa_jr) < 2) {
        return $nombrefac_jr." ".$apellidofac_jr;
    } else {
        return $empresa_jr;
    }
}
?>

Any ideas?

Edit. Another example If $payment_method is COD, put the value of $order_total, else put 0

3

There are 3 best solutions below

0
On

I have been able to solve it IF THE CUSTOMER IS REGISTERED, the main problem is that the functions in WP ALL Export do not accept more than one variable, so the results have to be called from the function.

If the client is registered, since I can obtain the data from get_user_meta, but if the client is not registered, the billing_company field is not obtained

In case any user might find it useful. This works perfectly, IF THE CUSTOMER IS REGISTERED, then if the billing_company field is empty, it shows the name and surname, if the billing_company field has data, it shows billing_company

IMAGEN

<?php
function cliente_jr( $customer_id ) {
    $empresa_jr = get_user_meta( $customer_id, 'billing_company', true );
    $nombrefac_jr = get_user_meta( $customer_id, 'billing_first_name', true );
$apellidofac_jr = get_user_meta( $customer_id, 'billing_last_name', true );
    if(strlen($empresa_jr) < 1) {
        return $nombrefac_jr." ".$apellidofac_jr;
    } else {
        return $empresa_jr;
    }
}
?>
0
On

I have already managed to solve it for any field that is on the 'postmeta' table

The first thing you must ALWAYS select in "Select a field to export" is "ORDER ID", whatever field you are going to get.

enter image description here

Once we have the order number, we can execute different calls to all the data we have in the postmeta.meta_key field

For example, in this case, I would like to obtain the company name if the client has indicated it and if not (the company field is empty) put their name and surname.

<?php
function cliente_jr( $post_id ) {
    $empresa_jr = get_post_meta( $post_id, '_shipping_company', true );
    $nombrefac_jr = get_post_meta( $post_id, '_shipping_first_name', true );
    $apellidofac_jr = get_post_meta( $post_id, '_shipping_last_name', true );
    if(strlen($empresa_jr) > 1) {
        return $empresa_jr;
    } else {
        return $nombrefac_jr." ".$apellidofac_jr;
    }
}
?>

From there any field of the postmeta table can be called without problems, I have applied it for different results and formulas and in all of them it works perfectly.

I hope it will be useful for you in the future.

1
On

You can use a custom export field and the WP Allexport shortcode, ie:

  [company_conditioning_first( {Billing Company},{Shipping First Name},{Shipping Last Name})]

No need to use the ORDER ID-field. See screenshot from two custom fields in working export width three variables.

The function:

function company_conditioning_second($_shipping_company,$_shipping_firstname,$_shipping_lastname){if(strlen($_shipping_company) > 2){return $_shipping_firstname.''.$_shipping_lastname;}}

Picture examples custom export field