I am trying to limit access to the WooCommerce thank you page so the user can only view it once (At the moment you can paste the URL in another browser and see it still.)
I was thinking of creating/attaching a custom order meta to the order once the thank you page has loaded and then wrapping the whole page template in an if statement that checks if this meta exists. Thus when they paste it into another browser/window the template sees that this metas exists and shows them a different message.
Is this the right way to do it? This is what i have done so far but it doesnt work!
//functions.php
add_action( 'wp_footer', 'hasLoadedPlayPage', 20 );
function hasLoadedPlayPage( $order ){
if( !is_wc_endpoint_url( 'order-received' ) ) return;
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
Any guidance anyone could give me would be greatly appreciated!
Thanks
James
Looks good to me except that you need to use add_action('woocommerce_thankyou'... instead of wp_footer. But as woocommerce_thankyou only receives the order id as an argument, you'll need to use $order = wc_get_order($order_id) to get the WC_Order object. Something like:
//functions.php
add_action( 'woocommerce_thankyou', 'hasLoadedPlayPage', 20, 1);
function hasLoadedPlayPage( $order_id ){
$order = wc_get_order($order_id);
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
Related
I need to add a custom text that is displayed on the thank you page, if the order has been processed and has a completed status.
I am using the following code in the "woocommerce_order_details_before_order_table" hook and it works perfectly, but when I use it in the "woocommerce_thankyou" hook it generates errors.
add_action( 'woocommerce_thankyou', 'complete_custom_text' );
function complete_custom_text( $order ) {
$status = $order->get_status();
if(($status === 'completed')){
echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
}
}
If someone could help me I would appreciate it.
You are receiving order_id in that hook, use $order = wc_get_order( $order_id ); to get the order before reading the status.
Hi I created custom thank you page for my COD (cash on delivery) payment option. Redirect code below working.
add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
// Get the order ID
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Set HERE your Payment Gateway ID
if( $order->get_payment_method() == 'cod' ){
// Set HERE your custom URL path
wp_redirect( home_url( '/custom-page/' ) );
exit(); // always exit
}
}
}
Now in my /custom-page/ I want to display same details as in Woocommerce order-details.php. I created custom-page.php with(/* Template Name: COD Thank you page */) for my Thank you Page, copied code from original Woocommerce order-details.php but just loads blank page. Can someone help me to do this correctly please.
Thank you
Ensure that your new order-details.php has a URL and that that URL is added inside of WooCommerce > Settings > Advanced > Order Received. In there you would add the slug of that URL of your new order-details page.
There are also easier ways to create a custom thank you page using plugins such as this one: https://wordpress.org/plugins/yith-custom-thank-you-page-for-woocommerce/
I have a checkbox on a Wordpress admin options page that if checked I want to call a file. After hours of searching I am still coming up short. I am trying to use the advice here: https://github.com/WebDevStudios/CMB2/wiki/Tips-&-Tricks#using-cmb2-helper-functions-and-cmb2_init as well as a confitional statement inside. Is there any advice on what I am doing wrong?
function cmb2_init_check_field_value() {
$checkbox_value = cmb2_get_field_value( 'compel_option_metabox', 'compel_checkbox', get_queried_object_id() );
if($checkbox_value == yes) {
require_once( $this->directory_path . '/post-types/staff.php' );
require_once( $this->directory_path . '/post-types/sermons.php' );
}
}
add_action( 'cmb2_admin_init', 'cmb2_init_check_field_value' );
The cmb2_admin_init hook is too early,so you can not use get_the_ID() to get the post
I just encountered this as well. I couldn't figure out how to get the field value function to work. However, I did figure out how to get the value using the get_post_meta function.
eg:
$checkbox_value = get_post_meta( get_the_ID(), 'compel_checkbox', true);
if($checkbox_value == 'on') {
In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.
I'm developing a plugin and one of the issues I am running into is that I cannot get the post id within a function assigned to the admin_init hook.
I tried a few different methods; but, they all seem to use the $wp_query.
Below is a simple version the code I am using. I implemented the code like this just now and ran it by viewing the "post edit" page
add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article()
{
global $wp_query;
echo "<pre>";
print_r($wp_query);
echo "</pre>";
die();
}
$wp_query is a mostly empty array, notably, the post member is empty
-- EDIT --
I got some advice over at wordpress.stackexchange and added in this function:
function get_admin_post()
{
if( isset($_GET['post']) )
{
$post_id = absint($_GET['post']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
elseif( isset($_POST['post_ID']) )
{
$post_id = absint($_POST['post_ID']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
else
{
return false;
}
}
I think this answer will help. It states that the earliest action you can hook into, to get the global $post/$posts variables is the wp action. In the action hook reference on the codex, you can see that the wp action executes a bit after admin_init, which is why you can't retrieve any posts there, I think.
So, that should work:
add_action('wp','do_optional_featured_article');