Remove WooCommerce noindex meta in myaccount page - php

Here is a solution to remove the meta "noindex" which causes an issue for the myaccount page to be indexed in google, because some people want it to appear for their clients to easy find the login page.
The function match the my-account page and then remove the meta
function remove_wc_page_noindex(){
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( false !== strpos( $url, 'my-account' ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'init', 'remove_wc_page_noindex' );
My question: is there is a way to directly locate the my account page instead of matching part of the url?

You can get more details about the conditional tags here.
/**
* Disable/Enable search engines indexing myaccount pages.
*
*/
function is_wc_page_noindex() {
if ( is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'template_redirect', 'is_wc_page_noindex' );

Since WP 5.7, Woocommerce uses the wp_robots filter. If remove_action( 'wp_head', 'wc_page_noindex' ) did not work for you, then you can try the following:
// Remove WooCommerce noindex meta in cart, checkout and myaccount pages
add_action( 'template_redirect', 'srj_woo_remove_noindex' );
function srj_woo_remove_noindex() {
if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_filter( 'wp_robots', 'wc_page_no_robots', 10 );
}
}

Related

How to include WooCommerce conditional tags in custom plugin

I need to use the conditional tag is_product() from the WooCommerce plugin so my custom plugin only loads in the script if the user is at a product page on the front-end.
Additionally, I don't want the plugin to break if WooCommerce is not installed on the website.
My current code:
if ( !is_admin() && is_product() ) {
function add_script() {
wp_enqueue_script( 'main', plugin_dir_url( __FILE__ ) . '/js/main.js' );
}
}
add_action( 'wp_enqueue_scripts', 'add_script' );
This doesn't work at the moment since it can't find the conditional tag is_product().
Any advice?
"I don't want the plugin to break if Woocommerce is not installed on the website." Your main plugin file should start with:
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
// Check if WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) exit; // Exit if WC not active
Loads the script if the user is at a product page, use:
function add_script() {
// is_admin() - Determines whether the current request is for an administrative interface page.
// is_product() - Returns true on a single product page.
if ( !is_admin() && is_product() ) {
wp_enqueue_script( 'main', plugin_dir_url( __FILE__ ) . '/js/main.js' );
}
}
add_action( 'wp_enqueue_scripts', 'add_script' );

Wordpress/WoOCommerce Changed search-slug: Want to search/show results from specific custom type

Search is set via Relevanssi plugin to only search for the custom type "brands". However, using this code beneath to change the url slug makes it search everything except custom types. How can I alter the code to search specifically in the "brands" custom type?
function change_sok_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/sok/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_sok_url' );
function rewrite_sok_slug() {
add_rewrite_rule(
'sok(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?s=$matches[2]&paged=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_sok_slug' );

Clear Woocommerce Cart on Page Load Even for logged in users

I want to clear the cart page on page load if this page is not a cart or a checkout page Even for logged in users and admins, any page then it clears. This code was working but its not anymore
/**
 * Clears WC Cart on Page Load
 * (Only when not on cart/checkout page)
 */
 
add_action( 'wp_head', 'bryce_clear_cart' );
function bryce_clear_cart() {
if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
return;
}
WC()->cart->empty_cart( true );
}
Updated and enhanced.
Use Woocommerce conditional tags and try template_redirect hook instead (when cart is not empty):
add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
if ( ! ( is_cart() || is_checkout() ) && ! WC()->cart->is_empty() ) {
WC()->cart->empty_cart( true );
}
Code goes on function.php file of your active child theme (or active theme). It should work.
This works fine, as i said in the comment your code doesn't work i don't know why i just edited it like that it worked but i don't know if it now works fine as the condition to work only executed if cart is not empty on all other pages than cart and checkout)
add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
if ( ( ! ( is_cart() || is_checkout() ) ) && ! ( WC()->cart->is_empty() ) ) {
WC()->cart->empty_cart( true );
}
}
I think there is an issue with this ! ( WC()->cart->is_empty() )
I think this function maybe doesn't work with the operator "!", my code executes the same as yours but i don't know why yours no

WooCommerce does not redirect to internal link after purchase

I want to redirect to custom page after purchase on woocommerce code below:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_page_by_title( About )->ID );
exit;
}
}
It redirects to 'order-received' page which does not exist.
You have forgot th little "" around the title and you should need to use get_permalink() function too this way:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_permalink( get_page_by_title( "About" )->ID ) );
exit;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I have tested it and this should work now

If cart is empty, the cart page will redirect to shop page in WooCommerce?

In WooCommerce, I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page. Can anyone have the solution ?
Here is the code I have tried, but it does not work:
function my_empty_cart() {
global $woocommerce;
if (isset( $_GET['empty-cart'] ) ) {
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'product' ) ) );
}
}
add_action( 'init', 'my_empty_cart' );
// old woocommerce : use sizeof( $woocommerce->cart->cart_contents) to check cart content count
// In new woocommerce 2.1+ : WC()->cart->cart_contents_count to check cart content count
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
}
}
init hook will run everytime. use template_redirect
==============Updates=============
In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.
WC()->cart->cart_contents_count
2021 Update
Since WooCommerce version 3 use the following to redirect to shop page when trying to access cart page and cart is already empty:
add_action( 'template_redirect', 'empty_cart_redirect' );
function empty_cart_redirect(){
if( is_cart() && WC()->cart->is_empty() ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Notes - Obsolete and deprecated code:
global $woocommerce; use with $woocommerce->cart is replaced simply by WC()->cart
sizeof($woocommerce->cart->cart_contents) == 0 is replaced by simple WC()->cart->is_empty()
woocommerce_get_page_id() is replaced by wc_get_page_id()
When removing all cart items on cart page, to make the redirection active, some additional jQuery code is required, see: Redirect to shop if cart is emptied on cart page in WooCommerce 3+
Just tested this myself as I needed something similar.
function cart_empty_redirect_to_shop() {
global $woocommerce;
if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
}
}
add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
I tried #Pushpak's solution, but it doesn't work anymore. To check the cart content please use this code:
global $woocommerce;
if ( $woocommerce->cart->cart_contents_count != 0 ) {
// cart has content
} else {
// cart is empty
}
2018 - 25 - Sept
working for me today::
function cart_empty_redirect_to_shop() {
global $woocommerce, $woocommerce_errors;
if ( is_cart() && sizeof($woocommerce->cart->cart_contents) == 0) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
add_action( 'template_redirect', 'cart_empty_redirect_to_shop' );
Simply go to woocommerce folder and navigate to the CART folder.. in there is a cart-empty.php
then locate the following:
<p><a class="button1 btn btn-normal" href="<?php echo get_permalink.....
it would say getpermalink(woocommerce_.....
simply change that to
<p><a class="button1 btn btn-normal" href="<?php echo get_permalink('THE PAGE ID YOU WANT TO REDIRECT TO');
and bobs your uncle.. it will redirect to page with id you specify.
Let me know if you dont understand.

Categories