I am designing Woocommerce single product page,
I have designed product page for simple product
now I am designing for variable product for that I have created this function
to arrange "Choose variation" selection and "Add to cart" button
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
remove_action( 'woocommerce_variable_add_to_cart',
'woocommerce_variable_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
remove_action( 'woocommerce_single_variation',
'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_single_variation_add_to_cart_button', 60 );
}
}
}
I used conditional logic because without it my simple product page show nothing.
This code is working fine but I have these actions and their priority
remove_action( 'woocommerce_before_single_product_summary',
'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_single_product_summary',
'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_title', 30 );
remove_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_before_single_product_summary',
'the_content', 40 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_add_to_cart', 60 );
I want "Choose Option" selection before price i.e.
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
Its priority is 50
so i have added priority 45 for
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
But still woocommerce_variable_add_to_cart is showing below
woocommerce_template_single_price
In the above picture if I have 45-50-60 then 60 and 50 will come in single line, and 45 above 50, that is my requirement.
I tried you code but it broke the structure of the page for me here.
Then ultimately it lead me to research through the hooks and tried some permutation and combination which resulted in this code.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_template_single_price', 19 );
Hope that works for you. Have not added any conditional logic here to restrict it from getting applied on other types of products(I think that you can handle very well).
Related
as the title states, I'm trying to remove the buy button for virtual products. I am using the Astra theme in WordPress alongside Woocommerce, and Elementor.
Now my issue is that with my current implementation, it does remove the button from the individual product page. However it still displays the button on the categories page, which is a pain. I have tried setting the price to nothing, and while that does work. It doesn't help as I still want the products to have their prices listed.
I have added the following code to the functions.php section of my duplicated theme file;
function buy_filter()
{
if ( ! is_product() ) return;
$product = get_product();
if ($product->is_virtual('yes'))
{
//$product->is_purchasable('false')
//remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
//remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30);
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30);
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30);
}
}
add_action ('wp', 'buy_filter');
Any help would be greatly appreciated, and I'm happy to give any more details I can to aid in the matter.
Thank you kindly!
You need to get the specific product by it's ID, I have modified a few part of your code and removed few of the hooks you used. Please add those if required.
function woocommerce_remove_cart()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))
{
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}//end of function
add_action('wp_head','woocommerce_remove_cart');
Hope it works.
add_filter( 'woocommerce_is_purchasable', 'woocommerce_isproduct_purchasable', 10, 2 );
function woocommerce_isproduct_purchasable( $purchasable, $product ) {
return ($product->is_virtual( 'yes' ) ) ? false : $purchasable;
}
This is the hook you are looking for
This is how it all looks
I want to add a new "add_to_cart" button template for the outer product card without affecting the inner one. but when I add, the old one remains. What's wrong with my code?
it is my code in functions.php
function loop_custom_variable_products() {
global $product;
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 30 ); // Remove price
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30); // remove add to cart
remove_action( 'woocommerce_after_shop_loop_item', 'loop_variations_custom_buttons_and_prices', 30 ); // Add prices with custom buttons
}
add_action( 'init', 'loop_custom_variable_products' );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_variable_add_to_cart1', 30 );
//add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_variable_add_to_cart1', 30);
function woocommerce_variable_add_to_cart1(){
//... some code for new add to card style
}
I'm searching for a method to make this:
From Monday - Sunday from 5pm to 2pm the add to cart is enabled, before or after this time, the add to cart button needs to be disabled.
On Tuesday the complete day, the Shop is closed, so the add to cart is disabled.
I found this snippet to disable the add to cart but without the time and day specification:
/**
* #snippet WooCommerce Holiday/Pause Mode
* #how-to Watch tutorial # https://businessbloomer.com/?p=19055
* #sourcecode https://businessbloomer.com/?p=20862
* #author Rodolfo Melogli
* #testedwith WooCommerce 2.6.4
*/
// Trigger Holiday Mode
add_action ('init', 'bbloomer_woocommerce_holiday_mode');
// Disable Cart, Checkout, Add Cart
function bbloomer_woocommerce_holiday_mode() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );
add_action( 'woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5 );
add_action( 'woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5 );
}
// Show Holiday Notice
function bbloomer_wc_shop_disabled() {
wc_print_notice( 'Our Online Shop is Closed Today :)', 'error');
}
My English is not good, but I hope you can help me.
Here's something to get you started, this piece of code will check if today is Tuesday and if it is then the add to cart functionality will be disabled.
function bbloomer_woocommerce_holiday_mode() {
$disable_cart = my_wc_disable_add_to_cart();
if( $disable_cart ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );
add_action( 'woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5 );
add_action( 'woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5 );
}
}
function my_wc_disable_add_to_cart() {
// Tuesday is day number 2 of the week
$day_of_week = date("w", current_time());
if( $day_of_week === 2 )
return true;
return false;
}
It will be a similar process adding the other time criteria for enabling/disabling the cart.
i'm trying to write my own snippet and this is what I achieved so far:
add_action( 'wp_head', 'disable_checkout_sunday' );
function disable_checkout_sunday(){
if (date(D) == "Sun"){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
remove__action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_login_form');
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form');
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review');
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment');
}
}
I have two templates, one for Panorama & Multi-Panel photographs, the other for Normal ones.
I'm trying to code a conditional statement that checks the product tag in order to select the relevant template to be displayed.
Panorama photos have the product tag 'panorama' or 'multi-panel'.
If the 'panorama' or 'multi-panel' tag is true >> perform some remove_action and add_action functionality on the Panorama product page...
...otherwise do nothing to the Normal product page...
This is what I have been dappling with to no affect!!
function robhemphill_panorama_layout () {
if ( has_tag( array( 'panorama', 'multi-panel'))) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
} else {
// Do nothing
}
}
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout' );
Any thoughts or ideas would be great, can't find anything online!
As you are targeting Woocommerce product tags (but not WordPress tags) try this instead:
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout', 1 );
function robhemphill_panorama_layout () {
global $product;
if ( has_term( array( 'panorama', 'multi-panel' ), 'product_tag', $product->get_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
}
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.
Sorry I am somewhat of a novice at php but am wondering if someone might be able to point me in the right direction. I have a function created that works perfect. It is used in woocommerce and for a certain category only, removes the 'add to cart' button and replaces with a link to another page. There is one product in this category that I need to ignore the function for. The working code is:
function fishing_buttons(){
// die early if we aren't on a product
if ( ! is_product() ) return;
$product = get_product();
if ( has_term( 'fishing', 'product_cat' ) ){
// removing the purchase buttons
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
// adding our own custom text
add_action( 'woocommerce_after_shop_loop_item', 'fishing_priceguide' );
add_action( 'woocommerce_single_product_summary', 'fishing_priceguide', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_grouped_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_variable_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_external_add_to_cart', 'fishing_priceguide', 30 );} // fishing_buttons
add_action( 'wp', 'fishing_buttons' );
/**
* Our custom button
*/
function fishing_priceguide(){
echo do_shortcode( '[pl_button type="fish" link="http://localhost:8888/fish/fishing-price-guide/"]View our price guide[/pl_button]' );
} // fishing_priceguide
The product ID is 1268 that I want to ignore (i.e. keep the add to cart button). Is it possible to include an 'and' condition in the if statement? I have tried
if ( has_term( 'fishing', 'product_cat' ) && product_id != '1268' ){
But have not had any success
product means nothing as you have it. $product is an object and the product ID is stored as the class variable $product->id. Therefore your code should be:
if ( has_term( 'fishing', 'product_cat' )&& $product->id != '1268' )
Also what hook is fishing_buttons attached to? You can probably just use the global $product but don't need to retrieve the product again.
function fishing_buttons(){
global $product;
if ( has_term( 'fishing', 'product_cat' )&& $product->id != '1268' ){
// your stuff
}
}
add_action( 'woocommerce_before_shop_loop_item', 'fishing_buttons' );