Disable WooCommerce SKU on Product Page - wordpress

I have a WooCommerce store and I don't want to display the SKU on any single product page. Looking at their code, I found this filter:
/**
* Returns whether or not SKUS are enabled.
* #return bool
*/
function wc_product_sku_enabled() {
return apply_filters( 'wc_product_sku_enabled', true );
}
and I attempted to override it with this line of code I placed in a custom plugin:
apply_filters( 'wc_product_sku_enabled', false );
I also tried placing the apply_filter inside an action function for woocommerce_product_meta_start which fires right before but it still renders the SKU on the product page. Any ideas?

I think you shoul try with this:
add_filter( 'wc_product_sku_enabled', '__return_false' );
That will remove sku from all woo, back and front end. You can always hide it just by CSS if need it on admin.

The easiest way is with CSS:
.sku_wrapper {
display:none;
}
A more robust approach is to recreate the woocommerce template woocommerce/templates/single-product/meta.php in your own theme and simply comment out the line:
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span>
To recreate a woocommerce template in your own theme, see:
http://docs.woothemes.com/document/template-structure/

Hiding the SKU/UGS by using cSS is not an efficient solution because it will be still part of the HTML code.
In order to hide it from the product single page and keep it in the admin page, you have to add this code in the child (or parent if you don’t have the child) functions.php :
// Remove the Product SKU from Product Single Page
add_filter( 'wc_product_sku_enabled', 'woocustomizer_remove_product_sku' );
function woocustomizer_remove_product_sku( $sku ) {
// Remove only if NOT admin and is product single page
if ( ! is_admin() && is_product() ) {
return false;
}
return $sku;
}
Make sure also in the product php page (it can have a different name depending on the theme you use) to have this condition to show the SKU in the product single page:
if (wc_product_sku_enabled() && $product->get_sku()) { // HTML code that shows the SKU in the product single page}

Make Sure to remove it from the frontend only by using this code on function.php usually you can edit the function file on theme editor
add_filter( 'wc_product_sku_enabled', 'my_remove_sku', 10 );
function my_remove_sku( $return, $product ) {
if ( !is_admin() && is_product() ) {
return false;
} else {
return true;
}
}

If you don’t need to use SKUs at all in your shop, you can disable them completely by using this plugin. simply install this plugin. https://wordpress.org/plugins/woocommerce-remove-sku/

Related

Add blocks to custom location on shop page?

On my woocommerce shop page I want to remove the description/blocks from the all-in-one woocommerce product grid block. Then add them back above it separately for layout reasons.
So I've removed the description like this:
remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
Then I thought I could just add it back in with a shortcode block in the archive-product.html template.
add_shortcode('shop_description', 'woocommerce_product_archive_description');
The problem is, this adds it to the very top of document even before the <html> tag, instead of where I put the shortcode block...
I realised the woocommerce_product_archive_description function uses echo and to make it work with a shortcode it needs to instead use return.
So I made a new function for the shortcode and boiled it down to be:
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$allowed_html = wp_kses_allowed_html( 'post' );
$description = wc_format_content( wp_kses( $shop_page->post_content, $allowed_html ) );
if ( $description ) {
return $description;
}

Issue with output of WooCommerce hooks on cart and checkout page

I am trying to add some text below the applied coupon on my cart page, but for some reason, I can only get it to display above the table, as per the screenshot.
I even created a fresh install with the twentytwentyone theme, and no other plugins installed besides woocommerce.
These are the code that I am using:
add_action('woocommerce_cart_totals_before_shipping', 'bb_before_shipping');
function bb_before_shipping() {
echo 'woocommerce_cart_totals_before_shipping';
}
And
add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
echo 'woocommerce_before_cart_totals';
}
}
Any help will be appreciated!
You are using the correct hook for the cart page, but the output is part of a HTML table.
So you get:
// Cart
function action_woocommerce_cart_totals_before_shipping() {
echo '<tr><td>woocommerce_cart_totals_before_shipping</td></tr>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'action_woocommerce_cart_totals_before_shipping' );
// Checout
function action_woocommerce_review_order_before_shipping() {
echo '<tr><td>woocommerce_review_order_before_shipping</td></tr>';
}
add_action( 'woocommerce_review_order_before_shipping', 'action_woocommerce_review_order_before_shipping' );

Remove some features of WooCommerce

I'm modifying a theme for WordPress and i can't find a solution for:
   1. How can I delete the WooCommerce product image inside the gallery? Because this image is added automatically and cannot be deleted.
IMAGES: https://imgur.com/a/qd0INEX
   2. How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Greetings to all who comment, I hope these issues can help more people in the future.
Redirect to Checkout when a Product has been added to the Cart
Second question answer:
How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Step 1.
// Disable AJAX add to cart buttons
First of all, we have to do some small configurations in WooCommerce Settings – Uncheck the “Enable AJAX add to cart buttons on archives” checkbox.
Step 2.
// Change text on add to cart buttons
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'nik_add_to_cart_text_1' );
function nik_add_to_cart_text_1( $add_to_cart_html ) {
return str_replace( 'Add to cart', 'Buy now', $add_to_cart_html );
}
/*
* Change button text on product pages
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nik_add_to_cart_text_2' );
function nik_add_to_cart_text_2( $product ){
return 'Buy now';
}
I decided that str_replace() for this situation is the most simple and easy solution, but if you do not want to use it, you can replace the first part of the code with this one:
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'nik_add_to_cart_text_1', 10, 2 );
function nik_add_to_cart_text_1( $text, $product ){
return $product->is_purchasable() && $product->is_in_stock() ? 'Buy Now' : 'Read more';
}
Step 3.
// Redirect to Checkout Page
add_filter( 'woocommerce_add_to_cart_redirect', 'nik_skip_cart_redirect_checkout' );
function nik_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
Step 4.
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'nik_remove_add_to_cart_message' );
function nik_remove_add_to_cart_message( $message ){
return '';
}

Woocommerce: how to jump directly to product page when category only holds one item

I have a top menu which permits the display of product categories.
In this case, the name of the application I am selling.
When this menu item is clicked, it shows the contents of the category - as it should do.
However, as this category only contains one item, I want to jump straight to the product page, instead of displaying a category page with one item.
Here is a link to the page in question : boutique.zimrahapp.com/categorie-produit/app/
I have not been able to find either a hook or a template where I can adjust the output or do a redirect.
Has this kind of thing already been done ?
The above code didn't work for me. I have a solution that works, but it redirects all single archive results. In my case I wanted to also redirect single tags.
/* Redirect if there is only one product in the category or tag, or anywhere... */
function redirect_to_single_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_safe_redirect($post_url , 302 );
exit;
}
}
add_action('template_redirect', 'redirect_to_single_post');
I had asked this same question here: Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?
WooCommerce redirects a search query with only one result to that result. You can see how they are doing it here.
Modifying their code you get something like this:
function so_35012094_template_redirect() {
global $wp_query;
// Redirect to the product page if we have a single product
if ( is_product_category() && 1 === $wp_query->found_posts ) {
$product = wc_get_product( $wp_query->post );
if ( $product && $product->is_visible() ) {
wp_safe_redirect( get_permalink( $product->id ), 302 );
exit;
}
}
}
add_action( 'template_redirect', 'so_35012094_template_redirect' );
Untested, so watch out for copy/paste fails. Always use WP_DEBUG so you can figure out what went wrong.

