How to set WooCommerce notices from functions,php? - wordpress

I'm processing some requests in my Wordpress theme's functions.php, and need to set notices there.
But none of the WooCommerce methods are available. How can I access wc_add_notice() from there?
Code from my functions.php file:
function my_function() {
// some code
wc_add_notice('Yay!', 'success');
wp_redirect( '/' );
die();
}
add_action( 'admin_post_nopriv_action', 'my_function' );
add_action( 'admin_post_action', 'my_function' );
NOTE: There is a similar question Display Woocommerce notices on a page, but this question is not a duplicate of that one. The notices will be displayed on standard WooCommerce template files. I'm attempting to set notices from within the functions.php file.

WC Notices will only show on WooCommerce template pages it won't show on your main home page unless it's a WooCommerce page. They are shown when the function wc_print_notices() is called.

Related

Show default page template for product categories in Woocommerce

I'm having a problem with WooCommerce / Wordpress templates. I used to have everything working.
I have this code in my functions.php file:
add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template' );
function wpse138858_woocommerce_category_archive_template( $original_template ) {
if ( is_product_category() ) {
return get_template_directory().'/woocommerce/archive-product.php';
} else {
return $original_template;
}
}
This code chose my custom archive template for product categories and shop the main page. Suddenly, it's not working anymore. It seems like WordPress cannot use custom files from wp-content/themes/mytheme/woocommerce anymore. Single product page and product categories use page.php for some reason. I fixed an issue with single product page by creating file single-product.php on my theme folder. This works okay but I can't do the same with Woocommerce archive pages.
All archive pages uses page.php. They don't have pageId and I print out var_dump(is_product_category()); on some product category page, result is false.
Is there any way to force product categories to use archive-product.php template? How is it possible that category (archive) page uses page.php template?
Add add_theme_support( 'woocommerce' ); in your theme functions.php file. After this all WooCommerce templates will work as expected ( provided that the structure of the templates is correct ).

Removing action added by a plugin in Wordpress

I am using Popup Maker plugin for Wordpress and I am trying to prevent it to load on a particular page using the functions.php file of a child theme.
I've located in the plugin's directory the file popup-maker.php which contains the following line at the end:
add_action( 'plugins_loaded', 'popmake_initialize', 0 );
If I remove / comment this line, the popup will not fire, so I guess this is the action I need to remove. I have read the WP codex and numerous posts but still can't get it to work.
Right now I am stuck at this (function I have added in my child theme's functions.php file):
function remove_popmaker() {
remove_action('plugins_loaded', 'popmake_initialize', 0);
}
add_action( 'init', 'remove_popmaker', 1 );
PS: I am using shopkeeper theme and Woocommerce.
All help appreciated, thanks.
you are adding a action to init which is after plugins_loaded so you cannot remove a action after it has run.
you can try the same action but you will have to do this from a plugin
remove_action( 'plugins_loaded', 'remove_popmaker', 0 );
But i suspect actions added before yours will be run after, this may be unpredictable if not you may have to code a MUplugin (google this).
There is a way I managed to do this:
add_action('wp', 'disableEmailPopup');
function disableEmailPopup () {
global $post;
if ($post->ID === 11625249) return remove_action( 'wp_enqueue_scripts', 'popmake_load_site_scripts');
}
#BMM - Without knowing the use case as to why you need to disable the entire plugin on one page its hard to give you the best answer.
For example if you simply wanted a single popup to not show on one page you can use the conditions panel to add a negative condition using the (!) button. Click it to turn it red and you will check for the opposite of a condition, so (!) Page Selected: Page xyz would prevent it from loading on that page.
Alternatively you can create your own custom conditions allowing you to add that condition to any popup.
Lastly if you wanted to unhook it just from the front end you can simply remove the rendering & script handlers
remove_action( 'wp_footer', 'popmake_render_popups', 1 );
remove_action( 'wp_head', 'popmake_script_loading_enabled' );
And if you want to prevent the queries as well
remove_action( 'wp_enqueue_scripts', 'popmake_preload_popups', 11 );
Hope that helps.

How to make a wordpress theme woocommerce compatible?

How can I make a wordpress theme woocommerce compatible ? I want to make cart page, my account page, product loop page, product single page,checkout page design into my wordpress theme.
We Can make WordPress theme compatible with woocommerce here is how you can do that
There are two ways to resolve this:
1] Using woocommerce_content() -
This solution allows you to create a new template page within your theme that will be used for all WooCommerce taxonomy and post type displays.
To set up this template page, perform the following steps:
Duplicate page.php-
Duplicate your theme’s page.php file, and name it woocommerce.php. This file should be found like this: wp-content/themes/YOURTHEME/woocommerce.php.
Edit your page (woocommerce.php)-
Open up your newly created woocommerce.php in a text editor, or the editor of your choice.
Replace the loop-
In woocommerce.php, replace the Loop with woocommerce_content();
i.e., instead of if(have_posts)… endif; should be replaced by
woocommerce_content()
This will ensure that the WooCommerce templates are picked up for the product and taxonomy pages.
2] Using WooCommerce Hooks-
The hook method is more involved that using woocommerce_content, but is more flexible. This is similar to the method we use when creating our themes. By inserting a few lines in your theme’s functions.php file, First unhook the WooCommerce wrappers;
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
Then hook in your own functions to display the wrappers your theme requires:
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); function my_theme_wrapper_start() {
echo '<section id="main">';} function my_theme_wrapper_end() {
echo '</section>';}
3] Declare WooCommerce support -
Now that you have made the changes, the final thing you have to do, is specify that your theme now supports WooCommerce. You need to add the following in functions.php of your theme.
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
To make it more practical for you this is the video for you, which you
can follow too- How To Make WordPress Theme Compatible With WooCommerce Plugin
You need to install WooC and look at the all the style tags that come accross with it then you can style up the pages and add all of that to your style sheet.
Also you can use hooks but Im not 100% sure how you would check if WooC is active off the top of my head so that hooks in your code only come up when the plugin is active.

stop wordpress revision and autosave to page

Wordpress revision and autosave is keeping reverting my changes to a wordpress page. No matter how many changes I make always reverting to that point.
Is there some solution to stop doing that but not to disable revision to all site.
To Disable Autosave put the code below in your functions.php.
function disableAutoSave(){
wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'disableAutoSave' );
To Disable Post Revisions put the code below in your wp-config.php file.
define( 'WP_POST_REVISIONS', false ); // no revisions
Note: Using the above code will disable the Autosave and/or Post Revisions
throughout the site.
I believe the correct code should be as above code throws notification messages.
add_action( 'admin_init', 'disable_autosave' );
function disable_autosave() {
wp_deregister_script( 'autosave' );
}

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