Woocommerce product archive displays nothing when called from custom Page - wordpress

I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!

When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.

Related

WooCoommerce after page loads removes my modifications on mini-cart

How do I modify something on the minicart without WooCommerce removing it? When page is still loading I can see my text on minicart, but after a second, when the page complete loads, it gets removed.
This is the code that I use to display the minicart in the header. Everything that comes with this function will reset after a while
<div class="widget_shopping_cart_content">
<?php woocommerce_mini_cart(); ?>
</div>
I am trying to append a few things for each item product but I would also need to do it for subtotal and maybe some other things. If I try to use this filter (which by itself shows single product price and quantity in the cart) then my appended HTML shows for a split of second and then disappears.
add_filter('woocommerce_widget_cart_item_quantity', 'ms_woocommerce_widget_cart_item_quantity', 999, 3);
function ms_woocommerce_widget_cart_item_quantity($html_price, $cart_item, $cart_item_key)
{
return $html_price . ' <div>Something</div>';
}
If I add some HTML code in the template cart/mini-cart.php, same thing happens. It will just reset itself to original state after page loads. Same thing happens if I modify directly that template directly in the WooCommerce plugin folder for testing it out, so I don't know where does it pull original HTML from?
I understand that WooCommerce updates fragments but shouldn't it update minicart with my modifications included? Either from mini-cart.php template or with above filter.

Wordpress template for specific nested page

In Wordpress I have a page with the slug logged-user with a parent page ecadmin, making it accessible under the URL /ecadmin/logged-user/.
Since it has a parent page, I (or any other user) can create a page with the same slug logged-user as long as it isn't nested under the same parent page.
The problem now is that I cannot create a single page template page-logged-user.php in theme's folder, as this template can be potentially applied to any other page named logged user no matter where it belongs hierarchly.
Is there a way to name the template file in such way that is referencing to its parent page(s) as well? E.g. page-ecadmin-logged-user.php
The feature you are hoping for doesn't exist, but there is a quite simple way to accomplish what you want to do:
You can create a custom page template [whatever-you-want].php and place this comment in the header
/*
Template Name: Logged In User
*/
That template will be available in the Page Attributes meta box for any "Page" in Wordpress, but will only be applied to a given page if selected in that meta box.
Note:
All content creators have access to the page attributes meta box by default.
If you are worried about users applying that template inappropriately you could hide the Page Attributes meta box from everyone who isn't an admin:
function remove_post_custom_fields() {
if (!current_user_can( 'create_users' )){
remove_meta_box( 'pageparentdiv' , 'post' , 'normal' );
}
}
add_action( 'admin_menu' , 'remove_post_custom_fields' );
http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
You could try using using page-$id.php which would be unique to that page.

Can Shortcodes be used in a Static WordPress Homepage?

I have a Static page set as the home page of my wordpress website. It has a custom shortcode to show a woocommerce catalog based on my own meta query.
When I add valid products, it does not show up immediately or even after a while. However, if I publish the static page, then it shows up magically, thus leading to my question.
What are my alternatives, if I want to show my shortcode on a static home page?
I think this is my problem and nothing else, because I tried disabling all plugins and clearing the cache, but still it did not show the latest added products.
You can edit the template of your theme adding the do_shortcode() function where you want the shortcode to be displayed, for example:
<?php echo do_shortcode( '[contact-form-7 id="91" title="quote"]' ); ?>

Wordpress get permalink of a first page of a specific page template

I wonder if it is possible in Wordpress to get the permalink
of a first page of a specific page template.
For example, I have a couple of custom page templates.
In my index page I have to anchors that one should load the first page of default (text) page template and the second should load the services template for example.
Is it possible to achieve this in Wordpress?
If yes, what is the solution?
Thank You in advance!
There aren't any WordPress functions to do this (that I know of) but you can write your own query with $wpdb. If I had a template with a filename of "template-faq.php" and wanted to get the permalink of the first page that used this template it would look like this:
<?php
global $wpdb;
/* Get post IDs of all pages using "template-faq.php" */
$faq_pages = $wpdb->get_results("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` ='_wp_page_template' AND `meta_value` = 'template-faq.php' ", ARRAY_A);
/* Get permalink using post ID of first page in the results */
$faq_permalink = get_permalink($faq_pages[0]['post_id']);
?>

wordpress : how to add categories and tags on pages?

I have generated pages using a custom template by creating a php file in my theme directory
something like :
<?php
*
* Template Name: Contact Page
*/
?>
<html ..... </html>
and then adding a new page on the dashboard selecting this new template
How can i now associate tags and categories to each pages ?
Is creating posts instead of pages the only solution?
Even better is to add to functions.php in your theme folder:
function myplugin_settings() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');
}
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );
Tried using the accepted answer but for some reason it only shows the Post types and none of the Pages shows in the category page. E.g. /category/entertainment/
To fix that, I have to do this:
// add tag and category support to pages
function tags_categories_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}
// tag and category hooks
add_action('init', 'tags_categories_support_all');
add_action('pre_get_posts', 'tags_categories_support_query');
Try this:
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats(){
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
Not at all helpful to say 'download plugin' for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages with regular accounts - ie you're not a developer.
First, make sure you have your pages in your menu set up properly.
YOU DO NOT NEED TO MAKE YOUR PAGES 'Categories' or 'Tags'!
This wouldn't give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn't be able to.
Then go to WP Admin > Pages
Select a page to edit and go to the text editor instead of visual editor (far right hand side tab)
Then past the following short code:
[display-posts category="hair,makeup,reviews,beauty" posts_per_page="10" include_date="true" text-decoration: none date_format="F j, Y" order="DESC" include_excerpt="true" wrapper="div" image_size="large"]
<
(The shortcode collects all the posts that you have assigned certain categories in your blog posts i.e. mine was hair and beauty. So obviously change yours to ones that are appropriate. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)
this plugin sorted me out :
http://wordpress.org/extend/plugins/add-tags-and-category-to-page/
with the standard instructions :
Upload the plugin files to the /wp-content/plugins/ directory
Activate the plugin through the 'Plugins' menu in WordPress
Use the setting page of the plugin from Settings > Add Tags And Category For Page.

Resources