Custom Woocommerce Search Result Page should look like shop layout - wordpress

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.

Related

Wordpress: Advanced Custom Fields not showing up on posts in front end after modifying theme configuration

I installed Advanced Custom Fields and created a field group called "Apps", that shows up whenever a post is made under the apps category. IN that group is a "description" field. The field shows up without fail, or doesn't when the conditions aren't met, while making the post. However, after modifying the singular.php file in the twentytwenty theme to show the field on the front end, nothing changes. I am using the the_field function, like so:
contents of singular.php
<?php
/**
* The template for displaying single posts and pages.
*
* #link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* #package WordPress
* #subpackage Twenty_Twenty
* #since Twenty Twenty 1.0
*/
get_header();
?>
<main id="site-content" role="main">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'template-parts/content', get_post_type() );
the_field('Apps_description');
}
}
?>
</main><!-- #site-content -->
<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
<?php get_footer(); ?>
I tried putting the the_field function as individual php outside the while loop as well; same result.
Is there something I'm missing? This appears to be what was laid out in the ACF function reference.
Thanks!

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.

Show woocommerce errors

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.

how can we add another left sidebar to our site

i m working on wordpress with bootstrap theme.i m using a Main left-sidebar on my site.as i put the code into WIDGETS> MainSidebar but now i have a problem that on clicking a link i want to open another page having same as my home page but different sidebar.how can i achieve it??
Here is my Template for bootstrap:Page Template (page.php)
<?php
/** page.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.
*
* #author Konstantin Obenland
* #package The Bootstrap
* #since 1.0.0 - 07.02.2012
*/
get_header(); ?>
<div id="primary" class="span9">
<?php tha_content_before(); ?>
<div id="content" role="main">
<?php tha_content_top();
the_post();
get_template_part( '/partials/content', 'page' );
comments_template();
tha_content_bottom(); ?>
</div><!-- #content -->
<?php tha_content_after(); ?>
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
/* End of file page.php */
/* Location: ./wp-content/themes/the-bootstrap/page.php */
You must have two different sidebars.
Put sidebar code in a custom template name them you like.
see how to Create custom template link
and use wordpress conditional tags like.
if(is_page( 2 )){ // 2 is page id
get_template_part( 'custom', 'sidebar' ); // (custom-sidebar.php)
}
else{
get_sidebar();
}
conditionnal tags
By this way you can have 2 sidebars on different pages.

Hide Empty Tab on Magento Frontend Product Page

We installed Wordpress on our server and the Fishpig extension that integrates Wordpress and Magento. We want to make use of the extension's ability to associate blog posts and products by adding related posts to a new tab on the frontend product page (where the product description and upsell products are). I got all of that to work--there is a new tab on the product pages titled "Related Blog Posts" and when selected, it displays the post titles and excerpts exactly how I wanted it to. The problem is, that tab displays even when there are no related blog posts. How do I hide it when it's empty?
There's probably something simple that I'm missing. Here's how I added the tab:
1) This file app/design/frontend/base/default/layout/wordpress.xml establishes which block the related posts are displayed in on the frontend product page.
In this file, near the bottom, I changed the reference name from product.info.additional to *related_blog_posts*.
2) To the file: app/design/frontend/default/{template}/layout/catalog.xml near line 210, I placed the following code. I put it between similar code portions for the product description and the product upsells.
<action method="addTab" translate="title" module="catalog"><alias>related_blog_posts</alias><title>Related Blog Posts</title><block>catalog/product_list_relatedposts</block><template>catalog/product/list/relatedposts.phtml</template></action>
3) Added a new file: app/code/local/Mage/Catalog/Block/Product/List/relatedposts.php Added the following code to this file:
class Mage_Catalog_Block_Product_List_Relatedposts extends Mage_Core_Block_Template
{
protected $_list;
public function __construct()
{
parent::__construct();
$this->setTemplate('catalog/product/view/additional.phtml');
}
public function getChildHtmlList()
{
if (is_null($this->_list)) {
$this->_list = array();
foreach ($this->getSortedChildren() as $name) {
$block = $this->getLayout()->getBlock($name);
if (!$block) {
Mage::exception(Mage::helper('catalog')->__('Invalid block: %s.', $name));
}
$this->_list[] = $block->toHtml();
}
}
return $this->_list;
}
}
4) Added a new file: app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml and added the following code to this file:
<?php foreach ($this->getChildHtmlList () as $_html): ?>
<div class="collateral-box">
<?php echo $_html ?>
</div>
<?php endforeach; ?>
5) In the file app/design/frontend/base/default/template/wordpress/post/associated/list.phtml I changed the following code:
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</li>
<?php endforeach; ?>
</ul>
To:
<div class="related-posts">
<?php foreach($posts as $post): ?>
<h3><?php echo $this->escapeHtml($post->getPostTitle()) ?></h3>
<?php $post->setExcerptSize($this->getExcerptLength()) ?>
<p class="related-post-excerpt"><?php echo $post->getPostExcerpt() ?></p>
<?php endforeach; ?>
</div>
This last change adds an excerpt to each related post instead of just displaying the title.
6) Cleared caches and recompiled the site.
Summary: The new tab appears on the frontend product page and the related blog posts are appearing within the tab as they should. The tab, however, displays even when there are no related blog posts for that product. I have tried multiple ways of wrapping the code in Relatedposts.phtml in if/count conditions but I can't get anything to work. How do I prevent my new tab from appearing when there is no content?
I would try something like this or like you had mentioned some sort of count, in
app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml
<?php
if($this->getChildHtmlList()): ?>
<?php foreach ($this->getChildHtmlList () as $_html): ?>
<div class="collateral-box">
<?php echo $_html ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
You also need to ensure that this block is not returning any white space either because it will be interpreted as content and create the tab. I also had a custom tab and this was the issue for me. If you are having trouble you should Zend_Debug::dump() the $this->getChildHtmlList() and see what is getting generated.

Resources