Home › Forums › General Issues › Add Custom Fields to Woocommerce Cart
I’d like to say I’m trying but honestly I don’t even know where to start. I have the custom fields displaying in the product pages just fine, no problem. I have copied the cart template into a child theme and have made some modifications already so I’m in the right place I just have no clue how to draw in the custom fields.
I want to add Ticket Information (Seat #, Row #, Section #) to the cart page. I tried to pull the fields with the_field(); but obviously it doesn’t work because its not the product page. I googled my head off on this and can’t find anything on it. Can someone post a code snippet that would help to draw in my custom fields?
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.
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….
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>';
}
?>
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 );
Glad you were able to get this figured out. Sorry I didn’t reply early, the weekend was extremely busy for me.
Who have you show the input field on the checkout page.
<?php the_field(‘your-field-name’, $product_id ); ?>
i see no field
I can add the <?php the_field(‘acf-field-name’, $product_id ); ?> to the cart.php file and this works fine.
However is there a solution to make this survive a plugin update?
The topic ‘Add Custom Fields to Woocommerce Cart’ is closed to new replies.
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.