Page template for all WooCommerce Files - wordpress

I have a page template that is a minimalized page (call it min-page.php) in my theme. I use it to remove the header and footer info and add in some basic links so the customer doesn't get distracted.
I can change this template in the Page Attributes on various WooCommerce pages (checkout, cart, my account) but I can't change it on the product page or the general shopping page that displays all the products.
How can I set these pages to use my page template?

You can change the single product template by using the following code via functions.php
function get_product_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/min-page.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_product_template' );
Check the template path after placing the code.

Related

How to get product data in header.php file wordpress woocommerce

I want to show something of product like: name, price, sku in the header.php file.
Below is my code to get product in header.php
global $product;
var_dump($product);
But var_dump just show me the product name like string(6) "hoodie", not array of product.
So, how can I get the product data in header.php file.
Thank you.
Use the Wordpress get_the_ID() function.
Wordpress - get_the_ID()
Obviously this will only work if you are on the product page, so you can add an extra control with WooCommerce's is_product() function.
WooCommerce - is_product()
Finally, to add custom code in the header you can use the wp_head hook.
Wordpress - wp_head
So the function will look like:
// gets the product data in the header
add_action( 'wp_head', 'get_product_data' );
function get_product_data() {
// only on the product page
if ( ! is_product() ) {
return;
}
// gets the product object
$product = wc_get_product( get_the_ID() );
?>
HTML CODE HERE
<?php
}
Tested and it works. The code goes into your active theme's functions.php file.

woocommerce : Unable to override single-product template

I am trying to override woo commerce single product template but it won't get overridden.
I have done the following steps:
Added file inside my theme directory.
Added support for Woo-commerce in my functions.php file
add_action( 'after_setup_theme', 'wpse319485_add_woocommerce_support' );
function wpse319485_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
Made sure the debug mode is off in config file
define( 'WC_TEMPLATE_DEBUG_MODE', false );
Checked the status of templates over-riden
Added an h1 tag with test text in it.
I even tried deleting everything but the default template is loaded.
What am I missing ?
I'm not sure if you are still facing this problem. I ran into the very same. After a very frustrating day, tempted to hack the plugin files, I ended up adding woocommerce.php to my theme, containing:
if ( is_product() ) {
wc_get_template( 'woocommerce/single-product.php' );
}else{
//For ANY product archive.
//Product taxonomy, product search or /shop landing
wc_get_template( 'woocommerce/archive-product.php' );
}
This is an adaptation of what I found here:
https://wordpress.org/support/topic/archive-productphp-template-overwrite-not-working/
It did not work without adding
woocommerce/

How do I change the css of my page based on woocommerce parent product category?

I want to change the css of my page based on the selected woocommerce parent product category. I want to change the colour scheme of my navbar and footer based on these categories. I have already created the style sheets to achieve this and if I change the css path in developer options on my browser it works.
I have tried editing the taxonomy-product_cat.php file and I am able to successfully navigate to a different page using the following code:
// Get current category slug
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
// Call template conditionally
if($cat_slug == 'sport-bikes') {
wc_get_template( 'archive-land.php' );
} else {
wc_get_template( 'archive-product.php' );
}
The two issues I've encountered here are:
1) This only works for the specific sub-category - If I use the parent category "land" it does not work.
2) I'm unable to edit the css from whatever template I use. In this instance I created the "archive-land.php" template.
I have also tried adding the following to my functions.php file:
// add taxonomy term to body_class
function woo_custom_taxonomy_in_body_class( $classes ){
if( is_singular( 'product' ) )
{
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
}
return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );
Based on the following solution on this link:
Woocommerce custom style for specific product category?
The issues encountered here are:
1) The body class remains as " customize-support"
2) My css files are specific to changing the colour of the headers and footers
Basically I just want to change the colour scheme of my headers and footers based on the parent product category.
Edit:
Partially solved - I've created a separate "archive-land.php" and "archive-marine.php" and added the following code to the top of each file calling the css files I need:
<?php
function c3land_theme_styles(){
wp_enqueue_style( 'hoverland', get_template_directory_uri() . '/css/hoverland.css' );
wp_enqueue_style( 'land', get_template_directory_uri() . '/css/land.css' );
}
add_action( 'wp_enqueue_scripts','c3land_theme_styles' );
?>
This takes care of switching the css but I still need to have them changed on the parent category rather than the sub-categories here is my updated code on taxonomy-product_cat.php file:
// Get current category slug
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
// Call template conditionally
if($cat_slug == 'sport-bikes') {
wc_get_template( 'archive-land.php' );
}
if($cat_slug == 'outboard-motors') {
wc_get_template( 'archive-marine.php' );
}
else {
wc_get_template( 'archive-product.php' );
}
1) Are there any potential issues in my initial proposed solution or is there a more elegant / proper way of achieving this ?
2) How to I call my "archive-land.php or archive-marine.php" by the parent category ?

