Ive set up an ACF with 2 checkboxes, which are shown up in the product admin:
on the product frontend only the selected values form the backend are chooseable:
Ive the following code im my funtions:
// Display on product page
add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_single_product_pages', 1 );
function display_acf_single_product_pages() {
global $product;
$colors = get_field( 'colors', $product->get_id() );
$sizes = get_field( 'sizes', $product->get_id() );
if( $colors ): ?>
<div class="vorcartbutton">
<div class="vorcartbuttonlable">
<label for="cars">Farbe wählen:</label>
<select name="farbe" id="farbe">
<?php foreach( $colors as $color ): ?>
<option><span class="color"><?php echo $color['label']; ?></span></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<br>
<?php
if( $sizes ): ?>
<div class="vorcartbuttonlable">
<label for="cars">Größe wählen:</label>
<select name="groesse" id="groesse">
<?php foreach( $sizes as $sizes ): ?>
<option><span class="groesse"><?php echo $sizes['label']; ?></span></option>
<?php endforeach; ?>
</select>
</div>
</div>
<?php endif; ?>
<br>
<?php
}
// Display on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
$colors = get_field( 'colors', $cart_item['product_id'] );
$sizes = get_field( 'sizes', $cart_item['product_id'] );
if ( ! empty($colors) ) {
$custom_items[] = array( "name" => __("Farbe", "woocommerce"), "value" => $colors );
}
if ( ! empty($sizes) ) {
$custom_items[] = array( "name" => __("Größe", "woocommerce"), "value" => $sizes );
}
return $custom_items;
}
// Display on orders and email notifications (save as custom order item meta data)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_acf_on_orders_and_emails', 10, 4 );
function display_acf_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
$colors = get_field( 'colors', $values['product_id'] );
$sizes = get_field( 'sizes', $values['product_id'] );
if ( ! empty($colors) ) {
$item->add_meta_data( __("size", "woocommerce"), $colors );
}
if ( ! empty($sizes) ) {
$item->add_meta_data( __("color", "woocommerce"), $sizes );
}
}
In the cart the values just show array:
Can someone push me in the right direction, to get the correct values in the cart, checkout, mails and orders?
Originally taken from here Display ACF product custom fields everywhere in WooCommerce 3+
for="cars"
in no way relates toid="farbe"
orid="groesse"
. This is an example of bad copy-pasta. Loic has addressed this mistake in his answer. – mickmackusa