How to check WooCommerce thank you page - wordpress

In Wordpressis_page() can check page by ID,name or by slug but how can we check WooCommerce thank you page whether its a part of checkout page.
Also We have a lot of WooCommerce conditional tags but cant find something solve my issue
for example I try
if(is_page('checkout')) {
//some thing for only checkout page
}else if(is_page('thankyou') && !is_page('checkout')){
//some thing for only thank you page but not on checkout page
}else{
//some thing for all other page
}

This sample code may work:
if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) {
...
}

I think better to use endpoint like
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
//Get Order ID
$current_order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
echo $current_order_id;
}

Related

Need to get submission_id of elementor form to add in reference and content of the mail

I use the elementor pro form for my contact form. I want to add the submission_id to the mail. I want to display the id in the reference header and in the body of the mail. Like a ticket-System.
EXP: New Mail from xxx - ID: 4566334
I tried to use the following code to add a new shortcode, that gives back the submission_id, but ist not working. Is empty and dont have a value:
add_shortcode( 'submission_id', 'get_submission_id' );
function get_submission_id() {
if ( ! isset( $_POST['form_id'] ) || ! isset( $_POST['_wpnonce'] ) ) {
return '';
}
$form_id = intval( $_POST['form_id'] );
$nonce_value = sanitize_text_field( $_POST['_wpnonce'] );
if ( ! wp_verify_nonce( $nonce_value, 'elementor-pro-form-' . $form_id ) ) {
return '';
}
$submission_data = ElementorPro\Modules\Forms\Classes\Form::get_instance( $form_id )->get_submission_data();
if ( ! $submission_data || ! isset( $submission_data['_id'] ) ) {
return '';
}
return $submission_data['_id'];
}
Any other way to add the submission id from elementor form to the mails?
I tried different plugins, but i dont want to pay for a plugin, just to get the id.
Also tried to add my own shortcode the the function.php, but i doesnt give back a value.
UPDATE: It seems that this part doesnt work
if ( ! isset( $_POST['form_id'] ) || ! isset( $_POST['_wpnonce'] ) ) {
return 'Error 01';
}
I had the same problem. I solved it in the simplest way. My unique id is a code from date and time.
I added a hidden text field to the form. Then in the advanced tab I selected dynamic data, page, current time.
You can set any date/time format along with any character/word....

Wordpress redirect if URL parameter is empty for each post

So there are people who access my website directly (not thru the tracking link I got with Voluum), thus they are not able to click the links and I can't see them as a part of my stats.
How can I redirect users who don't have a /?voluumdata=BASE64... URL parameter to a tracked URL, and to have a different redirect for each blog post?
I was testing and looking for a plugin / .htaccess trick for hours but nothing seemed to help.
Edit: I found a solution that I was certain is going to work but for some reason it didn't:
[insert_php]
if(empty($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Also tried:
[insert_php]
if(!isset($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Both just break the page loading proccess.
Unfortunately, I cannot understand what is the purpose of the code you have entered in your question. I mean that is not clear the reason you use the tags [insert_php].
A solution to your problem it can be the following.
function redirect_direct_access( ) {
// You may use the code:
//
// global $wp_query
//
// in order to determine in which pages you should run your
// redirection code. If you only check for the token existence
// then you will be faced with redirection loop. I don't explain in
// depth how to use the $wp_query as it is not part of your question
// but you always have the opportunity to check what is the contents
// of this variable using the code:
//
// echo "<pre>";
// print_r( $wp_query );
// echo "</pre>";
//
// This way you will be able to build your specific if statement
// for the page you like to test.
if (
! isset( $_GET[ 'voluumdata' ] ) ||
empty( $_GET[ 'voluumdata' ] )
) {
wp_redirect( home_url( '/page/to/redirect/' ) );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );
You can find more information in the WordPress documentation related to the template_redirect hook.
Just in case anyone would face the same issue, #Merianos Nikos has given a half-answer and I mastered it into this:
function redirect_direct_access( ) {
$post_id = get_the_ID();
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );

How to use If condition for wordpress plugin Widget Logic

I'm trying to put an IF condition on a widget through the plugin Widget Logic.
if single post is 'this', then dispay this text.
I tried this but it doesn't work, do you know why ?
if ( is_single( 'dialyse-mayotte' ) ) {
echo 'test'; // conditional content/code
}
thanks for your help
is_single only checks if the page is single you should check if the page is this and is it a single page
if ( is_page( 'dialyse-mayotte') && is_single() ) { echo "test"; }

WooCommerce inquiry if no price available

I'm helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.
However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.
Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?
Had the exact same issue and couldn't find a plugin or a solution anywhere so I figured a workaround myself:
You need to edit file
/wp-content/themes/your-theme-name/woocommerce/single-product/add-to-cart/simple.php
(if it's not there just copy it from woocommerce plugin
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php)
and on line 14 where it says
if ( ! $product->is_purchasable() ) return;
you need to comment it out and write something like
if ( ! $product->is_purchasable() ) {
// put your code here
return;
}
and in the //put your code here line you can enter for example a shortcode for a form or a more complicated solution would be to put code for a button that when clicked will open up a popup form.
Still working on that ;)
Maybe too late, but I have had same issue currently.
Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404
Notice about: && '' !== $this->get_price()
/**
* Returns false if the product cannot be bought.
*
* #return bool
*/
public function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}
So you need to write a filter like this to override default:
add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );
Try the following code snippet. You just have to put it in your functions.php. You don't need a plugin or to overwrite WooCommerce files in your child theme.
// Inquiry link if no price available
function add_inquiry_link_instead_price( $price, $product ) {
if ( '' === $product->get_price() || 0 == $product->get_price() ) :
return ''.__( 'Jetzt anfragen' ).'';
endif;
}
add_filter( 'woocommerce_get_price_html', 'add_inquiry_link_instead_price', 100, 2 );

Wordpress is_page() for blog post listing page

I'm trying to use an is_page() conditional in Wordpress to detect whether or not the current page is the blog listing page (or a blog post page actually). The conditional works fine for any other page (contact, whats-on etc) but for some reason it doesn't work for the blog page.
Plus the actual blog post pages - URLs are www.domain.com/the-post-title format, so I can't check for 'blog' in the URL from $_SERVER or anything. Any help would be hugely appreciated.
We need to check:
if this page is blog page.
if this page is "page for posts"
function is_page_for_posts() {
$result = false;
if ( is_home() && ! is_front_page() ) {
$page = get_queried_object();
$result = ! is_null( $page ) && $page->ID == get_option( 'page_for_posts' );
}
return $result;
}
You may need to do a combination of is_home() - if your home page is the blog
or is_single() based on the type of blog post
reference: http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page
According to this article, this is how you use these kind of conditional functions:
if ( is_front_page() && is_home() ){
// Default homepage
} elseif ( is_front_page()){
//Static homepage
} elseif ( is_home()){
//Blog page
} else {
//everything else
}

Resources