Total : 0.00 €

Before contacting us, please make sure to check our F.A.Q

Please, login or register to our website in order to be able to post a presale question in the forum.

× WooPrice Calculator PRO

Access to field value on order

More
4 days 5 hours ago #2097 by Naidi
Naidi replied the topic: Access to field value on order
Hi Stefano,

in this case it will be something more complex, anyway here is the code :
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_checkout_create_order( $order, $data ) {
	$woopricecalculator = $GLOBALS['woopricecalculator'];
	$order_items = $order->get_items();

	foreach ($order_items as $id => $item){
		//print_r($item->legacy_values['simulator_fields_data']);


		foreach($item->legacy_values['simulator_fields_data'] as $field_key => $field_value){

			$field_id = str_replace("aws_price_calc_", "", $field_key);
			$field = $woopricecalculator->fieldModel->get_field_by_id($field_id);

			if(!empty($field_value)) {
				if (empty($field->label)) {
					$label = "[FIELD DELETED]";
				} else {
					$label = $field->label;
				}

				if($field->type == "picklist"){
					$picklistItems = $woopricecalculator->fieldHelper->get_field_picklist_items($field);

					foreach($picklistItems as $index => $item){
						if($field_value == $item['id']){
							$htmlElement = $item['value'];
						}
					}
				}else if($field->type == "radio"){
					$radioItems   = $woopricecalculator->fieldHelper->getFieldItems('radio', $field);

					foreach($radioItems as $index => $item){
						if($field_value == $item['id']){
							$htmlElement = $item['value'];
						}
					}
				}else if($field->type == "imagelist"){
					$imagelistItems   = $woopricecalculator->fieldHelper->getFieldItems('imagelist', $field);

					foreach($imagelistItems as $index => $item){
						if($field_value == $item['id']){
							$htmlElement = $item['value'];
						}
					}
				}else{
					$htmlElement = $woopricecalculator->orderHelper->getReviewElement($field, $field_value);
				}
			}
		}
	}
}

Best regards,
Naidi
AT1074

Please Log in or Create an account to join the conversation.

  • Maria Stella Malvaso
  • Maria Stella Malvaso's Avatar Topic Author
  • Offline
More
1 week 14 hours ago #2092 by Maria Stella Malvaso
Maria Stella Malvaso replied the topic: Access to field value on order
I don't get what I need. "getReviewElement" give me the label, while I need the value.

Please Log in or Create an account to join the conversation.

More
1 week 14 hours ago #2091 by bill
bill replied the topic: Access to field value on order
Hi Stefano,

above you will find the code that will work on your specific case:
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_checkout_create_order( $order, $data ) {
   $woopricecalculator = $GLOBALS['woopricecalculator'];
   $order_items = $order->get_items();

   foreach ($order_items as $id => $item){
       //print_r($item->legacy_values['simulator_fields_data']);


       foreach($item->legacy_values['simulator_fields_data'] as $field_key => $field_value){

           $field_id = str_replace("aws_price_calc_", "", $field_key);
           $field = $woopricecalculator->fieldModel->get_field_by_id($field_id);

           if(!empty($field_value)) {
               if (empty($field->label)) {
                   $label = "[FIELD DELETED]";
               } else {
                   $label = $field->label;
               }

               $htmlElement = $woopricecalculator->orderHelper->getReviewElement($field, $field_value);
           }

       }
   }

}

Best regards,
Bill
AT1074

Please Log in or Create an account to join the conversation.

  • Maria Stella Malvaso
  • Maria Stella Malvaso's Avatar Topic Author
  • Offline
More
1 week 2 days ago #2087 by Maria Stella Malvaso
Maria Stella Malvaso replied the topic: Access to field value on order
Hi, I'm trying with your code, but $woopricecalculator is empty.

I've simulator_id and field_id.

This is my code:

add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
function action_woocommerce_checkout_create_order( $order, $data ) {
$woopricecalculator = $GLOBALS;
$item = array_values($order->get_items())[0];
$simulator_id = $item->legacy_values;
$simulator_fields_data = $item->legacy_values;
error_log('$simulator_fields_data:'.print_r($simulator_fields_data,true));
foreach($simulator_fields_data as $meta_key => $field_data) {
$field_id = str_replace("aws_price_calc_", "", $meta_key);
error_log($woopricecalculator);
$field = $woopricecalculator->fieldModel->get_field_by_id($field_id);
error_log($field_id);
if(!empty($field_data) && !empty($field->label)){
//$label = $field->label;
error_log($field_id.'='.$field);
//TODO
}
}
};

Please Log in or Create an account to join the conversation.

More
2 weeks 3 days ago #2071 by bill
bill replied the topic: Access to field value on order
Hi ,

for the moment there is no direct hook you can implement to get this values on order created. But you can try this way,
as long as you are able to get the information of 'simulator_fields_data' inside that variable you can loop like this :

$woopricecalculator = $GLOBALS;
foreach($orderItem as $field_key => $field_value){

$field_id = str_replace("aws_price_calc_", "", $field_key);
$field = $woopricecalculator->fieldModel->get_field_by_id($field_id);

if(!empty($field_value)){
if(empty($field->label)){
$label = "[FIELD DELETED]";
}else{
$label = $field->label;
}

$htmlElement = $woopricecalculator->getReviewElement($field, $field_value, false);
}
}


so the variable $htmlElement will hold the value that you want to get.

Best regards.
Bill
AT1074

Please Log in or Create an account to join the conversation.

  • Maria Stella Malvaso
  • Maria Stella Malvaso's Avatar Topic Author
  • Offline
More
2 weeks 5 days ago #2063 by Maria Stella Malvaso
Maria Stella Malvaso created the topic: Access to field value on order
Ciao,
how can I access to the value of the field on shop_order? I need it during the shipping process.

It could be enough to have access to them during checkout, but on order I see only:
"simulator_fields_data":{"aws_price_calc_1":"1","aws_price_calc_2":"2","aws_price_calc_3":"on","aws_price_calc_14":"14\/02\/2019","aws_price_calc_4":"on","aws_price_calc_15":"06\/02\/2019","aws_price_calc_5":"on","aws_price_calc_16":"07\/02\/2019","aws_price_calc_6":null,"aws_price_calc_7":null,"aws_price_calc_8":null}

I need "aws_price_calc_1" a combobox, but 1 isn't the value but <option value="1">27/05/2019 - 03/06/2019</option>, which probably is used in the simulator to get the real value on excel data.

My idea is to hook "woocommerce_checkout_create_order( $order, $data )" to read value of aws_price_calc_1 and save them, because on order they aren't saved as I supposed (I find label of "aws_price_calc_1" "PERIODO" and the key of item "27/05/2019 - 03/06/2019", not its value).

Any suggestion?

Thanks

Please Log in or Create an account to join the conversation.

Moderators: david
Time to create page: 0.233 seconds

Cart

Total : 0.00 €