Show woocommerce errors - woocommerce

I have a woocommerce store and want to show wocommerce error messages in specific place of my theme, for example under submit button.
I know wocommerce error messages come from here:
<ul class="woocommerce-error">
<?php foreach ( $messages as $message ) : ?>
<li><?php echo wp_kses_post( $message ); ?></li>
<?php endforeach; ?>
</ul>
But, when i put this code in my page (for example edit-my-address.php page), its giving error!
What is working or simple code for showing messages in woocommerce?
Thanks in advance.

you can use both methods here..
woocommerce_show_messages();
wc_print_notices();
You can also show woocommerce custom notices according to your condition like...
wc_add_notice( 'This is a Success notice', 'success' );
wc_add_notice( 'This is a Error notice', 'error' );
wc_add_notice( 'This is a \'Notice\' notice\'', 'notice' );

You can add this piece of code to functions.php of your child theme
add_shortcode('woocommerce_notices', function($attrs) {
if (wc_notice_count() > 0) {
?>
<div class="woocommerce-notices-shortcode woocommerce">
<?php wc_print_notices(); ?>
</div>
<?php
}
});
And then use it as a shortcode [woocommerce_notices] in any desired post or page, or use <?php echo do_shortcode('[name_of_shortcode]'); ?> in a desired PHP template.
Tested and works. Good luck.

A bit late to the party but here is a solution.
Try to use the following piece of code (taken from the original file here: wp-content/plugins/woocommerce/templates/single-product.php)
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
Hope it helps

you missed to add this line of code:
<?php wc_print_notices(); ?>

Add this to your functions.php file inside your child theme (or main theme if there is no child).
add_shortcode('woocommerce_notices', function($attrs) {
if (function_exists('wc_notice_count') && wc_notice_count() > 0) {
?>
<div class="woocommerce-notices-shortcode">
<?php wc_print_notices(); ?>
</div>
<script>
(function($) {
$(window).on("load", function(){
// You can execute some js here
});
})(jQuery);
</script>
<?php
// You can execute some php here
wc_clear_notices();
}
});
Then add a shortcode to your page as:
[woocommerce_notices]
The notices and the code will get executed wherever you paste the shortcode.

Related

Show sign-up link for non-logged in users instead Buddypress Activity Loop

How to show login link for non-logged in users instead Buddypress Activity Loop?
Is there an easy way to do this in theme functions.php file?
I know how to add notice before loop with: action_activity_loop_start but how to hide the whole loop?
Option 1
You can use something like this:
add_filter( 'bp_get_template_part', static function( $templates, $slug, $name ) {
if ( $slug === 'activity/index' && ! is_user_logged_in() ) {
$templates = [];
}
return $templates;
}, 10, 3 );
This snippet will completely hide the default activity loop, with all filters, forms, etc, rendering an empty page instead of all BuddyPress Activity.
This may not be a desirable way.
Option 2
In the $templates variable you can also define your own path to a custom template (like activity/logged-out.php, so it will look like this:
$templates = [ 'activity/logged-out.php' ];
You will need to create a wp-content/themes/theme-name/buddypress/activity/logged-out.php file with something like this:
<?php do_action( 'bp_before_directory_activity' ); ?>
<div id="buddypress">
<?php do_action( 'bp_before_directory_activity_content' ); ?>
<div id="template-notices" role="alert" aria-atomic="true">
<?php do_action( 'template_notices' ); ?>
</div>
<?php do_action( 'bp_before_directory_activity_list' ); ?>
<div class="activity" aria-live="polite" aria-atomic="true" aria-relevant="all">
CUSTOM TEXT FOR LOGGED OUT USERS.
</div><!-- .activity -->
</div>
You can check activity/index.php file to decide what you need and what you don't, but I think this is a good idea to remove the majority of content and hooks.
Option 3
If you use the BuddyPress Legacy Template Pack (check in BuddyPress settings) you can copy /wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress/activity/index.php file into your theme: wp-content/themes/theme-name/buddypress/activity/index.php and do the modification where needed. Check the line around 235 which has this: <?php bp_get_template_part( 'activity/activity-loop' ); ?>.
For BuddyPress Nouveau Template Pack everything is basically the same, but you will need to deal with wp-content/plugins/buddypress/bp-templates/bp-nouveau/buddypress/activity/index.php file. You will need to wrap its div#activity-stream into is_user_logged_in() check before display the whole div and its content, something like this:
<?php if ( is_user_logged_in() ) : ?>
<div id="activity-stream" class="activity" data-bp-list="activity">
<div id="bp-ajax-loader"><?php bp_nouveau_user_feedback( 'directory-activity-loading' ); ?></div>
</div><!-- .activity -->
<?php endif; ?>
This way you will basically override the default BuddyPress template file with your own.
Please remember about the ajax functionality of the page (filtering etc).

Custom post type archive with date condition

Am using The7 Theme. Since I wanted to create a NEWS section to post latest news (apart from the Blog, which is separate), I used the Custom Post Type UI plugin to create a custom post type named news
I created a new file in my child theme, by copying the contents of the parent theme's archive.php file and named it as archive-news.php, with a minor modification:
<?php
/**
* Archive pages.
*
* #since 1.0.0
*
* #package The7\Templates
*/
defined( 'ABSPATH' ) || exit;
$config = presscore_config();
$config->set( 'template', 'archive' );
$config->set( 'layout', 'masonry' );
$config->set( 'template.layout.type', 'masonry' );
get_header();
?>
<!-- Content -->
<div id="content" class="content" role="main">
<div class="news_years text-center">
<?php for( $i = date('Y'); $i >= 2009; $i-- ) { ?>
<?php echo $i; ?>
<?php } ?>
</div>
<?php
//the_archive_description( '<div class="taxonomy-description">', '</div>' );
if ( have_posts() ) {
the7_archive_loop();
} else {
get_template_part( 'no-results', 'search' );
}
?>
</div><!-- #content -->
<?php do_action( 'presscore_after_content' ); ?>
<?php get_footer(); ?>
So the modification done was that I commented out the description that would be shown at the top of the page, and added year buttons.
After this, when the www.mysite.com/news was accessed, it pretty well loaded all my news items and when clicking an item, it loaded the detailed page as well. And the pagination also works very well, as it inherits the parents theme's template files.
The problem am facing is, I want to make it display year wise items also. Say for example, right now, when accessing the www.mysite.com/news url, it loads up all the News items with pagination. I want to filter it based on a particular year. That's why I included the additional FOR loop in the above code.
Upon searching, I found that the WP Query has date parameters that can be used which will resolve my issue.
But am not sure how to apply this as this archive page is already doing the fetching of posts. And also, how to pass the year in the URL as well. Any pointers would be highly appreciated.
If this is not the right forum to ask this question, please feel free to move this thread to the apt forum. Thank you.

How to remove rel="nofollow" from feed page

In this page every link has rel="nofollow".
can you guys please tell me how to remove the nofollow from that page.
I am using WordPress them.
Hoping to receive some help from your side
Thanks in advance
To remove nofollow from link please add the below code in your current active theme functions.php file.
The rss hook not allow to change content within tag, so create custom template for your feed.
/**
* Deal with the custom RSS templates.
*/
function my_custom_rss() {
if ( 'photos' === get_query_var( 'post_type' ) ) {
get_template_part( 'feed', 'photos' );
} else {
get_template_part( 'feed', 'rss2' );
}
}
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'my_custom_rss', 10, 1 );
For more help see this link : Click here.
You can get the content of custom feed templates from core wordpress folder /wp-includes/feed-rss2.php
I hope it will help you.
Take a look at the following code snippet:
global $post;
?>
<?php if ($post->ID == $pageid): ?>
<script type="text/javascript">
$("a").each(function() {
if ($(this).attr("rel")) {
$(this).removeAttr("rel");
}
});
</script>
<?php endif; ?>

