Home › Forums › Backend Issues (wp-admin) › WooCommerce Variation Gallery Deletes Other Field
Hi All,
Really hoping someone can help. I’ve got the following code:
<?php
###
# Add "Product Variation" location rule values
###
function wp_acf_location_rule_values_post_type($choices){
$keys = array_keys($choices);
$index = array_search('product', $keys);
$position = $index === false ? count($choices) : $index + 1;
$choices = array_merge(
array_slice($choices, 0, $position),
array('product_variation' => __('Product Variation', 'auf')),
array_slice($choices, $position)
);
return $choices;
}
add_filter('acf/location/rule_values/post_type', 'wp_acf_location_rule_values_post_type');
###
# Add "Product Variation" location rule match
###
function wp__acf_location_rule_match_post_type($match, $rule, $options, $field_group){
if ($rule['value'] == 'product_variation') {
$post_type = $options['post_type'];
if ($rule['operator'] == "==")
$match = $post_type == $rule['value'];
elseif ($rule['operator'] == "!=")
$match = $post_type != $rule['value'];
}
return $match;
}
add_filter('acf/location/rule_match/post_type', 'wp__acf_location_rule_match_post_type', 10, 4);
###
# Render fields at the bottom of variations - does not account for field group order or placement.
###
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $wp_i; // Custom global variable to monitor index
$wp_i = $loop;
// Add filter to update field name
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
// Loop through all field groups
$acf_field_groups = acf_get_field_groups();
foreach( $acf_field_groups as $acf_field_group ) {
foreach( $acf_field_group['location'] as $group_locations ) {
foreach( $group_locations as $rule ) {
// See if field Group has at least one post_type = Variations rule - does not validate other rules
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
// Render field Group
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
// Remove filter
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
// Filter function to update field names
function acf_prepare_field_update_field_name( $field ) {
global $wp_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$wp_i][", $field['name'] );
return $field;
}
###
# Save variation data
###
add_action( 'woocommerce_save_product_variation', function( $variation_id, $i = -1 ) {
// Update all fields for the current variation
if ( ! empty( $_POST['acf'] ) && is_array( $_POST['acf'] ) && array_key_exists( $i, $_POST['acf'] ) && is_array( ( $fields = $_POST['acf'][ $i ] ) ) ) {
foreach ( $fields as $key => $val ) {
update_field( $key, $val, $variation_id );
}
}
}, 10, 2 );
###
# Rebind the ACF events after the variations are loaded
###
add_action('acf/input/admin_footer', 'wp__acf_input_admin_footer');
function wp__acf_input_admin_footer() {
?>
<script type="text/javascript">
(function($) {
$(document).on('woocommerce_variations_loaded', function () {
acf.do_action('append', $('#post'));
})
})(jQuery);
</script>
<?php
}
This creates an option when setting up an ACF group, whereby you can then select the WooCommerce Variable product.
In my case, I’m then adding a gallery
Editing a product, if I expand the variable products, I can add/edit/save the images – no problems at all.
I then needed another ACF field for downloads. This is a file field linked to the post type Product (not variable!).
If I edit my product, add my download and save – all good!
If I edit my product in any way, then once I click save, the download file removes the attachment.
Am totally stumped!
Any help would be very much appreciated.
Thanks
You must be logged in to reply to this topic.
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.