Skip to content

Instantly share code, notes, and snippets.

@arixwap
Created May 22, 2022 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arixwap/ff61edabbdb32b54b5c70a9715befcda to your computer and use it in GitHub Desktop.
Save arixwap/ff61edabbdb32b54b5c70a9715befcda to your computer and use it in GitHub Desktop.
This is how to create ACF rule location in product variation
<?php
/**
* ACF Location Rule Product Type - Product Variation
* Untested with repeater fields
*
* https://support.advancedcustomfields.com/forums/topic/custom-fields-on-woocommerce-product-variations/
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Check if plugin woocommerce and ACF is active
if ( ! class_exists( 'WooCommerce' ) || ! class_exists( 'ACF' ) ) return;
/**
* Add new ACF location rule for product variation
*
* @param array $choices list
* @return array $choices list
*/
add_filter( 'acf/location/rule_values/post_type', 'acf_add_rule_product_variation' );
function acf_add_rule_product_variation( $choices ) {
$choices['product_variation'] = __('Product Variation', 'acf');
return $choices;
}
/**
* Render ACF fields at bottom of product variations - does not account for field group order or placement.
*
* @param int $loop
* @param array $variation_data
* @param WP_Post object $variation
* @return void
*/
add_action( 'woocommerce_product_after_variable_attributes', 'render_acf_field_product_variation', 10, 3 );
function render_acf_field_product_variation( $loop, $variation_data, $variation ) {
// Custom global variable to monitor index
global $globalVariationLoop;
$globalVariationLoop = $loop;
// Add filter to update field name
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
// Loop through all field groups
foreach ( acf_get_field_groups() as $acf_field_group ) {
foreach ( $acf_field_group['location'] as $group_locations ) {
// Set rule flag
$ruleVariationValid = false;
$ruleTaxonomyValid = true;
foreach ( $group_locations as $rule ) {
// Check rule is product variation
if ( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
$ruleVariationValid = true;
}
// Check rule product taxonomy
if ( $rule['param'] == 'post_taxonomy' ) {
$array_taxonomy = explode(':', $rule['value']);
$taxonomy = $array_taxonomy[0];
$term = $array_taxonomy[1];
if ( $rule['operator'] == '==' && has_term($term, $taxonomy, $variation->post_parent) ) {
$ruleTaxonomyValid = true;
} else if ( $rule['operator'] == '!=' && ! has_term($term, $taxonomy, $variation->post_parent) ) {
$ruleTaxonomyValid = true;
} else {
$ruleTaxonomyValid = false;
}
}
}
// Render field group
if ( $ruleVariationValid && $ruleTaxonomyValid ) {
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
}
}
}
// Remove filter
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
// Add ACF script for input image, gallery, etc
echo
"<script>
( function($) {
acf.do_action('append',$('.woocommerce_variation')); // For Product Block Editor
acf.do_action('append',$('#post')); // For Product Default Editor
} ) (jQuery);
</script>";
}
/**
* Filter function to update field names
*
* @param array $fields
*/
function acf_prepare_field_update_field_name( $fields ) {
global $globalVariationLoop;
$fields['name'] = preg_replace( '/^acf\[/', "acf[$globalVariationLoop][", $fields['name'] );
return $fields;
}
/**
* Save product variation ACF data
*
* @param int $variationId
* @param int $i index
* @return void
*/
add_action( 'woocommerce_save_product_variation', 'save_acf_product_variation', 10, 2 );
function save_acf_product_variation( $variationId, $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, $variationId );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment