Remove subtotal and Shipping calculator block in Woocommerce cart totals section - php

In woocommerce, I am using the following to remove the shipping block in cart totals section bloc:
function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
But in that cart totals section bloc there are some other things like the cart Subtotal and Total.
I just need to keep only Total
Any help is welcome.

You seem to keep only the total on cart totals section. For that the only way is overriding woocommerce templates.
There is 2 steps:
You should read Template structure & Overriding templates via a theme to understand how you can override woocommerce templates via your active child theme (or active theme).
So you will have to create in you active theme folder (if it doesn't exist yet) a folder named "woocommerce" and a sub-sub folder "cart"…
You will have to copy from the wp-content/plugins/woocommerce/templates/cart/cart-totals.php to your active theme woocommerce/cart/cart-totals.php (if it not exist yet).
Editing cart-totals.phptemplate and removing code:
Since WooCommerce version 2.3.6 and up: You will have to remove the code from line 32 to 59 (You will keep only fees and taxes if they are any)
If you want to keep only the total amount you will remove the code from line 32 to line 87
Then save.
You will not need your function code anymore
Now your cart totals will be something like this:

add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' );
function adjust_woocommerce_get_order_item_totals( $totals ) {
unset($totals['cart_subtotal'] );
return $totals;
}
Is this what you are looking for? Basically you asked to remove the subtotal.

Related

Disable products in same category if an item is added to the cart - WooCommerce

I've been looking for a solution to disable certain products from the same category if one of the items is added to the cart.
For example: I have 1 category with products from A to E.
If I add product A to the cart I want to disable products B and E, so they can't be added to the cart during the same shopping session.
Is there a way to achieve this?
Many thanks.
Yes , you actually have two options ,one is to customize your own plugin and insert it to your website but this actually takes time .The other way is to download a wholesale and b2b plugin , these plugins usually contain features as the one you are looking for , so you will be able to activate the feature that you want .
You can do that by validating the cart whenever an item is added to it.
You can do that by editing your theme (or child theme) functions.php file or by using your custom plugin.
You can use the following hook 'woocommerce_add_to_cart_validation'
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_item_category_in_the_cart', 10, 2 );
function allowed_item_category_in_the_cart( $passed, $product_id) {
$product = wc_get_product($product_id);
// Get product categories
$product_category_ids = $product->get_category_ids();
// Then you iterate through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
$cart_product_id = $cart_item['product_id'];
$cart_product = wc_get_product($cart_product_id);
$cart_product_category_ids = $cart_product->get_category_ids();
if (array_intersect($cart_product_category_ids, $product_category_ids))
return false;
}
return true;
}
PS: I just wrote this code. I didn't have time to test it yet.
Let me know your feedback.

Add a red asterisk at the end of all attributes labels in Woocommerce variable products

How do I add a red asterisk to the end of all attributes labels in Woocommerce? It seems like it should be simple, but I'm having a hard time with figuring it out. Wouldn't it be in the functions.php file?
The required attributes are actually working because the Add to Cart button is grayed out until an option is selected. However, apparently my customers don't realize this because they are telling me my Add to Cart button doesn't work. So, if I could draw their attention to the variations dropdown menu maybe they would realize that an option must be chosen before they can add the item to their cart.
To add a red asterisk at then end of product attribute labels on single variable products, just like in required checkout fields, you can use the following very simple hooked function:
add_filter( 'woocommerce_attribute_label', 'filter_single_variable_product_attribute_label', 10, 3 );
function filter_single_variable_product_attribute_label( $label, $name, $product ) {
if ( is_product() ){
$label .= ' <abbr class="required" title="required" style="color:#FF3333;">*</abbr>';
}
return $label;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Hide Price from WooCommerce Product Add-ons Dropdown if it's equal to zero

I found this post: Remove price from Woocommerce variable product dropdown menu, but it hides ALL variable prices. Instead I would like to hide the variable price if it is $0.00.
Can anyone help me with this?
Edit:
I didn't realize that the variation prices weren't part of WooCommerce. I am using WooCommerce Product Add-ons. I found this snippet that will target a specific product, but I don't know how to convert it to if !$0.00 condition. This post closer identifies my issue:
Hide displayed product prices from Woocommerce Product Add-ons Fields
Based on Hide displayed product prices from Woocommerce Product Add-ons Fields answer code, you can try to use something like:
add_filter( 'woocommerce_product_addons_option_price', 'filter_product_addons_option_price', 10, 4 );
function filter_product_addons_option_price( $price_html, $option, $i, $type ){
if( isset($option['price']) && ! ( $option['price'] > 0 ) ) {
$price_html = '';
}
return $price_html;
}
Code goes in functions.php file of the active child theme (or active theme). It could works.

Hide add to cart button when product visibility is hidden in WooCommerce

Every night I load in a CSV with my suppliers products. They remove and add products in every CSV. If a product is not in the CSV anymore and it was in the CSV before, my plugin will put the product visibility on hidden. This way the link still works, so no 404 errors in search console etc, but the product is not showing in my shop.
However, some customers still land on these links from different domains, i.e. google. They land on the "invisible product" and they have the possibility to click on the "in cart" button while the product is not available anymore.
Therefor my question: How can I (in functions.php?) make sure that when a product's visibility is hidden, the cart button is removed (a simple display: none; will do).
I use WP Import for importing the CSV and the way products are put on visibility:hidden is like this:
function my_is_post_to_delete($is_post_to_delete, $post_id, $import) {
// Get an instance of the product variation from a defined ID
$my_product = wc_get_product($post_id);
// Change the product visibility
$my_product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$my_product->save();
return false;
}
So I need something like this:
If product_visibility is 'hidden' then remove add to cart button.
You can simply use woocommerce_is_purchasable dedicated filter hook when the product catalog visibility is "hidden", this way:
add_filter('woocommerce_is_purchasable', 'filter_product_is_purchasable', 10, 2 );
function filter_product_is_purchasable( $purchasable, $product ) {
if( 'hidden' === $product->get_catalog_visibility() ) {
$purchasable = false;
}
return $purchasable;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: If customer has a previous cart session with the product in it, it will be removed from it.
Update - For external (or affiliate) products try to use the following instead:
add_action( 'woocommerce_single_product_summary', 'remove_product_add_to_cart_button', 4 );
function remove_product_add_to_cart_button(){
global $product;
if( $product->is_type('external') && 'hidden' === $product->get_catalog_visibility() ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Show empty subcategories in WooCommerce 3

I tried this code, it shows root categories, but subcategories without products are still hidden.
function hide_empty_categories ( $hide_empty ) {
$hide_empty = FALSE;
// You can add other logic here too
return $hide_empty;
}
add_filter( 'woocommerce_product_subcategories_hide_empty', 'hide_empty_categories', 10, 1 );
This problem can come from some other customizations you have added yourself, some wrong settings, your main theme customizations or some third party plugin.
Something else is altering your the product categories loop as your code correctly enables to show empty product subcategories in Woocommerce and it's the right hook to be used.
It can be simplified with this simple line of code too:
add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Categories