Need some help here is a snippet that adds additional text to woocommerce product title.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
function woocommerce_template_single_title_custom(){
$additional_text = 'Additional Text';
the_title( '<h3 class="product_title entry-title">', $additional_text.'</h3>' );
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title_custom', 5);
What i need is this code to work only for specific product id's.
Thanks
Just check the product_id before inside the function.
function woocommerce_template_single_title_custom(){
global $product;
if($product->get_id() == 123){
$additional_text = 'Additional Text';
the_title( '<h3 class="product_title entry-title">', $additional_text.'</h3>' );
}
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title_custom', 5);
Related
I have a snippet by the following code:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 35 );
That works fine, to move the whole price block in the single product page (regular price and sale price, together)...
How can I move only the "regular" price?
Just for clarification.
What is happening here woocommerce_template_single_price loads template price.php which outputs $product->get_price_html() which holds both regular and sale price.
Since you want to separate your prices we need to have two different actions for each price type.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'custom_regular_price', 35 );
add_action( 'woocommerce_single_product_summary', 'custom_sale_price', 36 );
function custom_regular_price() {
global $product;
if($product->is_on_sale()):
echo '<p class="regular-price"><del>'.get_woocommerce_currency_symbol().' '.$product->get_regular_price().'</del></p>';
else:
echo '<p class="regular-price">'.get_woocommerce_currency_symbol().' '.$product->get_regular_price().'</p>';
endif;
}
function custom_sale_price() {
global $product;
//Move currency symbol depending on currency
if($product->is_on_sale()):
echo '<p class="sale-price">'.get_woocommerce_currency_symbol().' '.$product->get_sale_price().'</p>';
endif;
}
I am adding an extra button on single product page. I would like to get the button url from the product.
I already have the following code
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
echo 'Extra Button';
}
what will be the editing to get href from the product on single product page?
What you are looking for is the following get_permalink( $product->get_id() );
https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
then you get
function my_extra_button_on_product_page() {
global $product;
// Get product id
$product_id = $product->get_id();
// Get url
$url = get_permalink( $product_id );
echo 'Extra Button';
}
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
After looking at the hooks used, the title has priority 5 and the price has priority 10. So, I did an remove_action and add_action and changed it to 6. That did not work.
What I need is this:
Product title: currency symbol price
Example:
Puzzle for kids: $29
The code I tried:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 6 );
I have tried this code as well now without any luck:
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title', 5);
add_action('woocommerce_single_product_summary', 'woocommerce_new_single_title', 5);
if ( ! function_exists( 'woocommerce_new_single_title' ) ) {
function woocommerce_new_single_title() {
$product = wc_get_product( $post_id );
$product->get_regular_price();
$product->get_sale_price();
$product->get_price();
$pprice = $product->get_price();
?>
<h1 itemprop="name" class="product_title entry-title"><span><?php the_title(); ?>: <?php $product->get_price(); ?></span></h1>
<?php
}
}
Any ideas where I am going wrong and how to fix this?
To change an action, simply add your custom function to this action.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
function show_title_with_price()
{
global $product;
$title = $product->get_title();
$price = $product->get_regular_price();
$symbol = get_woocommerce_currency_symbol();
//You may change <p> tag or add any inline CSS here.
echo "<p>$title: $symbol $price</p>";
}
add_action( 'woocommerce_single_product_summary', 'show_title_with_price', 5 );
This code removes the default title and default price sections (actions) from single product page and adds the custom function named "show_title_with_price()" to woocommerce_single_product_summary action with parameter '5' where the actual title is shown in product page.
Note: Code shows only regular price. I believe you already know how to show sale price if exists.
More about single product page actions: WooCommerce Single Product Page Default Actions
Woocommerce action hooks and overriding templates:Stackoverflow Reference
I tested the code, works fine. Have a good day.
I'm trying for few hours now to disable the images from my woocommerce shop but couldn't find any solution yet, here's a screenshot of how the shop page (not product page) is looking now and what I want to remove:
https://imgur.com/XEELei1
I've already tried adding:
// Remove product images from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
// Remove sale badges from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
In functions.php, but that image is still there.
Thank you.
Try below code if it is shop page
function before_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '<div class="no-image">';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_product', 9 );
function after_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_product', 9 );
This solved the problem:
.products .box-image {display:none;}
.products .product-small .box-text {display:flex!important; }
.products .product-small .price-wrapper {margin:20px;}
I am implementing a shop that only allows logged in users to view the add to cart buttons.
I have successfully hidden most of them with the following code:
function thread_remove_loop_button(){
if(!is_user_logged_in() ){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
add_action('init','thread_remove_loop_button');
However, there are still some showing in the New Products & Best seller widgets. Looking at the code, I can see these are calling the shortcode
do_shortcode('[add_to_cart id="'.$product->id.'"]');
What is the best way to amend these so the add to cart button only shows for logged in users. Obviously in the template I can do something along the lines of
if(is_user_logged_in())
echo do_shortcode('[add_to_cart id="'.$product->id.'"]');
}
but it seems like there should be a better way? Along the lines of a hook or something?
add_action('init', 'bbloomer_hide_price_add_cart_not_logged_in');
function bbloomer_hide_price_add_cart_not_logged_in() {
if ( !is_user_logged_in() ) {
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_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'bbloomer_print_login_to_see', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
function bbloomer_print_login_to_see() {
echo '' . __('Login to see prices', 'theme_name') . '';
}
put this in your plugins/woocommerce/woocommerce.php
it will hide the price and add to cart button and print a statement login to see price
The add to cart template function is pluggable, meaning that if you define a function in your theme with the same name it will override that of WooCommerce.
function woocommerce_template_loop_add_to_cart( $args = array() ) {
if(is_user_logged_in()){
wc_get_template( 'loop/add-to-cart.php' , $args );
}
}
You need to use hook which not affect other code.
add_action('init', 'hide_add_cart_not_logged_in');
function hide_add_cart_not_logged_in() {
if (!is_user_logged_in()) {
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 );
}
}
that only allows logged in users to view the add to cart button.
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html