[SOLVED]Access to field value on order

Home – SaaS Forums Support questions WooPrice Calculator (Pro) [SOLVED]Access to field value on order

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #5186

    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”:{“[b]aws_price_calc_1[/b]”:”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, which probably is used in the simulator to get the real value on excel data.

    My idea is to hook “[b]woocommerce_checkout_create_order[/b]( $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

    #5194
    bill
    Participant

    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[‘woopricecalculator’];
    foreach($orderItem[‘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->getReviewElement($field, $field_value, false);
    }
    }

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

    Best regards.
    Bill
    AT1074

    #5193

    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_id’];
    $simulator_fields_data = $item->legacy_values[‘simulator_fields_data’];
    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
    }
    }
    };

    #5192
    bill
    Participant

    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

    #5191

    I don’t get what I need. “getReviewElement” give me the label, while I need the value.

    #5190
    Naidi
    Participant

    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

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.