Show Woocommerce order details on custom thank you page - wordpress

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/

Related

woocommerce thank you redirect after 3 seconds

i am using this snippet to redirect the customer after making the purchase.
add_action( 'woocommerce_thankyou', 'pfwp_redirect_woo_checkout');
function pfwp_redirect_woo_checkout( $order_id ){
$order = wc_get_order( $order_id );
$url = 'https://********/thank-you-for-your-purchase/';
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url );
exit;
}
}
the page content is GIF and an alert section that says thank you for your purchase and a button.
I want the default page of the woocommerce thank you to be the first one and after 3 seconds it redirects the customer to the custom page.
$url = 'https://********/thank-you-for-your-purchase/';
How can I do that?
I can't comment, but my advice would be to copy the WC thank_you template into your theme and have a query var trigger JS/jQuery on a timer.

Woocommerce: get and print customer language in the order page details

I need to create a hook to get and print the customer language in the admin order page details. I mean the language the customer selected in the site to process his order.
UPDATE: I use WPML for languages.
The closest I fount is this post but I can not see how it can help me: Get Woocommerce customer order language
I just to print the value somewhere in the Admin order page details.
I guess that the code should be something like:
function add_language(){
// get language for the order
// print language in Order Admin page
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'add_language' );
I have not tested this because I am not using the plugin but according to the information I have found. The postmeta table contains a meta_key wpml_language
function action_woocommerce_admin_order_data_after_order_details( $order ) {
// Get ID
$order_id = $order->get_id();
$wpml_language = get_post_meta( $order_id, 'wpml_language', true );
if ( ! empty ( $wpml_language ) ) {
echo 'lang = ' . $wpml_language;
} else {
echo 'not found!';
}
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'action_woocommerce_admin_order_data_after_order_details', 10, 1 );

Error with: WooCommerce Avoid add to cart for non logged user

I wondered if anyone can help me?
I am using a Wordpress site with Woocommerce plugin.
I am using a piece of code to avoid adding to cart for non logged in customers which I found on this site, it works great apart from one issue. It doesn't work on the product page. When you click the add to cart button, it doesn't redirect to the custom login page like it does if you press the button on the category view page. Instead the page just refreshes.
I put the code in to the functions.php file. I've then tried putting it into a few other places but that hasn't worked. Could anyone help me with this and let me know if there is another location I should be putting the code in? Thanks in advance, I'd really appreciate the help!
Here's the link to the question and the code is below: WooCommerce Avoid add to cart for non logged user
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Firstly, your function hook for woocommerce_loop_add_to_cart_link is incorrect. You are using conditionally_change_loop_add_to_cart_link rather than quantity_inputs_for_woocommerce_loop_add_to_cart_link.
Secondly, your URL for the link is using the current product page ID, which is going to point you at the current product page URL and not another page.
Other than that, you had it mostly correct with woocommerce_add_to_cart_validation.
EDIT:
For product single pages, if you look at content-single-product.php in Woocommerce, the action woocommerce_template_single_add_to_cart seems to handle what the "add to cart" form looks like. If you'd like to not show the add to cart form, you'll want to first remove the action from the woocommerce_single_product_summary hook.
if(!is_user_logged_in()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
Then add your own action in at that priority to put in your message:
add_action('woocommerce_single_product_summary', function() {
global $product_id;
if(!is_user_logged_in()) {
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
echo $message;
}
});
yes, you can do it by just adding following code into your active theme function.php file.
add_filter('woocommerce_get_price_html','login_before_addtocart');
function login_before_addtocart($price){
if(is_user_logged_in() ){
return $price;
}
else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
$response .= $price;
$response .= '<br> Login to add product into cart';
return $response;
}
}

redirect specific page category if not logged in

I'm sure this was working but now it's not. I added categories to pages with:
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
then tagged a range of pages with a 'client' category and added to functions:
add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) );
exit();
}
}
the intention, simply enough, is that not logged in users trying to directly access any of the 'client' pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there's a dozen pages and growing, so restricting by category is easier than by page ID array - but i can't see what i'm doing wrong.
all advice greatly appreciated!
From global variables pages in codex $pages will return:
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
And you need either post/page ID or object when using in_category() function as a second argument, as noted here.
You can try to get the category of the visited page by setting:
$current_cat = get_the_category();
And then check
in_category( $category_slug, $current_cat );
inside the conditional.

Wordpress event onAfterPostUpdated or onAfterPostCreated

Is there any way to be registered to wordpress events?
I want to know when a post was updated or created in the system, and get its ID.
Is it possible?
Thanks
You can use the save post hook. It is triggered when the post is created and updated.
Here's a sample.
function post_hook( $post_id ) {
if ( wp_is_post_revision( $post_id ) ){
//Post is being updated
} else {
//Post is being created
}
}
add_action( 'save_post', 'post_hook' );
Here's a link from the codex.
Save Post WP

Resources