• Resolved lukasjan123

    (@lukasjan123)


    Hello,

    Thank you for a great plugin.

    I have a custom exporting field (custom_field_1).
    I want to change its value in exported file to string value ‘DKS’ if at least one of the order’s product has defined width, otherwise set it to string value ‘MKS’.

    I have this pseudo code, but not sure what is the best way to implement this feature.

    add_filter('woe_get_order_value_custom_field_1',function ($value, $order, $fieldname) {
    	$isDKS = false;
          	foreach ($order->get_items() as $item_id => $item_data) {
    			$product = $item_data->get_product();
    			$product_width = $product->get_width();
    			if( $product_width ) {
    				$isDKS = true;
    				break;
    			}
    		}
            if( $isDKS ) {
                return "DKS";
            } else
    	    return "MKS";
    },10,3);

    Hopefully you can help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author algol.plus

    (@algolplus)

    Hello

    this code looks ok. but it will run on each export.

    Will it be better use hook “woocommerce_new_order” + update_post_meta() ?
    and set custom_field_1 only once – after order creation ?

    thanks, alex

    Thread Starter lukasjan123

    (@lukasjan123)

    Hi Alex,

    Maybe that would be a better way, but I am not sure how I could accomplish that with your provided hooks.

    I wanted it to run only when exporting orders because I am not using this custom field anywhere else.

    However, when I add my provided custom php code to your plugin php snippet area, website server breaks when running export.

    Could there be an issue with the methods used in my code?

    This is the custom plugin export field I am trying to modify when exporting: https://ibb.co/g9mns7W

    Lukas

    Plugin Author algol.plus

    (@algolplus)

    hi Lukas

    I added your code , I added field “custom_field_1”.
    and I see DKS if any product has non-empty width.
    see my screenshot https://ibb.co/jLN4N0j

    Probably, the product was deleted .
    Please, try replace
    $product_width = $product->get_width();
    with
    $product_width = $product ? $product->get_width() : 0;

    thanks, Alex

    Thread Starter lukasjan123

    (@lukasjan123)

    Hi Alex,

    Thank you, it works now!

    Lukas

    Plugin Author algol.plus

    (@algolplus)

    you’re welcome

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exporting different custom field value based on product width’ is closed to new replies.