• Resolved tomzem

    (@tomzem)


    Hello I need to retrieve value of selected variable product from an order and used the following code:

    	$attributes = $product->get_attributes();
    	$variation_names = array();
    	if( $attributes ){
    		foreach ( $attributes as $key => $value) {
    			$variation_key =  end(explode('-', $key));
    			$variation_names[] = ucfirst($variation_key) .' : '. $value;
    			error_log(print_r(array("VARIATIONNAMES",$variation_names), true));
    		}
    	}

    With that, I am able to retrieve properly the name but the value is empty:

    [04-May-2021 07:59:21 UTC] Array
    (
    [0] => VARIATIONNAMES
    [1] => Array
    (
    [0] => pa_module :
    )

    )

    I can’t find the reason why, could you please help me ?
    Thank you

    • This topic was modified 3 years, 10 months ago by bcworkz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It looks like get_attributes() only gets attribute names and not associated values. I’m unfamiliar with WC product object property structure, but maybe you need a different method to get attribute values.

    Try var_dumping a product object’s properties. The output should reveal where the data you seek lies within. If you’re still stymied, I recommend asking through the WC dedicated support forum for assistance. The WC experts and devs there are in the best position to advise.

    Thread Starter tomzem

    (@tomzem)

    Hi ! just this reply to say you saved my day! (well, actually 3 days:-))

    Your answer routed me to the right direction, using var_dump I managed to locate where my data whee located and they are in the meta data, so to retrieve the value and names of attribute variable product in an order you can find them in the metadata of the order item:

    $metaData = $orderItem->get_meta(“pa_module”);
    $moduleID=print_r($metaData, true);

    I hope this will be helpful for other, thanks again for having taken the time to reply ??

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘woocommerce getting variable product attribute value from order’ is closed to new replies.