Support

Account

Forum Replies Created

  • @johnw6761

    I missed the essential point: what to examine. Try to display what’s in acf()->input

  • @johnw6761

    Ha ha! 🙂 We never know. Well, strange issue. ACF being activated, the issue must be about input property. Did you try to print_r or var_dump it to see what’s in it?

  • @johnw6761,

    This sounds weird. It’s like if acf() function was not existing or there was no input property. Sure ACF is activated?

  • @johnw6761,

    Could you please copy-paste code in nevelli/functions/theme-support.php around line 148?

  • Hi @johnw6761,

    I guess you are trying to call save_post() on $post or some other variable name. Make sure this variable exists. Maybe is $post (or other name) global. Did you try global $post; before you call save_post()?

  • Hi @johnw6761.

    If you have a 500 internal server error, either there is a syntax error or something wrong occurs in the code (ex. trying to access a property on a non-objet variable).

    Could you please see if you find an error message in the latest fatal-errors-YYYY-MM-DD-*.log located in ./wp-content/uploads/wc-logs. Else, it won’t help much.

    Thanks.


    Maxime.

  • Hey Rileym3,

    In my understanding, you created a product attribute named “variable” (WordPress Dashboard > Products > Attributes). Did you then:
    – create a “variable product” in WooCommerce, (“Product Data” section set to “variable product”),
    – selected your “variable” attribute in the “Attributes” tab
    – created a variation based on the selected attribute in the “Variations” tab?

    You would then find your “UPC code” variable below the “Description” field after you opened the variations you created for the product.

  • For some reason, when trying to use get_fields($variation_id) later (for example on front-end), you won’t be able to get the saved fields.

    This is quiet weird as update_field use to store fields seems to do the job the right way. Custom fields are saved in postmeta table : both field_name and _field_name are saves, the first with field value, the second with field key. Furthermore, these fields are restored the right way when editing product variations.

    For now, I have to use get_field('field_name', $variation_id) to get data.

  • Oups! “woocommerce_save_product_variation” action does not work properly.

    Replace with:

    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	if ( ! empty( $_POST['acf_variation'][$variation_id] ) && is_array( $fields = $_POST['acf_variation'][$variation_id] )  ) {
    		foreach ( $fields as $key => $val ) {
    			update_field( $key, $val, $variation_id );
    		}
    	}
    
    }, 10, 2 );
  • Found a solution for ACF fields requiring events to be attached.

    In your php code, add the following:

    function xxx_admin_head_post() {
    	global $post_type;
    	if ($post_type === 'product') {
    		wp_register_script( 'xxx-acf-variation', get_template_directory_uri() . '/js/acf-variation.js', array(
    			'jquery-core',
    			'jquery-ui-core'
    		), '1.1.0',
    			true ); // Custom scripts
    		
    		wp_enqueue_script( 'xxx-acf-variation' ); // Enqueue it!
    
    	}
    }
    
    /* actions fired when adding/editing posts or pages */
    /* admin_head-(hookname) */
    add_action( 'admin_head-post.php', 'xxx_admin_head_post' );
    add_action( 'admin_head-post-new.php',  'xxx_admin_head_post' );

    Create js/acf-variation.js with the following code:

    jQuery(document).ready(function($) {
        $( '#woocommerce-product-data' ).on( 'woocommerce_variations_loaded', function(){
            acf.doAction('ready');
        });
    });
  • This piece of code work for me, setting the location to “Taxonomy equals <<whatever attribute>>”. Note that, as variations are loaded dynamically, javascript events are not attached to fields. Thus, using the gallery or image does not work: click on buttons does nothing. Trying to find a fix…

    $GLOBALS['wc_loop_variation_id'] = null;
    
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    }
    
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    		}
    	}
    
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	}
    
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    
    	return $field;
    }, 10, 1);
  • Hi,

    This piece of code may help you. To use it, add a location to your field group where Taxonomy equals your attribute name. Please note, this only work with “basic” fields, ie. not relying on javascript (gallery or image buttons do not work).

    $GLOBALS['wc_loop_variation_id'] = null;
    
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    }
    
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    		}
    	}
    
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	}
    
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    
    	return $field;
    }, 10, 1);
Viewing 12 posts - 1 through 12 (of 12 total)