Using ACF true/false field on WooCommerce single product page - php

I have a WooCommerce site and in the single product page I have a custom checkbox to agree to terms before the add to cart button, but I am trying to add a true/false field in the dashboard so that this checkbox can be moved to a different position on the page.
My functions look like this:
add_action( 'acf/init', "acf_move_checkbox", 10 );
function acf_move_checkbox() {
$move_cart = get_field('move_cart');
if ($move_cart) {
add_action( 'woocommerce_after_single_product', "acf_product_terms", 10 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', "acf_product_terms", 10 );
}
// More code
}
And nothing is happening, am I along the right lines with this or way off?

While acf/init is similar to the WordPress init action, I wouldn't use it.
Init hooks are performed constantly.. since you want to apply an action on the single product page it's best to use a hook that only applies to that page, and will only run on those kinds of pages.
For example you can use the woocommerce_single_product_summary hook. It might also be useful to test fields by hard coding before retrieving them.
So you get:
function action_woocommerce_single_product_summary() {
// Get field
//$move_cart = get_field( 'move_cart' );
// Set true OR false
$move_cart = false;
// When true
if ( $move_cart ) {
add_action( 'woocommerce_after_single_product', 'my_callback_function', 9 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', 'my_callback_function', 9 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
function my_callback_function() {
echo '<p style="color: red; font-size: 20px;">Hello World!</p>';
}
If the above step works, you can replace the hard coded field with the desired code/field

Related

How to target a certain product in woocomerce if it is a Variation

I'm trying to figure out how to target a specific variable product in woocommerce to show this message underneath the "Add to cart" button. The current code is showing for all variable product type.
`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
This is the snippet I am working on.`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
Global and dynamic solution is create a custom field in product and you can custom text field.
You can create custom field by custom code or you can use ACF plugin to create custom field in the product.
Custom code to create custom field:
https://www.cloudways.com/blog/add-custom-product-fields-woocommerce/
ACF plugin:
https://wordpress.org/plugins/advanced-custom-fields/
Then using below action hook you can make the process dynamic from admin itself using one time code.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
global $product;
$getcustomText = get_post_meta(post_id, 'meta_key', true);
if ( !empty ($getcustomText)) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}
Quick Note:
I know process is little big, but it will never give you trouble some or make again and again coding to add variable product id or other parameter in the theme functions file
Thanks
Found out this can target Variable or simple product type using this way.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
if ( is_single( '8941' )) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}

Woocommerce 5.4.1: How to Add Hook to a specific Post on Archive Page - Shop Page

After a lot of search and nothing found, how can I apply this Hook just for a Single Post on the Archive Page?
The goal is to use different Text for different Post, but in this position of the post. Ex. 10%, 20%, 30%
As you see on the picture, the function is applied for every Post and this is not the goal.
Maybe I must use the Post ID to select and use the function for the specific Post.
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10, 0 );
Result of the script above:
Yes you can use product ID to add text to specific post
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
global $post;
if($post->ID == your_post_id){
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";}
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10 );

Hide WooCommerce Product Meta (SKU, Categories, Tags) For Specific Pages Using Wordpress Custom Fields

You might also be asking: How do I trigger/define a WooCommerce action/function using WordPress Custom Fields?
Using WooCommerce and Wordpress, I've finished making a system whereby I can remove the product meta but only on certain product pages, and I need to define which pages are to have their product meta hidden using wordpress custom fields. The custom field name I want to call: 'hide_product_meta' and the defining values need to be either '1' or '0' for yes or no.
To be hidden:
I first had a go at creating a filter to do it. I've since edited this post to include the suggestion that lakshman rajput commented below.
This code hides the product meta on pages with a pre-created custom field called 'hide_product_meta' which you can add manually. Define a '1' in the custom field to trigger the code, or anything else to turn it off:
/* WooCommerce hide product page meta - hide_product_meta */
function hide_product_page_meta() {
global $post;
$product_id = $post->ID;
$HideProductMetaValue = get_post_meta($product_id,'hide_product_meta',true);
if (strpos($HideProductMetaValue, '1') !== false) {
return remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
remove_action( 'woocommerce_after_single_product_summary', 'action_woocommerce_after_single_product_summary', 10, 2 );
}
}
add_action('woocommerce_single_product_summary','hide_product_page_meta');
Thank you.
With lakshman rajput's help, I managed to get it to work using the following code, but instead of defining the action with 'yes' or 'no', I can define it with '1', like this:
/* WooCommerce hide product page meta - hide_product_meta */
function hide_product_page_meta() {
global $post;
$product_id = $post->ID;
$HideProductMetaValue = get_post_meta($product_id,'hide_product_meta',true);
if (strpos($HideProductMetaValue, '1') !== false) {
return remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
remove_action( 'woocommerce_after_single_product_summary', 'action_woocommerce_after_single_product_summary', 10, 2 );
}
}
add_action('woocommerce_single_product_summary','hide_product_page_meta');

woocommerce_add_to_cart_validation not executed

I'm trying to set validation on "add cart" button to my woocommerce.
I use this code in functions.php, but it is not executed:
function test( $passed ) {
//exit(); //it would broke the code
$passed = false;
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'test', 10, 5 );
I try to set exit() on test() function but nothing. What can i verify?
If you look at the woocommerce source code you can see an example of how filters are added to woocommerce_add_to_cart_validation: https://github.com/woocommerce/woocommerce/blob/d7f768f77919a3f7a059d915894d0e87b2cb3ab1/includes/wc-cart-functions.php#L13-L27
I have personally added woocommerce filters inside of a child theme: https://developer.wordpress.org/themes/advanced-topics/child-themes/ in the file functions.php, wrapped in the <?php ... ?> tag.
You could be running into a priority issue, see this comment for more info on how that could impact your function: https://wordpress.stackexchange.com/a/7998/120919

How to remove woocommerce tab?

the products in our woocommerce shop don't need any of the default tabs so I have managed to disable them being that I only need to have the product description below the product however, while I want to keep the actual description, I believe the tab itself is redundant since there aren't any other tabs.
Basically, I want to remove the tab's & title altogether but keep the content box below it without modifying the woocommerce core php template file. Is there a way to add a filter to my WordPress theme's functions.php?
function woocommerce_default_product_tabs( $tabs = array() ) {
global $product, $post;
// Description tab - shows product content
if ( $post->post_content ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab'
);
}
While CSS is great, if the stylesheet doesn't load correctly, you could end up showing someone tabs without meaning to. It is best to remove the content before loading (server side), by using a filter, as you had mentioned.
See code below as provided from Woothemes for unsetting data tabs.
EDIT Place within the functions.php file inside your theme.
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
EDIT As noted by #BasvanDijk
To remove altogether, you can use the following
add_filter( 'woocommerce_product_tabs', '__return_empty_array', 98 );
If you want to remove tabs from woo-commerce product details page, then add this code in your function.php
Option 1-
Go to functions.php and Add the following code.
(Go to Admin panel > Appearance > Editor > functions.php)
add_filter( 'woocommerce_product_tabs', 'woo_remove_tabs', 98 );
function woo_remove_tabs( $tabs ){
if(is_product()){
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
}
return $tabs;
}
By using this filter we can Remove the tabs From the Woocommerce Product Pages.
Option 2-
Or for an alternative approach just add this to your functions.php
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10);
Option 3-
Hide the tab by adding this to the bottom of woocommerce.css
.woocommerce_tabs .tabs {
display: none;
}
Read more -Woo-commerce: Remove tab from product page
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
function woocommerce_template_product_description() {
woocommerce_get_template( 'single-product/tabs/description.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_description', 40 );
This work for me with the contributions i got here. Other than removing the tab and also to place back the text.
Credits to Swapnali & Mustafa
Here is the working code:
add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
function wcs_woo_remove_reviews_tab($tabs) {
unset($tabs['reviews']);
return $tabs;
}
for some reason the code to add to functions.php file did not work for me, even tho it is in the woo commerce codex.
I was getting lots of comment spam to these products that showed reviews.
in the end I manually removed the review tab from all the products using built in wordpress/woocommerce functionality.
go to the product lising page
click the checkbox to select all products (it will only select the products on this page so you may have to go thru several pages to repeat)
from the bulk actions drop down, select edit
click apply
you will now see all the bulk edit actions you can do. Choose the "comments" drop down, and select "Do not allow"
click update
make sure to delete any cache if using a caching plugin
Excuse me but the question is not only removing tabs but also keeping the product description. If you ever tried the code above you would realize that while removing tabs you are actually removing the product description. And this is not the desired case.
You should somewhere add the following code to add it back. But unfortunately this time you can add the description side by side the picture and making a narrow column. I couldn't find the solution to add it nicely below the picture where the tabs existed before.
The code:
function woocommerce_template_product_description() {
woocommerce_get_template( 'single-product/tabs/description.php' );
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_product_description', 40 );
Using a combination of the answers above, nesting the function within the action so you can use within a conditional:
add_action( 'wp', 'custom_remove_tabs_by_tag' );
function custom_remove_tabs_by_tag() {
if ( is_product() && has_term( 'tag-term', 'product_tag' ) )
{
// Remove All Product Tabs
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
// Add Product Description
add_action( 'woocommerce_after_single_product_summary',
function () { woocommerce_get_template( 'single-product/tabs/description.php' );},
40 );
}

Categories