Hi John,
Thanks for getting back to me.
Fortunately, I was able to echo out the post content based on the ACF drop-down selection. I had to make a couple of alterations for this to work though, so it’s not as automated as I was hoping for.
So basically I changed the custom field type from Post Object to Select and just inserted a list of post IDs along with their respective guide names.
I then altered the PHP in functions to this:
//Add a guide
add_action( 'woocommerce_product_thumbnails', 'gotread_guide', 110 );
function gotread_guide() {
echo '<div class="row clear">';
echo '<div class="col-4">';
echo '</div>';
echo '<div class="col-8">';
$guideid = get_field('choose_your_guide', false, false);
$post_id = $guideid;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
echo '</div>';
echo '</div>';
}
A new variable of $guideid
was added which was equal to get_field('choose_your_guide', false, false);
I realised it wasn’t working before because it was echoing the ID as a string rather than a number so adding false to the $format_value
parameter stopped this. Which is pulling out both the title and the post content. Which is great, but you’re right I am having trouble with the images.
I want to echo out the featured image. How would you recommend I go about this using JavaScript?
Update:
I’ve now hooked into when an order has been purchased and set to complete:
woocommerce_order_status_completed
I’ve also managed to set the user_id:
$user = wp_get_current_user();
$user_id = 'user_' . $user->id;
No.
You cannot make permalinks like this and mix “/“, “–” without heavy PHP magic.
/grade-11/english to /grade-11/english-eng3u. Your field value (all of them) would need to have this sign “-“.
You dont have so many language courses ? Even theoretically it they are in hundreds WordPress now manages Terms well. As long you dont use to many Select (dropdown) options Terms on shared hosting.
Do it like this:
– “grade 11” = (you use WooCommerce) Products main Category/Taxonomy/Term.
– “english” = subcategory/Term of “grade11” main category.
– “eng3u” = sub category/Term of “english” subcategory.
Just simple nested categories and Terms. This is very reusable everywhere in WordPress for future website building, not like using fields. And Permalinks are generated naturally, no tweaks needed.
I’m using woocommerce to set up courses.
The courses available are for Grade 11 and Grade 12 students.
English, for example, is part of both categories and looks like this:
/grade-11/english
/grade-12/english-2
I’m trying to figure out a way to deal with this, I was thinking, because I already have a field that gets the course’s code, that I could use acf to modify the url to look like this:
/grade-11/english-eng3u
/grade-12/english-eng4u
I could do this manually in the post’s slug, but I was thinking it would be neat if it were dynamic somehow.
Oh yesssss.
Sorry I didn’t realize that product types were a taxonomy of the WooCommerce products.
Big thx for this.
Gilles
I’ve been looking into a good shortcode plugin and I did see several issues on github and the support forum on WP about that plugin having compatibility issues with several other plugins. I guess it makes WooCommerce completely useless.
This is a field added to WooCommerce using one of their functions woocommerce_wp_text_input()
. This field is not registered as a field in ACF. You cannot use get_field_object()
in ACF to get a field that is associated with another plugin.
<?php
// step 1 add a location rule type
add_filter('acf/location/rule_types', 'acf_wc_product_type_rule_type');
function acf_wc_product_type_rule_type($choices) {
// first add the "Product" Category if it does not exist
// this will be a place to put all custom rules assocaited with woocommerce
// the reason for checking to see if it exists or not first
// is just in case another custom rule is added
if (!isset($choices['Product'])) {
$choices['Product'] = array();
}
// now add the 'Category' rule to it
if (!isset($choices['Product']['product_cat'])) {
// product_cat is the taxonomy name for woocommerce products
$choices['Product']['product_cat_term'] = 'Product Category Term';
}
return $choices;
}
// step 2 skip custom rule operators, not needed
// step 3 add custom rule values
add_filter('acf/location/rule_values/product_cat_term', 'acf_wc_product_type_rule_values');
function acf_wc_product_type_rule_values($choices) {
// basically we need to get an list of all product categories
// and put the into an array for choices
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false
);
$terms = get_terms($args);
foreach ($terms as $term) {
$choices[$term->term_id] = $term->name;
}
return $choices;
}
// step 4, rule match
add_filter('acf/location/rule_match/product_cat_term', 'acf_wc_product_type_rule_match', 10, 3);
function acf_wc_product_type_rule_match($match, $rule, $options) {
if (!isset($_GET['tag_ID'])) {
// tag id is not set
return $match;
}
if ($rule['operator'] == '==') {
$match = ($rule['value'] == $_GET['tag_ID']);
} else {
$match = !($rule['value'] == $_GET['tag_ID']);
}
return $match;
}
?>
Hi,
something like this can help you to put it on PDF invoice:
(those 3 are 3 ACF fields shown on shop_order)
/**
* Adding a custom field to the invoice
*
* Uses template tag [[CUSTOMFIELD]]
*
* custom field name : childname
*
* 1 - Edit template.php and add the tag in the desired place
* 2 - add code to the theme functions.php file
*/
add_filter ( 'pdf_content_additional_content' , 'pdf_additional_content_terms',10 ,2 );
function pdf_additional_content_terms( $content, $order_id ) {
global $woocommerce;
if ( isset( $order_id ) ) :
$content = str_replace( '[[RECHUNGSNUMMER]]', get_post_meta( $order_id, 'rechnungsnummer', TRUE ) , $content );
endif;
return $content;
}
add_filter ( 'pdf_content_additional_content' , 'pdf_additional_content_terms_2',10 ,2 );
function pdf_additional_content_terms_2( $content, $order_id ) {
global $woocommerce;
if ( isset( $order_id ) ) :
$content = str_replace( '[[RECHNUNGSDATUM]]', get_post_meta( $order_id, 'rechnungsdatum', TRUE ) , $content );
endif;
return $content;
}
add_filter ( 'pdf_content_additional_content' , 'pdf_additional_content_terms_3',10 ,2 );
function pdf_additional_content_terms_3( $content, $order_id ) {
global $woocommerce;
if ( isset( $order_id ) ) :
$content = str_replace( '[[RECHNUNGSDATUMYEAR]]', get_post_meta( $order_id, 'jahre', TRUE ) , $content );
endif;
return $content;
}
Okay I will try it.
I’ve got a Woocommerce shop. There the customers can register. There are also four different customer categories: A, B, C and D. If a customers has registered, the admin can choose (with the radio button field ‘kundentyp’) on the profile page which typ the customer will be.
Then there are the normal product categories. In the backend on the category page the admin also can choose (with a radio button field called ‘kundenzugang’) which customer category is allowed to visit this category.
Every product has a field, where the admin can choose if the product should be published or not. So if there’s a customer B and want to visit the category ‘test’ where he hasn’t got an access (because only D is allowed to) and this category includes a product where this “public field” is tiggered, he is able to see this product in the frontend. Otherwise this public field isn’t tiggered he isn’t able to that product.
So I’ve created a function for my functions.php which should go through all posts and check if the user is allowed to see the products. If there are product’s he isn’t able to see, the id of this product is written in an array ($id_arr). After that the array is taken to the ‘post__not_in’ to exclude them from the query.
But my function (see first post) will not work and I don’t know why..
I hope it’s a better explanation. Thanks for your help. 🙂
Hello,
I’m having the same issue. I’m using Woocommerce, so I need the relationship field to be able to filter by product’s sku. I used the hookes mentioned above by @pedrolima, the original hooks are from StackExchange: http://wordpress.stackexchange.com/questions/178574/acf-relationship-field-search-filtering/207853
Those hooks are working great (they can search in meta data as well as post title) but, as @pedrolima said, it causes to loop the first result again and again instead of showing the relevant results.
Before I used those hooks from Stackoverflow, I tried to use the filter @hube2 mentioned “acf-fields-relationship-query”, I added the meta query that I need to $args array but unfortunately it didn’t work as excepted… It showed no results for any search I typed.
Any help will be appreciated!
Here is what I’m talking about below:
Hi @charlesg
For something like that, you can try this tutorial: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/.
That tutorial uses the select field, but you should be able to use the post object field too. Also, please keep in mind that you need to modify WooCommerce archive page instead of the standard WordPress archive page.
I hope this makes sense 🙂
Hi @s7media
WooCommerce has different template structure where you need to add the code. This page should give you more idea about it: https://docs.woocommerce.com/document/template-structure/.
If the code is located inside of the product loop, you should be able to use the get_field()
or the_field()
functions in a standard way. But if it’s located outside of the loop, then you need to pass the product ID to the second parameter of the get_field()
or the_field()
functions like this:
get_field('custom_field_name', 99);
Where “99” is the product ID.
For the product category, kindly check our tutorial here: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/.
If you don’t know how to get the product ID or which file you should modify, kindly get in touch with WooCommerce support instead.
I hope this makes sense 🙂
Hi @harikrishnan-1infogmail-com
There are a lot of places you can add the form. If you are not familiar with WordPress template structure, kindly check this page first: https://developer.wordpress.org/themes/basics/template-hierarchy/.
If you are using WooCommerce, kindly check this page too: https://docs.woocommerce.com/document/template-structure/.
To show the custom field value on the front end, please check our tutorial here: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/.
If you don’t have the time to learn more about WordPress, I suggest you hire a developer to help you out with it.
I hope this makes sense 🙂
Hi @pansotdev
There are some syntax issues in your code.
First, you can’t use “#” for the comment. Kindly use “//” instead.
Second, echo is used only to print text, not the code like in your example.
Third, it’s possible that the function is executed outside of WordPress loop. In this case, you need to pass the product ID to the second get_field() parameter. So your code should be like this:
function woocommerce_product_expire() {
// get the current post in the loop
global $post;
//Call the post expirator acf field
?>
<p>
<?php if( get_field('expires', $post->ID) ): ?>
<p>expires: </p>
<?php the_field('expires', $post->ID); ?>
<?php endif; ?>
</p>
<?php
}
I hope this makes sense 🙂
Here’s how I got this to work. Note the ‘post_id’ and ‘return’ values in acf_form():
add_action( 'woocommerce_save_account_details', 'acf_form_head', 20 );
function my_woocommerce_edit_account_form() {
?>
<fieldset style="margin: 20px 0">
<legend>Company</legend>
<?php
acf_form( array(
'post_id' => 'user_' . get_current_user_id(),
'form' => false,
'fields' => array(
'field_5846fdde9fe34',
'field_5846fdf79fe35',
'field_5846fe349fe36',
),
'return' => false,
) );
?>
</fieldset>
<?php
}
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
In case anybody was wondering, I’ve managed to fix it. Turns out it was because I was trying to echo PHP through PHP. I’ve updated the code to the following and it’s working.
add_action( 'woocommerce_single_product_summary', 'ngt_accordion', 11 );
function ngt_accordion() {
echo '<div id="ngt-accordion">
<ul class="nav">
<li><a href="#">Aviator\'s Notes</a>
<ul>
<li>';
the_field("aviators_notes");
echo '</li>
</ul>
</li>
<li><a href="#">Product Overview</a>
<ul>
<li>';
the_field("product_overview");
echo '</li>
</ul>
</li>
<li><a href="#">Size & Details</a>
<ul>
<li>';
the_field("size_and_details");
echo '</li>
</ul>
</li>
<li><a href="#">Care Instructions</a>
<ul>
<li>';
the_field("care_instructions");
echo '</li>
</ul>
</li>
<li><a href="#">Delivery & Returns</a>
<ul>
<li>';
the_field("delivery_and_returns");
echo '</li>
</ul>
</li>
</ul>
</div>'
;}
Because WooCommerce is the one that handles the checkout page, ACF won’t be able to do anything on that page.
To add ACF input fields, you can use the acf_form() with ‘form’ option set to false
. That way it will be integrated to the checkout form. But you need process this form manually.
On the link I gave you before, it uses woocommerce_checkout_update_order_meta
hook to process the form, so you need to do it here too. Using this hook, you can use the update_field() function to update the field for the order.
I hope this makes sense 🙂
Kindly check this page to learn how to customize the checkout page: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/.
Because this is not related to ACF, kindly get in touch with WooCommerce support for further support.
I hope this makes sense 🙂
The problem i’m facing is:
Scence 1:
+ Not login, basic upload
+ Leave “First name” field of WooCommerce blank
+ Press checkout, the file you choosed will be uploaded.
Scence 2:
+ Not login, basic upload
+ Fill all fields of WooCommerce
+ Press checkout, no file uploaded. You will be redirected to Order received page
===========
So I think that Scence 2 not working because it’s lacking of some function or a properly action/hook.
It would be great if I can debug and see what’s missing but I can’t enable for not-logged-in user.
Thank you
I have added the fields to the checkout page successfully; now I’m making it save data to Order detail, everything works file but for upload fields.
It works perfectly for the logged-in user, but for not the logged-in user, it shows the basic upload file selector, when it POST and save to order detail, I only can see the path of the file is C://fakepath/filename.
Do you know why it’s happening?
You can take a look at demo here: http://demo.catsplugins.com/acf-for-woocommerce/?add-to-cart=13
Hi @mikelweb
You should be able to do it, but you need to add custom code to process the submitted data. You should be able to process it when the order is created by using the update_field() function to update the order custom fields.
If you only use it for WooCommerce, I suggest you use another plugin that is designed for that kind of situation. But if you need it for other things too, I recommend ACF.
I hope this helps 🙂
If you have trouble with the ACF side of things let me know. If you can figure out what filters/hooks to use for WooCommerce we may be able to help with that.
This file i running independently, it’s custom and i want this file to run only when there is a woocommerce order. This file i just for retrieving a value (custom field) according to post id. I call wp-load on my file but with this a can just user wordpress function..not from plugin.
Can i run acf function in my case?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.