I got it figured out.
So yes, I did go with your suggestion to create the document attachment metabox using ACF, I then exported the code and dropped it into my functions.php so I could make some small modifications.
This was my first attempt to modify the UI for wordpress and yes I struggled because my php and js is very weak to put it politely. But I didn’t give up and now I have a nice little user interface that allows one of three field inputs to display, depending on the users desired outcome.
We all have to walk before we can learn to run, this was me learning how to crawl. lol.
I’m sorry but position side only applies to a “Field Group” and not an individual field. I need the field to stand alone, so that I can make it disappear and reappear as needed. So this will not work.
Regarding the Javascript option. I do not see why I should weigh down the website with unnecessary javascript when it is much cleaner to just a add a couple functions to the functions.php document. The code is mostly working at the moment which indicates it is a valid method of implementation. I am simply having trouble getting the saved values to actually appear in the saved fields.
Please if you are going to answer, try to answer with a solution for the problem I requested assistance with rather than trying to direct towards a completely different method of implementation.
Sorry, here are the document variables from above the previous section of code. Just in case it helps.
// Field Array DOCUMENT ATTACH Metabox
$class = 'active';
$icon = wp_mime_type_icon( $file->ID );
$title = $file->post_title;
$size = size_format(filesize( get_attached_file( $file->ID ) ));
$url = wp_get_attachment_url( $file->ID );
$explode = explode('/', $url );
$name = end( $explode );
$prefix = 'document_';
$document_meta_fields = array(
array(
'label' => 'Document Upload',
'desc' => 'Upload or Select Document.',
'id' => $prefix.'file',
'type' => 'file',
'class' => $class,
'icon' => $icon,
'title' => $title,
'size' => $size,
'url' => $url,
'name' => $name,
'defaults' => array(
'save_format' => 'object',
'library' => 'all',
),
'l10n' => array(
'select' => __("Select File",'acf'),
'edit' => __("Edit File",'acf'),
'update' => __("Update File",'acf'),
'uploadedTo' => __("Uploaded to this post",'acf'),
),
),
);
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….
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.