I have tried many code examples and none of them have worked.
My site has 2 main categories (New & Used) each have about 10 sub cats with them. I am using the YITH request a quote plugin but I only want to use it on the Used items, Is there a way I can display the Add To Cart button for just the New category?
the url is set up like site.com/used-equipment/sub-category/product-name/
Here is the code I have tried.
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category( array( 'used-equipment' )) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
— Updated —
You need to use has_term() conditional function to make it work. has_term() accept category or subcategory names, slugs or Ids, (a single string or an array of values).
Here is your changed code:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( has_term( 'used-equipment', 'product_cat')) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
}
This code is tested and works. It removes on the shop pages and on archives pages the add-to-cart button on products that belongs to a defined category or subcategory…
This goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related
Im trying to hide the add to cart button in woocommerce for all products except a varibale product i have. I have tried the following which leaves the variable select options (which is what i want) but it hides the add to cart button (which i don't want).
add_filter( 'woocommerce_is_purchasable', '__return_false' );
add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_variable_product', 1, 0 );
function hide_add_to_cart_button_variable_product() {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
}
Is there a way to do this?
All my products are simple products except for this single variable products, so maybe there is a function to hide the cart button for all simples except variables?
add_action('woocommerce_single_product_summary', 'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if ( !empty($product) && $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
I think you can just edit the simple.php template file in the add-to-cart directory so it wouldn't show the button for simple products.
I am trying to prepend a Woocommerce single product page title (in the header) with the category so it looks like
Category
Product Title
This is what I have so far:
function prepend_category_to_title($title, $id) {
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
if( is_product() ) {
$title = $product_cat . $title;
}
return $title;
}
add_filter('the_title', 'prepend_category_to_title', 10, 2);
I can get it to prepend the title fine but using the_title outputs the category name to every single menu item and every single breadcrumb as well.
I see from this: https://wordpress.stackexchange.com/questions/309151/apply-the-title-filter-in-post-page-title-but-not-in-menu-title that this is expected behaviour and I can add further filters to remove these extra outputs. e.g. add_filter( 'pre_wp_nav_menu'...
But this seems horribly inefficient and messy. Additionally, prepending the title this way means that the category text also gets wrapped in the title <h1> tags which I don't want as the category name needs to be above the product title in smaller <h5> tags (and different font).
To make this slightly more complex, due to the theme I am using (Astra), the product title sits in a header outside of Woocommerce's
woocommerce_single_product_summary hook.
How can I prepend the product title with the product category in a more elegant way? TIA!
Yes it is based on hook so it will not create an issue. But if you need to proceed based on WooCommerce way then you need to skip WordPress hook the_title and need to use WooCommerce hook.
woocommerce_single_product_summary hook is associate with title, price, expert, .. on single product page. see below code of line that shows hook and there related function.
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
To go with Woocommerce, you need to remove the actual hook and reinitate the function like below. Hope below code justify your question.You can put this snippet in your theme's functions.php file
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5);
if ( ! function_exists( 'woocommerce_my_single_title' ) ) {
function woocommerce_my_single_title() {
?>
/*
your logic goes here
No need to return data, just echo/print
*/
<?php
}
}
I have the official WooCommerce Product Addon's plugin and I am using it on a variable product. however I would like the price to show above the add to cart button instead of below the variation select list as it doesn't now.
I have looked everywhere and I have moved the price for single products to below the short description but can not work out out to do this for variable products with product add-ons.
Screenshot showing current location and desired location:
Any help is greatly appreciated.
WooCommerce Product Addon's plugin displays already a custom pricing table just before add to cart buttons, where the selected variation price is displayed, with the selected option pricing with the subtotal:
You can't only move the displayed selected variation price alone, as it's driven by javascript on live selection event and grouped with variation availability and variation description (if any).
So what you can do is:
1) To move the variation price with its availability and description before add to cart button using:
add_action( 'woocommerce_before_variations_form', 'reposition_display_for_variable_products', 10 );
function reposition_display_for_variable_products() {
global $product;
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 16 );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But it will be displayed after the custom "Addon's" pricing table:
2) hide the Woocommerce variation price (as it's already displayed by the custom "Addon's" pricing table)
add_filter( 'woocommerce_available_variation', 'hide_variation_selected_price', 10, 3 );
function hide_variation_selected_price( $data, $product, $variation ) {
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
$data['price_html'] = '';
}
}
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
3) Use both together, moving the variation availability and variation description after "Addon's" pricing table and before add to cart button:
jQuery('.price').insertAfter('.sauce')
a front-end solution (just put the right selectors)
To move the Product Add-Ons Totals placement you can add this to your functions.php file:
if( isset($GLOBALS['Product_Addon_Display']) ){
remove_action( 'woocommerce_product_addons_end', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
And if you want to add it back:
if( isset($GLOBALS['Product_Addon_Display']) ){
add_action( 'some_action_hook', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
You have to make sure of two things though:
You have to pass the id (or some id) as parameter to your new hook.
The JS that generates the totals looks for the #product-addons-total inside some .cart element.
In WooCommerce I am trying to remove add to cart from product keeping the stock information.
I tried to do it with a plugin but it also remove the stock information.
Is there any way to remove Add to Cart button keeping stock information in single product pages ?
The following code will remove add to cart form from single product on simple product type but will display the stock information:
// Single products (Simple): remove add to cart button and keep stock info
add_action( 'woocommerce_single_product_summary', 'remove_simple_product_add_to_cart_button', 1 );
function remove_simple_product_add_to_cart_button() {
global $product;
// For simple products type
if( $product->is_type( 'simple' ) && $product->is_purchasable() ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'show_stock_info', 30 );
}
}
// Function that stock info
function show_stock_info() {
global $product;
echo wc_get_stock_html( $product );
}
Code goes in function.php file of your active child theme (active theme). tested and works.
I am trying to run a function specific for one product that has a different ID than the page where is displayed. To this I run a function from my child theme functions.php.
I have tried page_id, post id, is_page but I do not get it to work.
My page-id is 8405, product id is 837.
My latest code try is:
// Remove price variation on single product page
add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
function my_remove_variation_price() {
if ( is_page('8405') ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
}}
}
How can I get the Page ID which is different frm the displayed product ID?
The following code will do the job and it will work specifically for your page ID:
// Remove price variation on single product page
add_action( 'woocommerce_single_product_summary', 'remove_variable_product_price', 1 );
function remove_variable_product_price() {
global $wp, $product;
$page = get_page_by_path($wp->request);
if ( $product->is_type( 'variable' ) && $page->ID == 8405 ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.