Awaiting Moderation Message not showing up in WordPress 4.9.6

I'm working on my comment-section.
I have a very basic custom Comment_Walker. But it doesn't show the "Awaiting Moderation" string after posting a comment.
Comments are posted successfully to the backend. I have ticked that comments have to be manually approved in the settings in the backend.
A comment shows in the comment section in the backend as "pending". But I won't get the message when posting the comment.
My Walker File (very basic reduced to demonstrate)
<?php
/** Custom Comment Walker Basic */
class Custom_Comment_Walker extends Walker_Comment {
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( $args['style'] === 'div' ) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'has-children col-12 p-0' : 'p-0 col-12 list-unstyled' ); ?>>
<div>
<?php echo get_comment_text(); ?>
<?php if ( '0' == $comment->comment_approved ) : ?>
Awaiting Moderation
<?php endif; ?>
</div>
<?php
}
}
Accessing it via:
<div class="comment-list">
<?php
wp_list_comments( array(
'walker' => new Custom_Comment_Walker()
) );
?>
</div>
I don't quite understand why the message wouldn't show up. Do I oversee something here? Using the latest WordPress 4.9.6 on a localhost. I did deactivate literally every plugin I've had active before posting here. I have also tested this with different accounts and roles.
Apparently the solution was to have the "Save my name, email, and website in this browser for the next time I comment." checkbox selected in order for the message to show up.
So you have to have the GDPR checkbox to be checked in order to have the comment and message appearing before approval.
Sorry for bothering.

Custom Woocommerce Search Result Page should look like shop layout

I am creating custom search feature in a theme. I have created a form on the homepage and with action set to a custom php search page. Form looks like this somewhat. I AM GETTING Class WP_Query not found Error. How to make it work?
The woo-search.php looks like this. I am using this as a template file. forms action attribute links to the page which has this template applied
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
* Template Name: Search Page
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
global $wp_query;
global $wpdb;
if (isset($_POST['submit'])) {
// echo "Submitted <br>";
$product_kinds = $_POST['product-kinds'];
echo $product_kinds;
$the_query = new WP_Query(array('category_name' => '2-wheeler-batteries'));
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}else{
echo "Get Away";
}
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
For demonstration purpose the query inside wp_query is static. I want to display the search result just like the shop page looks like, I mean with product image, then title, then price and add to cart button. With above code I will need to fetch all seperately and code it to look like the shop page. How do I fetch Woocommerce layout in this search results. What changes should I make in the while loop.

Resources