How To Move Product Variation Descriptions in WooCommerce - wordpress

I'd like to move my product's variation descriptions in Woocommerce beneath the add to cart button, and I can't find what hook I'm supposed to use. These are the variation's custom descriptions that load on selection in AJAX.
I'm able to hook another custom function beneath the add to cart button. So I think my problem is not knowing the name of the hook and/or if it's a hook versus a filter. I think it's either woocommerce_before_single_variation or woocommerce_before_add_to_cart_button.
Here's several attempts I've tried before with no luck in functions.php:
remove_action( 'woocommerce_after_single_variation','woocommerce_single_product_summary', 20 );
add_action( 'woocommerce_after_single_variation', 'woocommerce_single_product_summary', 9 );
//try #2
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
add_action('woocommerce_after_add_to_cart_button', 'woocommerce_single_variation', 35);
Thank you!

The functionality I was looking for is included in WooCommerce 2.4 as a default but is not done on a hook. It's added by jQuery updating a div instead - which I found in woocommerce/js/assets/frontend/add-to-cart-variation.js. So I moved the div's location instead:
add_action ('woocommerce_after_single_variation', 'move_descriptions', 50);
function move_descriptions() {
?>
<div class="woocommerce-variation-description" style="border: 1px solid transparent; height: auto;"></div>
<?php
}

I think this will do,
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
add_action('woocommerce_after_add_to_cart_form', 'woocommerce_template_single_excerpt');
Only move Short description if product is variable
add_action('wp_head', 'move_short_desc_for_varition');
function move_short_desc_for_varition() {
#
global $post;
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
add_action('woocommerce_after_add_to_cart_form', 'woocommerce_template_single_excerpt');
}
}

The problem is in the priority of the remove hook.
I give you an example (this work for me):
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 15 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
Pay attention and try with differents priorities.

Related

Remove Add to cart only single product page

I would like to remove the add to cart button from the single product page. someone help, please?
here my code, but not working for me
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_to_product_page', 1 );
function remove_add_to_cart_to_product_page () {
if ( get_option ( 'wcs_remove_cart_to_product' ) == 'yes') {
if ( is_product () ) {
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'); } } }
To remove add to cart button from single product page you can use below snippet:
add_action('wp', 'your_function_name' );
function your_function_name(){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
You can also set priority to the action:
add_action('wp', 'your_function_name',99);
function your_function_name(){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
You can use WooCommerce Action and Filter Hook :https://woocommerce.github.io/code-reference/hooks/hooks.html

Display Product Price next to Product Title on Product Page (next to, not under or above)

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.

how to move the WooCommerce Add to Cart button below the product description

I'd like to move the WooCommerce "Add to Basket" / "Add to Cart" button below the product description and extra form fields added by WP Field Factory # this page.
I've tried the following in functions.php without success:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 50 );
and
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 25 );
Use this code in functions.php
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_add_to_cart');
Since the description is part of the Tabs section as usual we can try to move the description tabs around the page. Try the below code in your functions page.
function move_product_tabs_override() {
remove_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
}
add_action('init', 'move_product_tabs_override', 5);
You should not try changing button position directly from summary hook like this, because even if you succeed this way, your button might be placed somewhere out of the form which will break your functionality.
Rather than this, you could just modify template files from templates/single-product/add-to-cart/ folder.

Remove add to cart button from shortcode in woocommerce

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

How to deacivate related products on wordpress?

I am working on wordpress,the plugins are :woocommerce,datafeedr api,and datefeedr product sets,I added some products from datafeedr also in woocommerce,now I want to deactivate the related product temporarily,I don t went to remove them.
remove_action(
'woocommerce_after_single_product_summary',
'woocommerce_output_related_products',
20
);
If that didn't work, remove it and use this in functions.php:
add_filter(
'woocommerce_related_products_args',
'_remove_related_products',
10
);
function _remove_related_products( $args ) {
return array();
}
I hope this one work, if didn't, you change 10 to 20, 30 or ... until it works, I hope.

Resources