wc_update_order_item not saving correctly
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
add a comment |
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
add a comment |
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
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
php wordpress woocommerce product orders
edited Nov 21 at 2:52
LoicTheAztec
84.1k136095
84.1k136095
asked Nov 21 at 1:45
bad boy
91112
91112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 21 at 2:52
LoicTheAztec
84.1k136095
84.1k136095
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
add a comment |
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
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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