Adding content just to the landing page or a WordPress site

I want to add an image below the header of my landing page. I've managed to do this with the code
add_action( 'genesis_after_header', 'add_image_after_header' );
function add_image_after_header() {
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['url'];
$banner_img = $upload_url . "/Pelican.jpg";
echo ("<img id='banner_pic' src=$banner_img>");
}
But the image appears on every page of the site and I just want it to appear on the landing page. Also, is there a more generic "after_header" hook I can use instead of the genesis version?
Thanks for your help.
If your landing page is your home page, you can use is_home() to only add the image when the home page is shown.
add_action( 'genesis_after_header', 'add_image_after_header' );
function add_image_after_header() {
if( is_home() ) {
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['url'];
$banner_img = $upload_url . "/Pelican.jpg";
echo "<img id='banner_pic' src=$banner_img>";
}
}
For the hook, I don't think you can find another one more generic than this one (for your needs).
The Visual Hook Guide is great to see where you can hook on genesis.
EDIT
is_home() works great when your home page show your blog posts, but if it's a static page, you should use is_front_page() which will be true for either blog posts or static page.

Woocommerce change product style and display it on a page

Sorry in advance for my approximative english. I would like to change product page style on woocommerce for a specific product category (don't display pictures and some other important layout changes). Already made it and it works, the problem is that I would like to display these products on a single page. I tried using woocommerce integrated shortcode [product_page id=$id], the problem is that the custom style and layout I created on content-single-product.php (in woocommerce folder) is not applied using this shortcode. I tried to copy my script on class-ws-shortcodes.php (woocommerce folder) but it didn't worked. What should I do to display the products on a specific page, with the real style of these products page (the custom one I created), and not the woocommerce shortcode one?
Thanks in advance for your answers
There are two ways to achieve this, it just depends on whether you want to edit single-product.php or content-single-product.php. Both will get you to the same end result.
To do the former, you can use default WordPress functionality and filter the template under given conditions via template_include
add_filter( 'template_include', 'so_29984159_custom_category_template' );
function so_29984159_custom_category_template( $template ){
if ( is_single() && get_post_type() == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'single-product-custom.php' );
}
return $template;
}
Or if you'd rather create a custom content-product.php template part, you can use WooCommerce's wc_get_template_part which works in pretty much the same way.
Technically I think you can use the same conditional logic, but I tweaked it here to point out the variables WooCommerce makes available at the filter.
add_filter( 'wc_get_template_part', 'so_29984159_custom_content_template_part', 10, 3 );
function so_29984159_custom_content_template_part( $template, $slug, $name ){
if ( $slug == 'content' && $name == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'content-product-custom.php' );
}
return $template;
}
In either case the single-product-custom.php or content-product-custom.php would be in your theme's root folder. To put them somewhere else, just change the path in the locate_template() argument.
You can not do coding in wordpress's file i.e. page.php to modify woocommerce pages. Its wrong way. You simply override WooCommerce pages into your theme folder and then modify any pages which you want to do. All templates/pages are located in a woocommerce/templates folder.
Here you can find documentation.
http://docs.woothemes.com/document/template-structure/
edit woocommerce/templates/content-single-product.php for single product view

Resources