Woocommerce & Wordpress - How to access Order ID on woocommerce_checkout_create_order_line_item












0















I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.



However, I can't seem to access the ID of the order.



I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?



    add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );

function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
$_p = $item->get_product();
$key = 'Draw #';
$order_id = $order->id;
error_log( print_r( $order, true ) );

if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
{
$numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
error_log( print_r( $numbers, true ) );
$item->add_meta_data( $key , 1 , true );
}

}









share|improve this question



























    0















    I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.



    However, I can't seem to access the ID of the order.



    I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?



        add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );

    function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
    {
    $_p = $item->get_product();
    $key = 'Draw #';
    $order_id = $order->id;
    error_log( print_r( $order, true ) );

    if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
    {
    $numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
    error_log( print_r( $numbers, true ) );
    $item->add_meta_data( $key , 1 , true );
    }

    }









    share|improve this question

























      0












      0








      0








      I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.



      However, I can't seem to access the ID of the order.



      I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?



          add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );

      function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
      {
      $_p = $item->get_product();
      $key = 'Draw #';
      $order_id = $order->id;
      error_log( print_r( $order, true ) );

      if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
      {
      $numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
      error_log( print_r( $numbers, true ) );
      $item->add_meta_data( $key , 1 , true );
      }

      }









      share|improve this question














      I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.



      However, I can't seem to access the ID of the order.



      I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?



          add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );

      function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
      {
      $_p = $item->get_product();
      $key = 'Draw #';
      $order_id = $order->id;
      error_log( print_r( $order, true ) );

      if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
      {
      $numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
      error_log( print_r( $numbers, true ) );
      $item->add_meta_data( $key , 1 , true );
      }

      }






      php wordpress woocommerce






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      ShaunShaun

      240117




      240117
























          2 Answers
          2






          active

          oldest

          votes


















          1














          If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.



          function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) { 

          //HERE product_meta is just a random key I have used here, you have to use your key here
          if (isset ( $values ['product_meta'] )) {

          foreach ( $values ['product_meta'] as $key => $val ) {

          $order_val = stripslashes( $val );

          if($val) {

          if($key == 'your_cart_item_key') {
          $item->add_meta_data('Your Key',$order_val);
          }

          }

          }

          }

          }
          //This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.





          share|improve this answer































            0














            You can access order id using below code.



            $order_id = $order->get_order_number();


            Tested and works well






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54251486%2fwoocommerce-wordpress-how-to-access-order-id-on-woocommerce-checkout-create%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.



              function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) { 

              //HERE product_meta is just a random key I have used here, you have to use your key here
              if (isset ( $values ['product_meta'] )) {

              foreach ( $values ['product_meta'] as $key => $val ) {

              $order_val = stripslashes( $val );

              if($val) {

              if($key == 'your_cart_item_key') {
              $item->add_meta_data('Your Key',$order_val);
              }

              }

              }

              }

              }
              //This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.





              share|improve this answer




























                1














                If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.



                function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) { 

                //HERE product_meta is just a random key I have used here, you have to use your key here
                if (isset ( $values ['product_meta'] )) {

                foreach ( $values ['product_meta'] as $key => $val ) {

                $order_val = stripslashes( $val );

                if($val) {

                if($key == 'your_cart_item_key') {
                $item->add_meta_data('Your Key',$order_val);
                }

                }

                }

                }

                }
                //This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.





                share|improve this answer


























                  1












                  1








                  1







                  If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.



                  function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) { 

                  //HERE product_meta is just a random key I have used here, you have to use your key here
                  if (isset ( $values ['product_meta'] )) {

                  foreach ( $values ['product_meta'] as $key => $val ) {

                  $order_val = stripslashes( $val );

                  if($val) {

                  if($key == 'your_cart_item_key') {
                  $item->add_meta_data('Your Key',$order_val);
                  }

                  }

                  }

                  }

                  }
                  //This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.





                  share|improve this answer













                  If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.



                  function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) { 

                  //HERE product_meta is just a random key I have used here, you have to use your key here
                  if (isset ( $values ['product_meta'] )) {

                  foreach ( $values ['product_meta'] as $key => $val ) {

                  $order_val = stripslashes( $val );

                  if($val) {

                  if($key == 'your_cart_item_key') {
                  $item->add_meta_data('Your Key',$order_val);
                  }

                  }

                  }

                  }

                  }
                  //This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 13 hours ago









                  Risha TiwariRisha Tiwari

                  813




                  813

























                      0














                      You can access order id using below code.



                      $order_id = $order->get_order_number();


                      Tested and works well






                      share|improve this answer




























                        0














                        You can access order id using below code.



                        $order_id = $order->get_order_number();


                        Tested and works well






                        share|improve this answer


























                          0












                          0








                          0







                          You can access order id using below code.



                          $order_id = $order->get_order_number();


                          Tested and works well






                          share|improve this answer













                          You can access order id using below code.



                          $order_id = $order->get_order_number();


                          Tested and works well







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 17 hours ago









                          raju_ewwraju_eww

                          629315




                          629315






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54251486%2fwoocommerce-wordpress-how-to-access-order-id-on-woocommerce-checkout-create%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Callistus III

                              Plistias Cous

                              Index Sanctorum