Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active October 11, 2017 19:22
Show Gist options
  • Save yratof/77e401201df2ef30e3ded87b312f5176 to your computer and use it in GitHub Desktop.
Save yratof/77e401201df2ef30e3ded87b312f5176 to your computer and use it in GitHub Desktop.
Woocommerce ACF to Variations
<?php
class woocommerce_acf_variations {
/**/
static function setup() {
/* Add this form to variations */
add_action( 'admin_head','acf_form_head', 20 );
add_action( 'woocommerce_product_after_variable_attributes', 'woocommerce_acf_variations::variation_field_group', 99, 3 );
add_action( 'woocommerce_product_thumbnails', 'woocommerce_acf_variations::display_thumbs', 99, 3 );
}
/* @todo – Javascript to pull in the variation images when variation is selected */
static function display_thumbs() {
$prodcut = get_product();
$variations = $prodcut->get_available_variations();
foreach ( $variations as $variation ) {
$id = $variation['variation_id'];
$gallery = get_field( 'variation_gallery', $id );
if ( ! $gallery ) {
continue;
}
// Print out the variation gallery separated
echo '<div id="gallery-variation-' . $id . '" class="gallery-variation">';
foreach ( $gallery as $image ) {
echo wp_get_attachment_image( $image['id'] );
}
echo '</div>';
}
}
static function variation_field_group( $loop, $variation_data, $variation ) {
add_action( 'admin_head','acf_form_head', 20 );
$variation_id = $variation->ID; ?>
<tr>
<td colspan="2">
<?php /* Attach the ACF Form to the variation */
acf_form(
[
'id' => 'acf_variaton_form',
'post_id' => $variation_id,
'form' => true, // Show the form on the variation
'label_placement' => 'top',
'instruction_placement' => 'label',
'field_groups' => [ 336 ], // This is the ID of the ACF Group
'return' => add_query_arg(
[
'post_id' => $variation->post_parent,
'updated' => 'true',
],
get_edit_post_link( $variation->post_parent, '' ) // Output the edit link
),
]
); ?>
</td>
</tr>
<?php
}
}
@rafatriolo
Copy link

Hi there,

First of all tks for sharing this code, it really worked for me. But this is not saving the values. Could you help me with this ? Tks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment