Got it figured out!
I stumbled across your code snippet for drawing a field from another page and all logic told me that should work. So I fumbled around with it for a bit until I got it to work. It wouldn’t pull in the post ID, but I noticed Woocommerce uses $product_id. When I replaced the page id location with $product_id, voila! It worked!
Here’s a sample for anyone else that needs to figure this out.
<?php the_field('your-field-name', $product_id ); ?>
Also, using $product_id worked for drawing in my custom taxonomy too.
The Checkout page also needed some customizing and I found I needed to do one extra step. The snippet of code that creates the $product_id variable is missing on checkout.php. I grabbed it from cart.php and everything worked fine. Below is the code snippet you need to add to checkout to use the $product_id variable.
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
This is a code snippet from the cart.php file. Specifically for the table item named Product. How would I replace the woocommerce product title with an ACF field??? My logic is… if I can get a single field to show anywhere on the cart page, I should be able to figure out the rest on my own.
<?php
if ( ! $_product->is_visible() ) {
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
} else {
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a class="cart-link" href="%s">%s </a>', esc_url( $_product->get_permalink( $cart_item ) ), $_product->get_title() ), $cart_item, $cart_item_key );
}
// Meta data
echo WC()->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
}
?>
I got frustrated with the woocommerce templates. So I overrode them completely for my post pages.
I took my page.php and renamed it woocommerce.
Added a function to my functions.php that groups the woocommerce products with regular posts so that the global wp-query calls them in.
Then created my own templates for page layouts and called in the fields from ACF and woocommerce in the places I want.
The cart though, that is the typical woocommerce template.
I have the child folder in my theme.
Location in woocommerce is….
Plugins > woocommerce > cart > cart.php
I played around with it a bit, but really don’t know what the preface is before using the_field();
The other guy who solved his own problem and didn’t post the solution claimed it was stupid simple. So I’m expecting it’s a simple solution and I’m being a bit dense on this….
Which of the templates from WooCommerce have you copied and edited. Since you know your in the right one it would help to know which one that is.
Found this while I was looking for an answer to another, newer topic and thought I would add the information here. The other topic is located at http://support.advancedcustomfields.com/forums/topic/quick-field-woocommerce/
There is a plugin, but it may not last, that says it does this. The developers comments are
I am not quite sure if I will continue developing it. If you encounter an issue or wish for a certain feature, please do not ask me to fix it for you. I will not, but I would really appreciate when you fix it youself and send me a pull request.
https://github.com/mcguffin/acf-quick-edit-fields
I can’t say if this will do what you want or not.
There is a tutorial here on expanding the quick edit menu http://shibashake.com/wordpress-theme/expand-the-wordpress-quick-edit-menu
Thank you John
That worked!
One comma was missing
$images = implode(',' , $images);
I’ll paste my functions.php here if anyone in future is doing the same:
Transferring product data (featured image, images gallery, price) from ACF to WooCommerce
function acf_set_featured_image( $value, $post_id, $field ){
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
add_filter('acf/update_value/name=my_featured_image', 'acf_set_featured_image', 10, 3);
function acf_set_current_price( $value, $post_id, $field ){
if($value != '')
update_post_meta($post_id, '_price', $value);
return $value;
}
function acf_set_regular_price( $value, $post_id, $field ){
if($value != '')
update_post_meta($post_id, '_regular_price', $value);
return $value;
}
add_filter('acf/update_value/name=my_price', 'acf_set_regular_price', 10, 3);
function my_acf_save_post($post_id) {
$repeater = 'other_images';
$subfield = 'image';
// using get_post_meta because that way we're sure to get the image ID
$count = intval(get_post_meta($post_id, $repeater, true));
$images = array();
for ($i=0; $i<$count; $i++) {
$field = $repeater.'_'.$i.'_'.$subfield;
$images[] = intval(get_post_meta($post_id, $field, true));
}
if (count($images)) {
$images = implode(',', $images); // convert to comma separated list
} else {
$images = '';
}
update_post_meta($post_id, '_product_image_gallery', $images);
}
add_action('acf/save_post', 'my_acf_save_post', 20); // priority of 20 so it runs after ACF
so it appears that woocommerce may be storing a comma separated list of id values? Now that I see the difference in the XML.
You could try
// priority of 20 so it runs after ACF
add_action('acf/save_post', 'my_acf_save_post', 20);
function my_acf_save_post($post_id) {
$repeater = 'other_images';
$subfield = 'image';
// using get_post_meta because that way
// we're sure to get the image ID
$count = intval(get_post_meta($post_id, $repeater, true));
$images = array();
for ($i=0; $i<$count; $i++) {
$field = $repeater.'_'.$i.'_'.$subfield;
$images[] = intval(get_post_meta($post_id, $field, true));
}
if (count($images)) {
// convert to comma separated list
$images = implode(',' $images);
} else {
$images = '';
}
update_post_meta($post_id, '_product_image_gallery', $images);
}
Hi @mbagri
I don’t think there’s a way to manually add the ACF field groups to WP admin.
It can only be added like a standard WP meta box with the options available in the location settings for the field group.
If you’re looking to add custom meta inside WooCommerces custom meta boxes I think you’ll have to create these yourself.
@abombelli You have to buy Admin Columns Pro. Once you have that, in the my account section, you’ll have 2 other plugins that come with Admin Columns Pro, cac-addon-acf (for Bulk Edit / Inline Edit of ACF fields) and a cac-woocommerce for woocommerce. The bulk edit on admin columns pro unfortunately isn’t like the wordpress bulk edit, but more of an inline edit on the columns that allows you to quickly edit a custom meta field so you don’t have to go into a post and do it. So its not REALLY bulk edit, but it makes editing custom fields for many posts faster because you can do it on the post listing screen in admin. See my image here for an example:
Sorry, this got buried in my inbox. The problem I have is that without setting up a woocommerce site to test on and that plugin and everything that goes with it to test and then doing the testing. I’m really not familiar enough with that plugin.
I think that the place you want to modify is this section of code
<?php
// whitespace altered for clarity
foreach ($products as $i => $product) :
$product_class = ( $i % 2 == 0 ? 'odd' : 'even' ) . ' product_' . $product->id;
/*
I think the post ID of the curretn product should be
$product->id
but I can't be sure
you would need to alter this code to add your fields
but I don't know if you'd add a new <td> for each field or
add the fields to this <td>
you would show a field with something like the below
if $product->id is the post ID
the_field('my-field-name', $product->id)
*/
?>
<td class="<?php echo $product_class; ?>"><?php
switch( $field ) {
case 'image':
echo '<div class="image-wrap">' . wp_get_attachment_image( $product->fields[$field], 'yith-woocompare-image' ) . '</div>';
break;
case 'add-to-cart':
$wc_get_template( 'loop/add-to-cart.php' );
break;
default:
echo empty( $product->fields[$field] ) ? ' ' : $product->fields[$field];
break;
}
?>
</td>
<?php
endforeach
?>
I hope that helps, but I think it’s the best I’m going to be able to do.
I’m not that familiar with WooCommerce, but I think that the you need to put the template into a folder named woocommerce
in you theme folder, and not in a subfolder.
wp-content/themes/my-theme/woocommerce/compare.php
Then try to make some changes to it, output some text just to make sure it’s working. If not you’ll need to contact the plugin developer.
Hello John,
I tried to copy the template folder from plugin and past it inside the theme folder woocommerce.
But still nothing showing.
Is there anything else i have to do ?
Thanks
See, Can I customize the compare table?
on this page https://wordpress.org/plugins/yith-woocommerce-compare/faq/
Once you have copied the template to your theme you can modify that template to add the additional fields you want to display by following the documentation for ACF.
I wonder if this could be simplified somehow by the fact that much of the info in that array is going to be the same for every one of the attributes I’m trying to add. I’m really stuck with this – WooCommerce is well developed as a one-size-fits-all system, but the client wants the product entry streamlined for his very specific application, where products will have three and only three attributes. He enters many products and wants to eliminate any extra clicks. In the default new product process, adding attributes requires several clicks that seem unnecessary.
You need to add an acf/save_post filter which is documented here http://www.advancedcustomfields.com/resources/acfsave_post/
The WooCommerce custom field stores a serialized array of image ID values. In the your save post filter you need to loop through your repeater field and add them to an array and then store this array into the WC custom field.
// priority of 20 so it runs after ACF
add_action('acf/save_post', 'my_acf_save_post', 20);
function my_acf_save_post($post_id) {
$repeater = 'other_images';
$subfield = 'image';
// using get_post_meta because that way
// we're sure to get the image ID
$count = intval(get_post_meta($post_id, $repeater, true));
$images = array();
for ($i=0; $i<$count; $i++) {
$field = $repeater.'_'.$i.'_'.$subfield;
$images[] = intval(get_post_meta($post_id, $field, true));
}
update_post_meta($post_id, '_product_image_gallery', $images);
}
Well first of your client is an idiot but fine! 😉
The attributes in WooCommerce is saved as an multidimensional array in the wp_postmeta table and looks like this:
array (
'testattribute' =>
array (
'name' => 'testattribute',
'value' => 'attributevalue',
'position' => '0',
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
),
)
So you would have to try to replicate that using your own custom fields.
I think it might be possible to do using nested repeater fields but you’ll not really end up with an easier way of input and the client wont be able to just hit “save attributes” but would have to update the whole product post each time they alter an attribute.
Really you should tell your client you can attempt to do your own solution but will have to add x hours to the invoice (probably atleast 4-5h) and in the end the solution might not be easier than the default. There’s a reason why WooCommerce have it as it is and they have multiple devs working SOLELY on optimising every aspect of WooCommerce and millions of users liking it as it is.
It all comes down to my first reaction, your client is an idiot 😉
I’ve tried installing the free version of this plugin on a test site that I use for testing WooCommerce and ACF. I’m not seeing an issue. This is probably something specific to the premium version.
So what you really need to see is what is being returned? and why?
You can try
$image = get_field('logo', 'option');
print_r($image);
HI @steele
Thank you for the question.
ACF only allows you to make use of the predefined set of conditionals to choose where the fields will be displayed. If this is not an option you could have a look at the Woocommerce site for the right hook you can edit to make this happen.
Hi @renee
Could you try this instead. I think it’s possible you could still use have_rows etc. for the looping of the repeater this just feels safer. Also I think your main issue was that you were looking if the_sub_field(‘product_pdf’) existed without first being in a loop (and the_* will always try to echo the results which is not desired when fetching a value for a variable).
<?php
add_filter( 'woocommerce_product_tabs', 'downloads_tab' );
function downloads_tab( $tabs ) {
// ensure ACF is available
if ( !function_exists( 'have_rows' ) )
return;
if ( get_field('downloads') ) {
$tabs[] = array(
'title' => 'Downloads',
'priority' => 15,
'callback' => 'show_download_content'
);
}
return $tabs;
}
function show_download_content() {
$downloads = get_field('downloads');
if( $downloads ):
// loop through the rows of data
foreach ( $downloads as $download ) :
// display a sub field value
echo '<p>'. $download['name_pdf'].'<.p>';
endwhile;
endif;
}
<?php echo single_term_title(); ?>
When placed outside or inside of the product archive loop correctly returns the proper title of the active Woocommerce category. So now I just need a way to break that result down to the slug, and I’d be in business.
Am trying to do a similar thing – not sure if you figured it but I found the answer at bloke.com. Add to functions.php
I’m trying to take it a step further and want to display a repeater field in the tab and can’t figure it out. Any Woocommerce gurus out there that can help?
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
global $post, $product;
$ingredients = get_post_meta($post->ID, 'ingredients', TRUE);
// Adds the new tab
if (!empty($ingredients)) {
$tabs['test_tab'] = array(
'title' => __( 'Ingredients', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}return $tabs;
}
function woo_new_product_tab_content() {
global $post, $product;
$ingredients = get_post_meta($post->ID, 'ingredients', TRUE);
echo($ingredients);
}
I just looked at a site I have to testing WooCommerce.
The taxonomy they use is “product_cat”
to get the categories you would use
$categories = get_the_terms($post_id, 'product_cat');
`
Are you sure what you want to get is the category? I think that WooCommerce uses a custom post type and custom taxonomy for products, but I’m not sure what those are right now.
You might want to try get_the_terms() https://codex.wordpress.org/Function_Reference/get_the_terms
Added echo 'eref-' . $category[0]->slug . '';
to my content-product.php that drives the Woocommerce category archive loop, and it just returned “eref-“.
So I guess ' . $category[0]->slug . '
is not matching up to anything.
Hi,
it’s happening on a local development environment, so it’s not possible to give credentials. If it matters, there are WooCommerce and WPML installed too. The error has happened on two developers’ machines (though pretty much identical Vagrant environments with one another).
If you can come up with any more information about the settings or so, just ask and I’ll provide them.
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.