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.
php wordpress woocommerce product orders
add a comment |
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.
php wordpress woocommerce product orders
add a comment |
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.
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
80.1k125992
80.1k125992
asked Nov 21 at 1:45
bad boy
91112
91112
add a comment |
add a comment |
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.
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 |
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.
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 |
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.
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 |
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.
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
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
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 |
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