Disable Shop page on Woocommerce to protect categories

Im trying to disable "shop" page in Woocommerce. Basically im creating a shop theme to sell prints and image downloads for a photographer.
Because i need to create private galleries i created a custom post type where i use the woocommerce category shortcode to show products and then i password protect the post type.
This is a workaround for password protecting the woocommerce categories (if someone knows a better one please explain).
The problem is that is someone goes to /shop they will all products, including the "protected ones". So i need to disable the shop page and i need to do it programmatically on my theme functions. Any thoughts?
To disable the shop page, copy over the archive-product.php file from the /wp-content/plugins/woocommerce/templates/archive-product.php and put in /wp-content/themes/{Your Theme}/woocommerce/archive-product.php
Open up the file and overwrite everything in file with the following code below:
<?php
global $wp_query;
$wp_query->set_404();
status_header(404);
get_template_part('404');
Save the file, and now your Shop page is gone and replaced with a 404 Page!
Add this to functions:
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' );
Docs: WooCommerce Conditional Functions Documentation
WooCommerce has a filter for the array that it uses to create the Product post type: woocommerce_register_post_type_product.
Rather changing the archive template to force it to redirect, you can completely remove the post type’s archive, but changing the has_archive attribute on the post type on creation.
add_filter('woocommerce_register_post_type_product', function($post_type) {
$post_type['has_archive'] = false;
return $post_type;
});
You should then remove the shop page in the CMS by going to WooCommerce » Settings » Product » Display, and clicking the “x” on the “Shop Page” option.
You might need to flush the permalink cache, which you can do just by clicking the “Update” button in Settings » Permalinks.
*Edit -
Apparently the page setting I suggested below no longer works. If WooCommerce doesn't have a plugin setting to change it, I personally would use a wordpress redirect plugin like Redirection. This way you can automatically redirect them from the undesired shop page to whatever page displays your products. It avoids a 404 issue and keeps everything in tact. It also avoids editing template files which adds complications to non-developers.
Old Answer:
Have you tried Woo settings?
Admin area, left main menu, Woocommerce > Settings
Click the pages tab.
Under Pages setup is "Shop Base Page", on the dropdown, there's a small "x" to right right. Click that to get rid of the page.
If there are links elsewhere that need to be fixed let me know and I'll find the hooks/filters to remedy it.
template_redirect is the last hook before page render so in my use case I ask if the page being viewed is the "shop" page and if it is I redirect to (in my case) a pricing page.
function my__template_redirect(){
if(is_shop()){
wp_redirect(site_url() . '/pricing/', '302');
}
}
add_action('template_redirect', 'my__template_redirect');
The last suggestion didn't work for me with WP 4.6.1 and WooCommerce 2.6.4. Hiding products in the Publish tab works for me.
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
To disable the default shop page and leave the /shop/ slug free for custom pages use this:
function remove_woocommerce_default_shop( $args, $post_type ) {
if (class_exists('WooCommerce')) {
if ( $post_type == "product" ) {
$args['has_archive'] = true;
}
return $args;
}
}
add_filter('register_post_type_args', 'remove_woocommerce_default_shop', 20, 2);
You need to hook a couple (or maybe more) things:
/* hide category from shop pages */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Change, obviusly YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 for your cat or cats to hide from shop pages.
Then, you need to hide them from menues too, right?:
/* hide category from menues */
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if category and on the shop page (change it with is_woocommerce() if want to hide from all woo pages)
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
But i don´t think this solve 100% the thing, since then, what about search results?
Try this
Create new page named "Shop"
Go to "woocommerce" > "Settings" > "Product" > "Display tab"
Select shop page named "Shop" then click save changes
Back to "Pages" then delete "Shop" page (keep the page on trash,
don't delete permanently)
This method works fine for me

Resources