wc_update_order_item not saving correctly











up vote
0
down vote

favorite












I'm trying to update the Item Name in the wp_woocommerce_order_items table and found the function wc_update_order_item to do the trick.



I want it to change to a randomly picked product. They main thing here is I already know the order_item_id that I want to change.



Here's my code:



    $order_item_id = array(1,2,3);
$num = 3;
$ctr = 0;

$products = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $num,
'orderby' => 'rand',
));

if ( $products->have_posts() ): while ( $products->have_posts() ): $products->the_post();

wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));

$ctr++;

endwhile; wp_reset_postdata(); endif;


The wc_update_order_item() is where it is supposed to update the order item name. It does update the order_item_name but not with the current $products->post->post_title value. It updates with with a random product title.



How do I know the title being saved is different from the current post_title inside the loop? If I echo $products->post->post_title inside the loop, it displays the current product name, as it should, but the updated order_item_name has a different value.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm trying to update the Item Name in the wp_woocommerce_order_items table and found the function wc_update_order_item to do the trick.



    I want it to change to a randomly picked product. They main thing here is I already know the order_item_id that I want to change.



    Here's my code:



        $order_item_id = array(1,2,3);
    $num = 3;
    $ctr = 0;

    $products = new WP_Query( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => $num,
    'orderby' => 'rand',
    ));

    if ( $products->have_posts() ): while ( $products->have_posts() ): $products->the_post();

    wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));

    $ctr++;

    endwhile; wp_reset_postdata(); endif;


    The wc_update_order_item() is where it is supposed to update the order item name. It does update the order_item_name but not with the current $products->post->post_title value. It updates with with a random product title.



    How do I know the title being saved is different from the current post_title inside the loop? If I echo $products->post->post_title inside the loop, it displays the current product name, as it should, but the updated order_item_name has a different value.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to update the Item Name in the wp_woocommerce_order_items table and found the function wc_update_order_item to do the trick.



      I want it to change to a randomly picked product. They main thing here is I already know the order_item_id that I want to change.



      Here's my code:



          $order_item_id = array(1,2,3);
      $num = 3;
      $ctr = 0;

      $products = new WP_Query( array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'posts_per_page' => $num,
      'orderby' => 'rand',
      ));

      if ( $products->have_posts() ): while ( $products->have_posts() ): $products->the_post();

      wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));

      $ctr++;

      endwhile; wp_reset_postdata(); endif;


      The wc_update_order_item() is where it is supposed to update the order item name. It does update the order_item_name but not with the current $products->post->post_title value. It updates with with a random product title.



      How do I know the title being saved is different from the current post_title inside the loop? If I echo $products->post->post_title inside the loop, it displays the current product name, as it should, but the updated order_item_name has a different value.










      share|improve this question















      I'm trying to update the Item Name in the wp_woocommerce_order_items table and found the function wc_update_order_item to do the trick.



      I want it to change to a randomly picked product. They main thing here is I already know the order_item_id that I want to change.



      Here's my code:



          $order_item_id = array(1,2,3);
      $num = 3;
      $ctr = 0;

      $products = new WP_Query( array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'posts_per_page' => $num,
      'orderby' => 'rand',
      ));

      if ( $products->have_posts() ): while ( $products->have_posts() ): $products->the_post();

      wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));

      $ctr++;

      endwhile; wp_reset_postdata(); endif;


      The wc_update_order_item() is where it is supposed to update the order item name. It does update the order_item_name but not with the current $products->post->post_title value. It updates with with a random product title.



      How do I know the title being saved is different from the current post_title inside the loop? If I echo $products->post->post_title inside the loop, it displays the current product name, as it should, but the updated order_item_name has a different value.







      php wordpress woocommerce product orders






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 2:52









      LoicTheAztec

      80.1k125992




      80.1k125992










      asked Nov 21 at 1:45









      bad boy

      91112




      91112
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Try the following that will get the related product IDs corresponding to the Order items, to exclude them from the WP_Query, avoiding having the same product names:



          $order_item_ids = array(1,2,3);
          $num = 3;
          $ctr = 0;

          $exluded_ids = array();

          // Loop through the Order items Ids
          foreach ( $order_item_ids as $item_id ) {
          // Get the order ID from the order Item ID
          $order_id = wc_get_order_id_by_order_item_id( $item_id );

          // Get the WC_Order object instance
          $order = wc_get_order( $order_id );

          // Get the WC_Order_Item_Product object instance
          $item = $order->get_item( $item_id );

          // Products IDs to be excluded from the WP_Query (array)
          $exluded_ids = $item->get_product_id();
          }

          $products = new WP_Query( array(
          'post_type' => 'product',
          'post_status' => 'publish',
          'posts_per_page' => $num,
          'orderby' => 'rand',
          'post__not_in' => $exluded_ids,
          ));

          if ( $products->have_posts() ):
          while ( $products->have_posts() ): $products->the_post();
          wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));
          $ctr++;

          endwhile;
          wp_reset_postdata();
          endif;


          It should work.





          Now as we don't know how you get the $order_item_id array, it's not possible to know what really happen. It should be better to explain what you are trying to do from the start.






          share|improve this answer





















          • Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
            – bad boy
            Nov 21 at 3:34










          • @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
            – LoicTheAztec
            Nov 21 at 3:47













          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',
          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%2f53404193%2fwc-update-order-item-not-saving-correctly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Try the following that will get the related product IDs corresponding to the Order items, to exclude them from the WP_Query, avoiding having the same product names:



          $order_item_ids = array(1,2,3);
          $num = 3;
          $ctr = 0;

          $exluded_ids = array();

          // Loop through the Order items Ids
          foreach ( $order_item_ids as $item_id ) {
          // Get the order ID from the order Item ID
          $order_id = wc_get_order_id_by_order_item_id( $item_id );

          // Get the WC_Order object instance
          $order = wc_get_order( $order_id );

          // Get the WC_Order_Item_Product object instance
          $item = $order->get_item( $item_id );

          // Products IDs to be excluded from the WP_Query (array)
          $exluded_ids = $item->get_product_id();
          }

          $products = new WP_Query( array(
          'post_type' => 'product',
          'post_status' => 'publish',
          'posts_per_page' => $num,
          'orderby' => 'rand',
          'post__not_in' => $exluded_ids,
          ));

          if ( $products->have_posts() ):
          while ( $products->have_posts() ): $products->the_post();
          wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));
          $ctr++;

          endwhile;
          wp_reset_postdata();
          endif;


          It should work.





          Now as we don't know how you get the $order_item_id array, it's not possible to know what really happen. It should be better to explain what you are trying to do from the start.






          share|improve this answer





















          • Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
            – bad boy
            Nov 21 at 3:34










          • @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
            – LoicTheAztec
            Nov 21 at 3:47

















          up vote
          1
          down vote













          Try the following that will get the related product IDs corresponding to the Order items, to exclude them from the WP_Query, avoiding having the same product names:



          $order_item_ids = array(1,2,3);
          $num = 3;
          $ctr = 0;

          $exluded_ids = array();

          // Loop through the Order items Ids
          foreach ( $order_item_ids as $item_id ) {
          // Get the order ID from the order Item ID
          $order_id = wc_get_order_id_by_order_item_id( $item_id );

          // Get the WC_Order object instance
          $order = wc_get_order( $order_id );

          // Get the WC_Order_Item_Product object instance
          $item = $order->get_item( $item_id );

          // Products IDs to be excluded from the WP_Query (array)
          $exluded_ids = $item->get_product_id();
          }

          $products = new WP_Query( array(
          'post_type' => 'product',
          'post_status' => 'publish',
          'posts_per_page' => $num,
          'orderby' => 'rand',
          'post__not_in' => $exluded_ids,
          ));

          if ( $products->have_posts() ):
          while ( $products->have_posts() ): $products->the_post();
          wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));
          $ctr++;

          endwhile;
          wp_reset_postdata();
          endif;


          It should work.





          Now as we don't know how you get the $order_item_id array, it's not possible to know what really happen. It should be better to explain what you are trying to do from the start.






          share|improve this answer





















          • Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
            – bad boy
            Nov 21 at 3:34










          • @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
            – LoicTheAztec
            Nov 21 at 3:47















          up vote
          1
          down vote










          up vote
          1
          down vote









          Try the following that will get the related product IDs corresponding to the Order items, to exclude them from the WP_Query, avoiding having the same product names:



          $order_item_ids = array(1,2,3);
          $num = 3;
          $ctr = 0;

          $exluded_ids = array();

          // Loop through the Order items Ids
          foreach ( $order_item_ids as $item_id ) {
          // Get the order ID from the order Item ID
          $order_id = wc_get_order_id_by_order_item_id( $item_id );

          // Get the WC_Order object instance
          $order = wc_get_order( $order_id );

          // Get the WC_Order_Item_Product object instance
          $item = $order->get_item( $item_id );

          // Products IDs to be excluded from the WP_Query (array)
          $exluded_ids = $item->get_product_id();
          }

          $products = new WP_Query( array(
          'post_type' => 'product',
          'post_status' => 'publish',
          'posts_per_page' => $num,
          'orderby' => 'rand',
          'post__not_in' => $exluded_ids,
          ));

          if ( $products->have_posts() ):
          while ( $products->have_posts() ): $products->the_post();
          wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));
          $ctr++;

          endwhile;
          wp_reset_postdata();
          endif;


          It should work.





          Now as we don't know how you get the $order_item_id array, it's not possible to know what really happen. It should be better to explain what you are trying to do from the start.






          share|improve this answer












          Try the following that will get the related product IDs corresponding to the Order items, to exclude them from the WP_Query, avoiding having the same product names:



          $order_item_ids = array(1,2,3);
          $num = 3;
          $ctr = 0;

          $exluded_ids = array();

          // Loop through the Order items Ids
          foreach ( $order_item_ids as $item_id ) {
          // Get the order ID from the order Item ID
          $order_id = wc_get_order_id_by_order_item_id( $item_id );

          // Get the WC_Order object instance
          $order = wc_get_order( $order_id );

          // Get the WC_Order_Item_Product object instance
          $item = $order->get_item( $item_id );

          // Products IDs to be excluded from the WP_Query (array)
          $exluded_ids = $item->get_product_id();
          }

          $products = new WP_Query( array(
          'post_type' => 'product',
          'post_status' => 'publish',
          'posts_per_page' => $num,
          'orderby' => 'rand',
          'post__not_in' => $exluded_ids,
          ));

          if ( $products->have_posts() ):
          while ( $products->have_posts() ): $products->the_post();
          wc_update_order_item($order_item_id[$ctr], array('order_item_name' => $products->post->post_title));
          $ctr++;

          endwhile;
          wp_reset_postdata();
          endif;


          It should work.





          Now as we don't know how you get the $order_item_id array, it's not possible to know what really happen. It should be better to explain what you are trying to do from the start.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 2:52









          LoicTheAztec

          80.1k125992




          80.1k125992












          • Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
            – bad boy
            Nov 21 at 3:34










          • @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
            – LoicTheAztec
            Nov 21 at 3:47




















          • Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
            – bad boy
            Nov 21 at 3:34










          • @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
            – LoicTheAztec
            Nov 21 at 3:47


















          Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
          – bad boy
          Nov 21 at 3:34




          Thanks. There are actually no duplicate product names being generated. It's just that a different value of $products->post->post_title is being saved than the one that is in the loop. I removed the 'orderby' => 'rand', in the query and the problems went away. I do need the products to be random though so it's not the same product every time.
          – bad boy
          Nov 21 at 3:34












          @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
          – LoicTheAztec
          Nov 21 at 3:47






          @badboy But your actual code is not testable as it doesn't allow to reproduce your issue. You should try to give the context in your question as we really don't catch the story. $order_item_id = array(1,2,3); doesn't help and $ctr = 0; is set to nothing.
          – LoicTheAztec
          Nov 21 at 3:47




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404193%2fwc-update-order-item-not-saving-correctly%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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...