WooCommerce - Add content on the thank you page by the shipping method - php

I would like to place my own content (in my case a shortcode) on the thank you page at WooCommerce after completing the order, which depends on the shipping method.
So for example:
Shipping Method 1: Content 1
Shipping Method 2: Content 2
Shipping Method 3: No content
I have already found something for text here, but i dont get the shortcode inserted. Alternatively I have tested what works with the shortcode here, where the dependency on the shipping method is missing.
Maybe someone can help me to implement this.
Ideally the content should be above the table with the order information.
Thanks a lot!

May be the way you added the shortcode to the thank you text can be the issue.
See the following shortcode function called avengers which i created for demo.
function call_avangers( $atts ){
return "<p>Avengers . . . . Assemble !!!</p>";
}
add_shortcode( 'avengers', 'call_avangers' );
You can display this shortcode in thank you page using the below function
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $text, $order ) {
if( $order->get_shipping_method() == 'your_shipping_method_here' ) {
$text .= do_shortcode('[avengers]');
}
return $text;
}
The basic thing is you have to call do_shortcode('your_shortcode_name')

Related

How to add a custom shortcode below the product table on woocommerce checkout page

Problem:
I need to add a shortcode [wc_sc_available_coupons] below the product table on the checkout page.
I added the following code in functions.php
The problem is the shortcode displays at the very bottom of the checkout form.
I changed the number 10 to 120 but still the same.
Would you please let me know how to add the shortcode below the product table (=above the payment)?
Code I tried:
add_action( 'woocommerce_after_checkout_form', 'wnd_checkout_code', 10 );
function wnd_checkout_code( ) {
echo do_shortcode('[wc_sc_available_coupons]');
}
Thank you.
Would woocommerce_checkout_after_customer_details hook work for you? So your code would be something like this:
add_action( 'woocommerce_checkout_after_customer_details', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
If not then you could try other hooks such as woocommerce_checkout_before_order_review or you could try this woocommerce_before_order_notes as well.
This one is right before the payment:
add_action( 'woocommerce_review_order_before_payment', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
Use the action:
add_action("woocommerce_after_cart_table", $action)
OR
add_action("woocommerce_before_cart_collaterals", $action)
Result
https://imgur.com/a/zQVNedb
In general for WooCommerce you can find hook info here:
https://woocommerce.github.io/code-reference/hooks/hooks.html
and for your template:
https://woocommerce.github.io/code-reference/files/woocommerce-templates-cart-cart.html#source-view.159
You can see information on the actions allowed by looking at the source code in woocommerce/templates/ and checking the do_action() functions.

Woocommerce | If page_id= .. { }

I am trying to apply specific functions to a certain page ID in Woocommerce.
However, it does not seem to apply. I have looked at both Wordpress and Woommerce conditional tags (https://docs.woocommerce.com/document/conditional-tags/).
The simple code I am testing is shown below, but even that is not working.
if ( is_product()) {
echo ' HALLO sdfdsfsdfsdfsdf ';
}
I have also tried with page_id and post_id but cannot get this to work.
I have added this code to my functions.php in my child theme.
Does anybody know to to write a specific function only for a specific product page (I know the value).
Method - 1
is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.
add_filter( 'template_include', function( $template ) {
if ( is_product() )
echo 'HELLO';
return $template;
});
Method - 2
The WordPress/WooCommerce conditional tags won’t work until after the wp action is fired, making it the earliest point at which you can use the tags. You can view the entire list, and the order of the core WordPress actions at the Action Reference page.
add_action( 'wp', 'init' );
function init() {
if ( is_shop() ) {
echo 'HELLO, this works!';
}
}

Add login form shortcode programatically to every published product page

I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues

Modify WooCommerce product columns

I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}

How to remove Text area of comment in Wordpress?

Hellow there all I tried searching everywhere but I coudn't just find it - may be because its a wiered requirement all I want to do is remove my Comment Text area in wordpress comment - I sucessfully removed URL website and name in the comment field by using following code
<?php
function remove_comment_fields($fields) {
unset($fields['url']);
unset($fields['author']);
unset($fields['email']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
?>
But not able to remove the Text area - Actually you all will thought what will I do removing all this I am using a plugin which allow you to post images in your comment and the only option I want to give to user is post images via comment. please guide me.
Two options, the comment_form_defaults filter:
add_filter( 'comment_form_defaults', 'so16856397_comment_form_defaults', 10, 1 );
function so16856397_comment_form_defaults( $defaults )
{
$defaults['comment_field'] = '';
return $defaults;
}
or the comment_form_field_comment filter:
add_filter( 'comment_form_field_comment', 'so16856397_comment_form_field_comment', 10, 1 );
function so16856397_comment_form_field_comment( $field )
{
return '';
}
Check the comment_form source code.
You can also hide the textarea comment box by taking its id set to display:none from CSS
Just simple, add this code in functions.php file
function disable_comments_everywhere( $open, $post_id ) {
return false;
}
add_filter( 'comments_open', 'disable_comments_everywhere', 10 , 2 );

Categories