Get View Order URL for custom email in WooCommerce - wordpress

I want to get the URL of the Order Details page from where user can see their order details, because using some 3rd party API I send mail to the customer after the product is shipped.
I tried this code
$order = wc_get_order(109);
$public_view_order_url = esc_url( $order->get_view_order_url() );
echo $public_view_order_url; //http://example.com/my-account/view-order/109/
but the URL generated by ^^ above code only works for logged in customer. Is it possible to get a Public URL so that user don't have to logged in because most of the customer don't have any account. I know for security reason email id and invoice number is needed.
One solution that I think is by creating an custom page which will accept order_id, order_key and email_id in GET parameter and query it and display the result; the whole thing I have to create but is there any WooCommerce function/hook for this?
I googled it and also spend time in Woo doc but the result was negative,
Any help or suggation will be very helpfull for me.
Thanks.

Not sure if I'm missing something, but it sounds like what you want can be achieved with the Order Received URL:
$order->get_checkout_order_received_url()
This would give you something like https://example.com/checkout/order-received/12345/?key=wc_order_1ab2cd3ef4g, which is a direct link to the order receipt shown after a successfully processed order.
This link will show billing & shipping details, order details & payment method by default, and doesn't require that the customer has an account or for them to be logged in.

I couldn't find a way to do that so I just put a condition in my email. If they have an account, link to the order, if they don't encourage them to make an account for the next order.
<p><?php
$email_order_url =$order->get_view_order_url();
if ( $order->get_user() ) {
_e( "Your message with <a href='" . $email_order_number . "'>Order
Number'</a>.", 'woocommerce' );
} else {
_e( "Message to encourage them to create an account on the regular
accounts page" , 'woocommerce' );
}

Everything you need is in post meta, this works for me when in a loop that can access get_the_ID():
<?php $the_order_id = get_the_ID();
$the_order_key = get_post_meta($the_order_id, '_order_key', true); ?>
https://yoursiteurl.com/checkout/order-pay/<?php echo the_order_id; ?>/?pay_for_order=true&key=<?php echo $the_order_key; ?>
Otherwise, if you can replaced get_the_ID() with the Order ID in some other way (however you're getting 109 in your initial question), that would work as well.

Related

ACF User fileld for subscriber not return value

I'm using WordPress ACF plugin to store users' data.
The values I'm calling in template are as below:
echo $uid=get_current_user_id();
echo $lesson_order = get_field('lesson_order', 'user_'.$uid);
echo $last_lesson_time = get_field('last_lesson_time', "user_".$uid);
The values returned for admins are correct, but when logged in as a subscriber, the code returns empty values. I also checked user ids, which appear to be correct.
Can someone give me a solution to this. Thanks a lot.
I've tested both option on a local project and both were working but have you tried retrieving the user ID using this instead?
<?php
$current_user = wp_get_current_user();
echo "Current User ID " . $current_user->ID;
?>
If it still doesn't work, can you show a little more of your code?
You can also check that answer for more information : https://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0

How to get Pay Now URL with custom order status in WooCommerce?

I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callbackĀ 
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filterĀ 
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();

add_post_meta prints out '6' on my page

What I am trying to do is each time a logged in user opens single.php I log a post meta activity. Essentially I want to know every time a logged in user reads a post. So I put this in single.php inside the loop and after having checked if user is logged in.
First I got just '6' (which is the post ID) in my post, so I added some echo to find which function was doing that and this is what resulted.
echo "before post meta<br />";
add_post_meta( the_ID(), 'post_read', (string)$current_user->ID );
echo "after post meta<br />";
prints out in my post:
before post meta
6after post meta
Why is add_post_meta printing '6' (the post ID)? How can I get rid of it? An incidentally when I:
$post_reads = get_post_custom_values('post_read', the_ID());
All I get is '6' again.
the_ID is printing out the post ID. Use get_the_ID to return it without printing.
You are using the_ID() in your function call. the_ID() displays the id to screen. You want to use get_the_ID() instead. This will give you a usable ID you can use for your function call.

Wordpress: Change user role conditionally

I am creating a Wordpress website for multi author and want to set user role as per article submission. Means if any user have 0-10 article they will go to Contributor role, if 11-30 will go to Author role if 31-100 will go to Editor role.
Also I want to make registration system where default registration group will be Subscriber. They will get a link into verification email like
If you want to become a Contributor please click on below link. (To submit an article you must have at least Contributor permission)
http:// link will be here ... this link automatically change user role from Subscriber to Contributor.
Hope I will get solution from you expert. I am posting this issue with lots of hope from you friends.
What you want to do is when they post their submission check to see how many posts they have authored and then change the role. So in your theme's functions.php file you'd need a hook that is like this.
add_action('publish_post', 'update_roles');
and then a function to update the roles.
function update_roles()
{
global $wpdb;
// Get the author
$author = wp_get_current_user();
// Not sure if $author and $u are the same object I suspect they are.
// so this may not be necessary, but I found this code elsewhere.
// You may be able to do without this and just replace $u with $author later in the code.
// Get post by author
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );
$numPost = count($posts);
// Do the checks to see if they have the roles and if not update them.
if($numPost > 0 && $numposts <= 10 && current_user_can('subscriber'))
{
// Remove role
$author->remove_role( 'subscriber' );
// Add role
$author->add_role( 'contributor' );
}
...... other conditions .......
}
Using SQL statements (Database Queries) to get at Wordpress data is not in accordance with Wordpress coding standards . See Wordpress Handbook
It would be better to use count_user_posts function
See Function on the codex

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources