I want to display the value from an Advanced Custom Field (ACF) in Woocommerce and I am using this code in this function hooked:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( get_field('size_chart') ) {
echo '<p>size guide</p>';
}
return;
}
But it is not working, it is displaying the custom field value above the href (size guide) and the href is empty like this:
size guide
Your problem is that you can't use echo with ACF the_field('my_field') , because when using the_field('my_field') is just like using echo get_field('my_field'), so you are trying to echo an echo. Instead use get_field('my_field') this way in your code:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( !empty( get_field('size_chart') ) ) { // if your custom field is not empty…
echo '<p>size guide</p>';
}
return;
}
After, I have add empty() function in your condition…
You can also try to return it instead of echo:
return '<p>size guide</p>';
Reference:
ACF the_field
ACF get_field
I used this code and it worked well in local but when i upload function file in server it doesn,t work and it gives server error 500, so I had to remove this code again
Related
I have this code to get the value for custom field myCustomField which works fine:
get_post_meta( get_the_ID(), 'myCustomField', true )
This code gets the value stored in this field.
But now I need a code to dynamically get and echo the Custom field label (not the stored value) which is "My Custom Field".
This code will be fired for this action:
add_action('woocommerce_product_meta_start' , 'add_in_product_page');
If you've created your custom field using ACF plugin, then what you need is the field object. You could call get_field_object function in order to get the object and then find the label returned in the object, like so:
$your_field_name = "your_custome_field_name";
$your_field_object = get_field_object($your_field_name); // You could also pass the field key instead of field name
echo $your_field_object['label'];
echo "<br>";
echo $your_field_object['value'];
echo "<br>";
echo $your_field_object['key'];
echo "<br>";
echo $your_field_object['type'];
echo "<br>";
echo $your_field_object['ID'];
You could also read more about this function on the documentation page:
ACF get_field_object function
UPDATE
Translating the returned label to your local language!
I would usually use these two filter hooks: gettext and ngettext.
You could read more about these on the documentation page:
WordPress gettext filter hook
WordPress ngettext filter hook
So the translation would be something like this:
$awesome_label = $your_field_object['label'];
add_filter('ngettext', 'your_theme_custom_translation', 20, 3);
add_filter( 'gettext', 'your_theme_custom_translation', 20, 3 );
function your_theme_custom_translation( $translated, $text, $domain ) {
if ( ! is_admin() ) {
if ( $awesome_label == $translated ){
$translated = 'etiqueta impresionante'; // This is the place where you could type anything you want in your local language to replace the default english label
}
}
return $translated;
}
I have just purchased the plugin Checkout Field Editor for WooCommerce from Theme High.
I need to create a custom display rule condition for fields.
They provide the filters hook:
apply_filters( 'thwcfe_show_field', $show, $field_name );
I have tried the following code, but it does not work.
Can someone help?
function display( $field_name='test' ) {
$show =true;
return $show;
}
add_filter('thwcfe_show_field', 'display');
The true / false options works but I can't make it specific to the $field_name = 'test'.
You are not using this filter in a correct way… First there are 2 arguments for the hooked function (one is missing).
To restrict the filter to a specific field name, you need an IF statement and between $field_name and the value to be tested, you need to use == or === comparison operators, but not =.
So your code is going to be:
add_filter('thwcfe_show_field', 'display_test_field', 10, 2 );
function display_test_field( $show, $field_name ) {
if ( 'test' === $field_name ) {
$show = true;
}
return $show;
}
where 10 is the hook priority and 2 the number of arguments for this hook.
It should better work now.
Documented WordPress add_filter() function
I have added a custom button to my woocomerce product pages using 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';
}
I would like to build the buttons url dynamically using the
get_option('myplugin_option_name')
I'm hoping this will be possible.
'myplugin_option_name' is a custom set value via a simple plugin that adds the field to the admin options. The plugin works and I can display whatever is set in the backend on the front end using a simple :
<?php echo get_option('myplugin_option_name'); ?>
The question is how do I get the value from myplugin_option_name to be added to the button url?
So for example if 'myplugin_option_name' = buy the button url should be generated as such :
http://sample.com/buy/product_id
Any help in the right direction would be appreciated.
Thank you!
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
$url_part = get_option('myplugin_option_name');
$id = $product->get_id();
$url = home_url("/".$url_part."/".$id);
echo 'Extra Button';
}
I am using Woo Commerce plugin and I want to display extra text for a specific product page.
The product ID seen in my body is:
single single-product postid-2624
So I tried the following code but it didn't work:
<?php
function ip_more_content() {
if ( is_single('2624'); ) {
echo 'show something';
}
else {
echo '';
}
}
add_action( 'woocommerce_product_thumbnails' , 'ip_more_content', 9 );
?>
How can I make WP do something based on a specific product id?
I guess that 'woocommerce_product_thumbnails' is running inside the loop, so you cab grab item id by simply using get_the_ID() function. So, edit your conditional like this:
if ( get_the_ID() == 2624 ) {
And it should work it out.
Hellow there all I tried searching everywhere but I coudn't just find it - may be because its a wiered requirement all I want to do is remove my Comment Text area in wordpress comment - I sucessfully removed URL website and name in the comment field by using following code
<?php
function remove_comment_fields($fields) {
unset($fields['url']);
unset($fields['author']);
unset($fields['email']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
?>
But not able to remove the Text area - Actually you all will thought what will I do removing all this I am using a plugin which allow you to post images in your comment and the only option I want to give to user is post images via comment. please guide me.
Two options, the comment_form_defaults filter:
add_filter( 'comment_form_defaults', 'so16856397_comment_form_defaults', 10, 1 );
function so16856397_comment_form_defaults( $defaults )
{
$defaults['comment_field'] = '';
return $defaults;
}
or the comment_form_field_comment filter:
add_filter( 'comment_form_field_comment', 'so16856397_comment_form_field_comment', 10, 1 );
function so16856397_comment_form_field_comment( $field )
{
return '';
}
Check the comment_form source code.
You can also hide the textarea comment box by taking its id set to display:none from CSS
Just simple, add this code in functions.php file
function disable_comments_everywhere( $open, $post_id ) {
return false;
}
add_filter( 'comments_open', 'disable_comments_everywhere', 10 , 2 );