I have an issue with WooCommerce. I want to disable the shop page completely, but I still want users to be able to search for products.
This is what I have now - it works, but it also disables the search page, as this uses the shop page.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Hope you can help.
I just modified code in your question & it is working. Please add below code in theme's functions.php file or custom plugin file.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop() && !(is_search())):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Related
I want to include custom Hashtag search page in my Wordpress theme.And I have the following code in functions.php file
function hashtag_template( $template ){
global $wp_query;
$hash = get_query_var('hashtag');
if( !empty($hash) ){
return locate_template('hashtags-search.php');
}
return $template;
}
add_action( 'template_include', 'hashtag_template' );
and I have also added a hashtag-search.php template
but when I am searching with www.mysite.com/?hashtag=string, it does not shows that(hashtag-search.php) search page.Where may be the problem?
I am trying to add the below code to a page.php file but call it from a custom plugin. At the moment, I have modified the theme's page.php but want to move custom code to a standalone plugin. Your help is appreciated.
// Check if the user is actually logged in first & if they have the ability to publish posts
if ( is_user_logged_in() && current_user_can('listee') || current_user_can('administrator') ) { // Execute code if user is logged in
acf_form_head();
wp_deregister_style( 'wp-admin' );
}
There are a number of ways to do it.
1. You could use an action hook.
On page.php:
// Hook where you dsire the php to go
do_action( 'custom_action_hook' );
In functions.php:
// function to hook into the custom action hook
function namespace_run_code() {
// run code here
}
add_action( 'custom_action_hook', 'namespace_run_code' );
2. Use conditional template functions
In header.php:
if ( is_page_template( 'page.php' ) ) {
// run php code if on page.php
}
I have created a new role in WordPress for WooCommerce. The only thing I require this role to see is:
WooCommerce --> Orders Only
Users - All Submenus
WP All Import - All Submenus except for Settings
WP All Export - All Submenus except for Settings
I successfully dwindled down to the list but am having issues isolating the WooCommerce Dashboard sub-menu.
I used this code to get a full list:
if (!function_exists('debug_admin_menus')):
function debug_admin_menus() {
if ( !is_admin())
return;
global $submenu, $menu, $pagenow;
if ( current_user_can('manage_options') ) { // ONLY DO THIS FOR ADMIN
if( $pagenow == 'index.php' ) { // PRINTS ON DASHBOARD
echo '<pre>'; print_r( $menu ); echo '</pre>'; // TOP LEVEL MENUS
echo '<pre>'; print_r( $submenu ); echo '</pre>'; // SUBMENUS
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
endif;
I found that component being: wc-admin where I added:
remove_submenu_page('woocommerce', 'wc-admin');
BUT it isn't removing it. Does anyone know why? I've also tried:
add_action( 'admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if($user_role == "custom_shop_admin") {
$remove_submenu = remove_submenu_page('woocommerce', 'wc-admin');
}
}
Everywhere I've looked up has ('woocommerce', 'wc-admin') as the dashboard. Not sure if I can't remove it, to redirect it to the orders page perhaps for that role only. I know there has been a recent WooCommerce Update so not sure if that has anything to do with it.
Thank you!
I know its been a long time no talk here. I had the same problem. I have figured it out and it may help people out in the future to solve the problem really quick. Here is the solution.
function remove_menus_custom(){
global $current_user;
if(is_admin(){
remove_submenu_page('woocommerce','wc-admin');
}
}
add_action( 'admin_menu', 'remove_menus_custom', 999 );
My client wants the checkout to be "streamlined" to skip the cart page and go straight to checkout one product at a time. I got that covered but I also need to automatically empty the cart if the customer decides not to confirm checkout.
For this I wanted to check whether or not I'm on any other page than cart or checkout and do it there but all the commands I tried (is_shop(), is_front_page(), is_page('Shop'), is_product(), is_home()) always return false so I'm not sure what to do about it. This is how I'm trying to do it (in my themses functions.php):
function reset_cart_front() {
global $woocommerce;
echo "Attempting to empty<br>";
if (is_shop()) {
echo "is right page<br>";
$woocommerce->cart->empty_cart();
} else {
echo "is not right<br>";
}
}
add_action( 'init', 'reset_cart_front' );
what gives?
Nevermind, I figure it out!
function reset_cart_shop_loop() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action( 'woocommerce_before_shop_loop', 'reset_cart_shop_loop' );
Correct me if I'm wrong...but I think you should check if the current page is_checkout(), and empty the cart if it's not:
function reset_cart_front() {
global $woocommerce;
if ( !is_checkout() ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'template_redirect', 'reset_cart_front' );
I also think 'init' is too early to hook (and would recommend trying 'template_redirect').
So, found this snippet that clears the basket and it works well when added to functions.php:
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action('init', 'my_empty_cart');
How can I modify this and make it empty the cart only when certain pages are loaded? I played around with if ( is_page( 'pageID' ) but couldn't get it working properly!
Try this,
global $post;
if($post->ID == 'something'){
add_action('init', 'my_empty_cart');
}
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
for more readings.
check this link it have all the page / function codes in woocommerce
Hope its works..
WordPress conditional didnt worked for me either so i did something that dont relies on WordPress conditionals:
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}
if u want to add to certain pages u can edit the conditional exp: (didnt try it)
if ($_SERVER['REQUEST_URI'] === get_permalink('post_id'))
if the get_permalink doest work use the exact url exp: 'https://www.domain.name/post-75'