how to remove "added-to-cart notice" from every page except product archive pages? - woocommerce

Note: In looking for an answer to my question I came across this post but it is NOT duplicate: Remove add to cart notice and change "add to cart" button in Woocommerce the answer there gives the option to remove the notice from the entire site. I want to remove it only from the cart page and I don't want to do it with CSS.
I use external links to my site to send people directly to the shopping cart with the item already added to the cart. When doing so, the "added-to-cart notification" shows up on the cart page which I do not want.
I found this code which removes the added-to-cart notification: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); but it removes the notification from all pages of my site which is not what I want.
To be more specific, I want the added-to-cart notification to show on every product archive page and nowhere else.
I tried to add a filter but it doesn't work the way I would expect it to, I tried the following two ways (and tested it with various pages to see if I could make anything work but it seems my general syntax is off because I Can't get it to do anything...
function hide_cart_notes() {
if ( ! is_archive() ) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
add_action( 'woocommerce', 'hide_cart_notes' );
function hide_cart_notes() {
if ( is_archive() ) {
return;
}
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
add_action( 'woocommerce', 'hide_cart_notes' );

when woocommerce hook starts? where it's docs? does it run at all?
these question should be answered before.
i know that WordPress parses query at parse_query hook, so i would try this
add_action('parse_query', function() {
if (!is_archive()) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
});
because is_shop(), is_archive(), is_* need query to be parsed first.

Related

How to add Stripe ApplePay to woocommerce minicart

I was able to hide Apple Pay button from single product page following way
// in functions.php
add_filter( 'wc_stripe_hide_payment_request_on_product_page', '__return_true' );
but now I want to add this button to minicart. Is there any hook or shortcode that allows placing Apple Pay button on minicart?
I am using this plugin https://wordpress.org/plugins/woocommerce-gateway-stripe/
add_action( 'woocommerce_widget_shopping_cart_buttons', 'mini_cart_stripe_button', 20 );
function mini_cart_stripe_button() {
if( wp_is_mobile() ){
//I'd like to add button here
}
}
Not sure if this helps but the snippet below makes the button appear on checkout.
add_filter( 'wc_stripe_show_payment_request_on_checkout', '__return_true' );
Also, here are some of the ApplyPay elements:
#wc-stripe-payment-request-wrapper
#wc-stripe-payment-request-button
#wc-stripe-payment-request-button-separator
Please let me know if you figure this one out. I would love to add this to the mini cart as well! Cheers.

Conditionally Hiding Pricing; Need to Make Visible from Tag/Custom Taxonomy

Hide product pricing & add to cart button unless logged in
Sale items be visible, logged in or not, if visited from some kind of tag, category, or other taxonomy so that nobody sees the sales unless they are directed to them explicitly.
Example: All product prices are hidden, except on URLs with the tag "waffles-sale". This is a simple example but I think that explains what I'm after with 2.
I've accomplished 1. with a plugin, and more recently a short code; this works great:
add_action( 'init', 'bbloomer_hide_price_add_cart_not_logged_in' );
function bbloomer_hide_price_add_cart_not_logged_in() {
if ( ! is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'bbloomer_print_login_to_see', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
function bbloomer_print_login_to_see() {
echo '' . __('Please login for pricing.', 'theme_name') . '';
}
But this leaves me with two, which is making things visible conditionally. I am sorry if this is vague. I've scoured the web for answers but I'm coming up short. I feel like my vocabulary is holding me back; I don't know the words for the hooks or phrases that I'm describing. I would love to stop reinventing the wheel (I'm having to clone products/product categories to make only them visible from temporarily published pages for sales).
Can anyone point me in the right direction? Is controlling product visibility in this way possible? I would greatly appreciate anyone's more experienced two cents or solution to this. Thank you.
You could try this (haven't tested), wrote it on rush, but it should help you in achieving what you need.
You need to define the query string then add/remove actions depending on what you need based on that query string.
Use the get_query_string:
// register query var
add_action('init','register_query_string');
function register_query_string() {
global $wp;
$wp->add_query_var('my_query_string_name');
}
//check if it's in the url
if ( get_query_var('my_query_string_name') ) {
// remove/add actions for the query string
elseif ( !user_not_logged_in()){
//something when not logged in
}
}
in case you need to check for query string and logged in, you can do something like
if ( !get_query_var('my_query_string_name') && ( !user_not_logged_in()){
// insert magic here
}
Code goes into functions.php from your child theme (or a plugin).

WooCommerce Url rewrites

I've got the same problem described in this post:
[WordPress URL rewrite for WooCommerce attributes, except that I need to filter by attribute not only inside a category.
Unfortunately, I cannot post a comment before reaching a higher reputation, so I'm creating a new question.
I defined a manufacturer attribute and if I want to browse all products from a certain manufacturer, I can use a url like www.example.com/shop/?filter_manufacturer=230, where 230 is the attribute ID.
I tried adding a endpoint, like suggested in the post linked above above, but I cannot get the rewrites working; for example, if I try to open www.example.com/shop/manufacturer/manufacturer_name I get a 404 error.
It's not clear to me if I should change anything in the permalink settings in Wordpress and, if yes, how.
I've always flushed the rewrite rules after every edit, BTW.
The missing link between your question regarding WooCommerce attributes and the linked answer is that product attributes are merely taxonomies with a 'pa_' appended to their name.
In your case the taxonomy is called "pa_manufacturer". WooCommerce sets these up by default to have no query var attached.
So in lieu of filtering query_vars we are going to target when WooCommerce registers that particular taxonomy. I've also modified to remove anonymous functions.
In my example I am using "color", so adjust to "manufacturer". I was able to then go to a URL of http://example.com/shop/color/black and see all the black products. Note that this doesn't get you a term archive where /shop/color will list all the colors. That is a different question and a lot more work.
I didn't test the activation part, so if you get 404s after activating you can just delete the whole activation function and simply go to Settings>Permalinks and save the permalinks again.
/**
* Plugin Name: Add an WooCommerce attribute endpoint to the URLs
* Plugin URI: http://stackoverflow.com/q/28460538/383847
* Credit to: http://stackoverflow.com/a/24331768/1287812
*/
function so_28460538_add_rewrite_endpoint(){
add_rewrite_endpoint( 'color', EP_ALL );
}
add_action( 'init', 'so_28460538_add_rewrite_endpoint' );
function so_28460538_attribute_args( $args ){
$args['query_var'] = 'color';
return $args;
}
add_filter( 'woocommerce_taxonomy_args_pa_color', 'so_28460538_attribute_args' );
/**
* Refresh permalinks on plugin activation
* Source: http://wordpress.stackexchange.com/a/108517/12615
*/
function WCM_Setup_Demo_on_activation(){
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
add_rewrite_endpoint( 'color', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'WCM_Setup_Demo_on_activation' );
EDIT
Adding some screenshots of my settings in case it will help determine why you are getting 404s:
Here are my permalinks settings:
and here is the WooCommerce setting for determining the product archive page:
And finally, here is the result of visting:
http://local.wordpress.dev/shop/color/black/
Where shop is the pretty permalink for the product archive page set above. All items have a 'black' color attribute.
I had a similar problem and was able to solve it like this:
function add_model_taxonomy_args($args) {
$args['query_var'] = 'filter_model';
return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
function custom_rewrite_rules() {
add_rewrite_tag('%filter_model%', '([a-zA-Z0-9-]+)');
add_rewrite_rule('^c/phone-cases/(.+?)/?$', 'index.php?product_cat=phone-cases&filter_model=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules', 10, 2);
Sample Url: c/phone-cases/iphone-8-plus/
Resulting Rewrite: index.php?product_cat=phone-cases&filter_model=iphone-8-plus

Woocoomerce remove product count from attributes

On a WordPress installation with Woocommerce I have an issue with wrong number of product count on the frontend. That's why I decided to remove it. I have remove it from sidebars and anywhere with snippets but not on product attributes.
Is there any snipped to fix that?
If you want really remove (not just hide) you need to edit your theme function.php file by adding at the end this code:
add_filter( 'woocommerce_layered_nav_count', '__return_false' );
It's work for WOOCommerce v 3.4.3
Here is a quick snippet to remove products count after categories names using WooCommerce. It’s pretty useful in particular when your main shop page list categories instead of listing products. you can put it in 'functions.php' file in your theme.
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
Adding
small.count
{
display: none !important;
}
to your style sheet should work. It should hide the product count.
i think i allready have that.
See what functions.php file includes
<?php
define('ETHEME_DOMAIN', 'legenda');
require_once( get_template_directory() . '/framework/init.php' );
add_filter( 'woocommerce_subcategory_count_html', 'jk_hide_category_count' );
function jk_hide_category_count() {
// No count
}
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
you can swee live that is not worked at http://www.karadimos.gr/product-category/xalia/
BTW thanx for your reply!!!

What is difference between these Wordpress filter hooks

I'm new to WordPress plugin development. I have a doubt about below 3
filter hooks.
content_edit_pre
content_filtered_edit_pre
excerpt_edit_pre
Please tell me the what the difference between these hooks.
content_edit_pre filter hook
The content_edit_pre filter hook is used to hook into the content of a post just before it is loaded to be edited. For example, if you included the following at the end of your functions file:
function test_of_content_edit_pre( $content, $post_id ) {
return "Insert this before the content about to be edited ".$content;
}
add_filter( 'content_edit_pre', 'test_of_content_edit_pre', 10, 2 );
Then open a post to edit it, you would see that the text has been inserted before the post:
excerpt_edit_pre filter hook
The excerpt_edit_pre filter hook is very similar to content_edit_pre, except it is used to hook into excerpts (instead of posts) just before they are loaded to be edited. For example:
function test_of_excerpt_edit_pre( $content, $post_id ) {
return "Add this to excerpt".$content;
}
add_filter( 'excerpt_edit_pre', 'test_of_excerpt_edit_pre', 11, 2 );
Would result in this being shown in the excerpt:
content_filtered_edit_pre filter hook
This one I am not sure about. I tested it out and it didn't seem to do anything. I will update my answer if I can find more information on this.

Resources