woocommerce : Unable to override single-product template - wordpress

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/

Related

Register Stylesheet if a particular plugin is installed

I use a basic custom plugin I have built for various WordPress admin functions.
One of them adds a custom CSS file that hooks into the admin as per the example below:
// Sets A Custom Admin Colour Scheme
function hits_admin_colour_scheme() {
wp_register_style('hits_admin_colour_scheme', plugins_url('/assets/css/admin.css',__FILE__ ));
wp_enqueue_style('hits_admin_colour_scheme');
}
add_action( 'admin_init','hits_admin_colour_scheme');
Is it possible to have an additional stylesheet that is only loaded if a particular plugin is installed? Say WooCommerce or ACF? This way I can reduce the size of the default CSS file and only load what is relevant.
Sorry if this is a newbie question. I appreciate any help.
You can check whether woocommerce(or others by same way) is activated before enqueue the file
function hits_admin_colour_scheme() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
wp_register_style('hits_aditional_colour_scheme', plugins_url('/assets/css/additional.css',__FILE__ ));
wp_enqueue_style('hits_aditional_colour_scheme');
}
}
add_action( 'admin_enqueue_scripts', 'hits_admin_colour_scheme');
and please use admin_enqueue_scripts hook to enqueue admin assets.

Custom post template in a plugin erase theme's one

i work with sportpress on Wordpress. I have a custom plugin that add a lot of function to it.
I created a template for the custom post-type of sportpress. They are in my plugin but i don't find how to make them prioritary on theme. The theme isn't a custom'one, so i don't want to delete or modify anything in it.
I have tried several things but theme's file is everytime superior.
Things tried :
naming the file 'single-....php',
filter : add_filter( 'template_include', 'toornament_template' ),
shortcode in theme file (works but not what i want...),
Thanks a lot...
EDIT :
here is what i tried exaclty, it works with another type of post that has no theme template, but not for the one that has single-post template in the theme...
add_filter( 'template_include', 'player_template', 1 );
function player_template( $template ) {
if ( is_singular( 'sp_player' ) && file_exists( plugin_dir_path(__FILE__) . 'templates/player-tpl.php' ) ){
$template = plugin_dir_path(__FILE__) . 'templates/player-tpl.php';
}
return $template;
};

Use my custom CSS just for my WordPress plugin settings page, how?

I'm developing my first WordPress plugin. I need to use a custom CSS for the settings pages that I created for the plugin but when I enqueue my stylesheet file, it affects the whole WordPress backend and not just my plugin's settings page.
Is possible to solve this problem? How?
When you want to add styles or scripts in WordPress you enqueue them using hooks. For the admin side the hook you are looking for is called admin_enqueue_scripts.
You can add something like
/**
* Register and enqueue a custom stylesheet in the WordPress admin.
*/
function wpdocs_enqueue_custom_admin_style() {
wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'css/woo-solo-api-admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );
You just need to be careful to correctly specify the url of the css script you want to enqueue.
Hope this helps.
Oh and https://developer.wordpress.org page is great for finding out about WordPress functionality, core functions, hooks etc.
Also check out the plugin handbook: https://developer.wordpress.org/plugins/
Loads of useful information can be found there :)
EDIT:
admin_enqueue_scripts has a parameter you can use read more
/**
* Register and enqueue a custom stylesheet in the WordPress admin.
*/
function wpdocs_enqueue_custom_admin_style( $hook ) {
if ( $hook === 'edit.php' ) {
wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'css/woo-solo-api-admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
}
add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );
This will load the script only on the edit post screen.
You can see what hook is loaded on your screen by adding
error_log( print_r( $hook, true ) );
In your wpdocs_enqueue_custom_admin_style function before the condition. Enable the debug log in your wp-config.php and you'll get a name of your custom post screen hook.
Alternatively, you could target the $current_screen to match the CPT's screen. This will load the script only on that page.

Using Timber for WordPress admin screens

I am building a WordPress plugin to show documentation related to the project. There is a good amount of HTML required in the views, with the content coming from markdown files.
I was wondering if it's at all possible to use Timber Plugin for admin screens, to help keep everything clean? I cannot find anything in the docs.
I have a function that renders a view (called by add_menu_page()) which is working fine. If I echo some text or HTML or even echo the contents of a file, I see what I'd expect.
Attempting to use Timber in the same way as I would in a theme, like this:
$context = Timber::get_context();
Timber::render(plugin_dir_url( __DIR__ ) . 'views/docs.twig', $context);
This just gives me an empty page, with no errors thrown at all.
Do I need to initialise Timber in some way first?
This is the first plugin I have made and my PHP isn't brilliant so apologies if this question isn't easy to follow or understand.
According to the Template Locations documentation, by default Timber looks in the views/ directory of the current theme for the template files. You can add other paths this way:
Timber\Timber::$locations = array(
plugin_dir_path( __FILE__ ) . 'views/'
);
Timber will look in that location first, so templates in the plugin will override any in the theme if they share the same name.
Yes! Just figured it out.
So, on your functions.php, do the usual procedure of adding a page on the admin side
prepare the function:
function documents_menu() {
add_menu_page('Docs', 'Docs', 'manage_options', '', 'myplguin_admin_page', 'dashicons-heart', 6);
}
then the hook:
add_action( 'admin_menu', 'documents_menu' );
then the renderer function:
function myplguin_admin_page(){
if ( ! class_exists( 'Timber' ) ) {
// if you want to show some error message, this is the right place
echo "Timber doesn't exist!";
return;`enter code here`
}
here's the key part:
Timber::render( 'docs.html.twig', array(
'args' => $args,
'instance' => $instance,
/* any other arguments */
));
}
then on your [theme]/views, create docs.html.twig and create your twig file!

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