Getting orderID and total amount in Woocommerce? why so difficult? - wordpress

I added this function in my themes functions.php but it just not working.. any idea what i do wrong?
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
// $ttotal = $order->get_order_total();
//echo trim( str_replace( '#', '', $order->get_order_number() ) );
//$order_id = absint( $wp->query_vars['order-received'] );
//$order = new WC_Order($GLOBALS['post']->ID);
echo $order;
}
add_shortcode('Woocommerce-Thankyou', 'woocommerce_thankyou_fun');

Use this code to see all datamember of order object.
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
echo "<pre>";
print_r( $order);
echo "</pre>";
die();
}
add_shortcode('woocommerce-thankyou', 'woocommerce_thankyou_fun',20,1);

Related

Wordpress: Trigger a function when a new post published

I want a function that triggers when a new WordPress post publish.
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $post_id, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$edit = get_edit_post_link( $post_id, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
I tried this one. I tried also different statuses such as save_post but I didn't get any email when the user add a new post and publish it. so it doesn't work. how can I solve this problem?
You use the publish_post hook. If get_post_meta() returns a value, it will trigger your actions to occur.
function run_on_post_publish( $post_id ) {
$post_data = get_post_meta( $post_id, 'run_once', true );
if ( ! empty( $post_data ) ) {
return;
}
// Run the action you wish to occur when a post is published
update_post_meta( $post_id, 'run_once', true );
}
add_action( 'publish_post', 'run_on_post_publish' );

add wait before add action function is run

I am sending extra Mail after new woocommerce order.
I am using woo commerce_new_order hook.
Problem is that when the email arrives it hasn't had product info. I think that woocommerce_new_order hook fires before everything are stored in the database. Because if I run this with the existing order every info is included.
The question is how could I add a delay before data is fetched and email is sent?
add_action( 'woocommerce_new_order', 'extra_mail_after_new_order', 20, 1 );
function extra_mail_after_new_order( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$product_variation_id = $item->get_variation_id();
$product_data = $product->get_meta('extra_email')
}
add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
$to = 'mail.mail#gmail.com';
$subject = $product_name . ' uusi tilaus!';
$message = 'Order id: '. $order_id . '<br />product name: '. $product_name . '<br />product id: '. $product_id. '<br />product meta: '. $product_data. '<br />status: '. $status ;
wp_mail( $to, $subject, $message ); }
Problem was solved with this hook
woocommerce_booking_in-cart_to_pending-confirmation_notification

Updating ACF field with purchased product's term ids - WooCommerce

I am trying to update the a 'product_themes' user field in WooCommerce/WP with a function that collects the term_ids for the items in an order and adds them to the user's profile under the 'product_themes' field at the time when the order is placed. This is working when I specify an array of IDs in the code but does not seem to be collecting the term IDs from each item for a custom 'theme' taonomy.
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$item_id = $item['order_item_id'];
$product_name = $item['name'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}
I have consulted the WooCommerce documentation and searched for a solution on any similar problems but I cannot seem to get this to work. Am I missing something?
Many thanks.
I found the solution myself, changed $itemID = $item['order_item_id']; to $itemID = $item['product_id'];
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$itemID = $item['product_id'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
update_field('product_themes', $push, 'user_' . $userID);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}

How to get the vendor_ids for each of the vendors with products on the cart page in woocommerce

global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php

Woocommerce hook after product is published

I'm using below
add_action('transition_post_status', 'my_product_update', 1000, 3);
function my_product_update($new_status, $old_status, $post) {
if($new_status == 'publish' && $post->post_type == "product") {
$product_id = $post->ID;
$product = new WC_Product($product_id);
echo $product->get_price();
}
}
I figure it out that $product->get_price() is string(0) "".
I think it beacause I retrieve the price before it's saved.
But I can retrieve the name by using $product->post->post_title
Any idea how I can get the price right after the product is published?
Thank you.
Try This :
function wpa104760_default_price( $post_id, $post ){
echo $price = get_post_meta( $post_id, '_regular_price', true);
echo $sale = get_post_meta( $post_id, '_sale_price', true);
// exit;
}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price',1000,2 );

Resources