Support

Account

Forum Replies Created

  • Hi there,

    About your first issue.
    When you’re using $artists = $select['value']; in your loop : you reset $artists variable every time a loop is completed.

    That’s why you store only the last value of your repeater field.

    I assume you’re working on the same file template.
    First, create an array of post IDs contained in your repeater field.
    Then, use this array to build your custom WP_Query.

    //Let's create an empty array
    $artists = array();
    
    //Loop over your repeater field
    if(have_rows('release_artist')):
      while(have_rows('release_artist')): the_row();
        $related_artists = get_sub_field('artist');
        //Working on post object
        if( $related_artists && is_object($related_artists) && !is_wp_error($related_artists) ) {
          $artists[] = $related_artists->ID;
        }
        //Working on an array
        if( $related_artists && is_array($related_artists) ) {
          foreach($related_artists as $post_object) {
            if( is_object($post_object) && !is_wp_error($post_object) ) {
              $artists[] = $post_object->ID;
            }
          }
        }
      endwhile;
      //You should have an array containing all post IDs from release_artists repeater
      //error_log(print_r($artists, true));
    endif;
    
    //Probably better to check if everything is valid in $artists array from there
    
    //Then you could use your array of post ID to build your custom Query
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $artist_release_args = array(
    		'post_type' 	 	 => 'releases',
    		'post_status' 	 => 'publish',
        'post__in'       => $artists,
    		'orderby' 		 	 => 'modified',
    		'order' 		 		 => 'DESC',
    		'posts_per_page' => 6,
    		'paged' 		 		 => $paged
    );
    $artist_release_query = new WP_Query($artist_release_args);

    Don’t forget to check if your query has posts when you loop over it.
    Add content-archive/content-none accordingly (or any template part needed).

    Note that is_object() and !is_wp_error() checks are probably not strictly necessaries.
    ACF normally returns an empty value if no post is selected in your sub field.

  • Hi there,

    I think this is more a divi “issue”, but it could be fixed without code.

    I suggest to modify your display rule for your copyright field and store it directly in your post.

    It’s not as user friendly as storing it in media datas (which could be used within divi editor).
    But it should be correctly display from divi.

    Possible explanation
    Divi probably looks for your image copyright value in your post metas.

    The value of your acf field is not directly stored in your post metas, but in image metas.
    Your post metas just store the featured image ID.

    So when Divi search for your copyright field it doesn’t found anything as it’s not related to post ID (which divi probably gets from context there).

    Hope it helps a bit,

    Rémi.

  • Hi there,

    Looks like you’ve misused the_field() function in your CSS.
    Please try the code below.

    $image = get_field('background_image');
    if( $image ): ?>
    <style type="text/css">
    hero {
    background-image: url (<?php echo esc_url($image);?>);">"
    }
    </style>
    <?php endif; ?>

    Remember : get_field() function store a value of an ACF field.
    So as you did, you could check if value is valid, then you could echo/print this value.

    the_field() function directly display your acf field value within your template.
    So you don’t have to echo/print the_field().

    Consider using the_field() only when you’re 100% sure the value is not empty and is valid/usable.

  • Hi there,

    You’re right, my first reply is for front-end orders only.

    Could you try the code below.
    Let me know if it helps (i could re-use it someday).

    Cheers,

    Rémi

    First, we write a small helper function to get and reorder your acf fields

    <?php
    //Helper function - Reorder acf datas
    function maybe_reorder_acf_fields($order_id) {
    	$name 		= get_field('{slug/key}', $order_id) ? get_field('{slug/key}', $order_id) : null; //CHANGE with your own acf slug/key
    	$start_date = get_field('{slug/key}', $order_id) ? get_field('{slug/key}', $order_id) : null; //CHANGE with your own acf slug/key
    	$adults 	= get_field('{slug/key}', $order_id) ? get_field('{slug/key}', $order_id) : null; //CHANGE with your own acf slug/key
    	$children 	= get_field('{slug/key}', $order_id) ? get_field('{slug/key}', $order_id) : null; //CHANGE with your own acf slug/key
    	
    	//Don't remember how datas are validated from get_field() - maybe esc/filter_var functions are useless
    	$custom_acf_metas = array(
    		'_acf_tour_customer_name' => esc_html($name),
    		'_acf_tour_start_date' 	  => esc_html($start_date),
    		'_acf_tour_adults_nbr' 	  => filter_var($adults, FILTER_SANITIZE_NUMBER_INT),
    		'_acf_tour_child_nbr' 	  => filter_var($children, FILTER_SANITIZE_NUMBER_INT)
    	);
    	
    	return $custom_acf_metas;
    }

    Next we use WP save_post hook to add OR overwrite custom metas each time an order is saved in backend.
    https://developer.wordpress.org/reference/hooks/save_post/

    Note : my_save_custom_order_metas() will add your acf datas to all products in an order.
    You may adapt this code and probably your acf datas depending on your needs.

    <?php
    //Add new metas to order item(s)
    add_action( 'save_post', 'my_save_custom_order_metas', 10, 3 );
    function my_save_custom_order_metas( $order_id, $order, $update ) {
    	//Not sure if post revisions are used for shop orders - it returns false on a valid $order_id
    	if ( !is_admin() || get_post_type( $order_id ) !== 'shop_order' || wp_is_post_revision( $order_id ) ) return;
    	
    	//Uncomment those two lines if you want to target specific order status
    	// $order_status = $order->get_status();
    	// if( $order_status !== 'wc-processing' ) return null;
    	
    	$custom_acf_metas = maybe_reorder_acf_fields($order_id);
    	//Loop over every items added to your order
    	if( $order->get_items() ) {
    		foreach ( $order->get_items() as $item_id => $item ) {
    			//Nested loop over reordered ACF datas
    			foreach($custom_acf_metas as $new_meta_key => $new_meta_value) {
    				//Uncomment the two lines below if you don't want to overwrite your values
    				//$new_meta_exists = wc_get_order_item_meta( $item_id, $new_meta_key, true);
    				// if( $new_meta_exists !== false ) continue;
    				if( $new_meta_value !== null ) {
    					//Probably not really needed too - could be useful
    					$new_meta_key = sanitize_key($new_meta_key);
    					$new_meta_value = sanitize_meta( $new_meta_key, $new_meta_value, 'order_itemmeta' );
    					//Add new meta
    					$item->add_meta_data(
    						$new_meta_key,
    						$new_meta_value,
    						true
    					);
    				}
    			}
    			//Don't forget to save modified order here
    			$order->save();
    		}
    	}
    }

    Finally we keep our last 2 woocommerce filters to modify back-end display key + email and customer account item details (human readable for both).

    <?php
    //Modify our custom meta key to make it human readable from back-end
    add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
    function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
    	//Don't know if there is a better way to retrieve $order_id (and related acf datas)
    	$item_datas = $item->get_data();
    	$order_id = $item_datas['order_id'];
    	$custom_acf_metas = maybe_reorder_acf_fields($order_id);
    	
    	// Change displayed label for specific order item meta key
    	foreach($custom_acf_metas as $new_meta_key => $new_meta_value) {
    		switch($new_meta_key) {
    			case '_acf_tour_customer_name' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_start_date' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_adults_nbr' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_child_nbr' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    		}
    		//Not sure if is_admin is necessary
    		if( is_admin() && $item->get_type() === 'line_item' && $meta->key === $new_meta_key ) {
    			$display_key = __($new_display_key, "my-text-domain" );
    		}
    	}
    	return $display_key;
    }
    
    // Add custom cart item data to emails AND customer account order details
    add_filter( 'woocommerce_order_item_name', 'my_custom_data_in_email', 10, 2 );
    function my_custom_data_in_email( $product_name, $item ) {
    	$item_datas = $item->get_data();
    	$order_id = $item_datas['order_id'];
    	$custom_acf_metas = maybe_reorder_acf_fields($order_id);
    	
    	// Change displayed label for specific order item meta key
    	foreach($custom_acf_metas as $new_meta_key => $new_meta_value) {
    		switch($new_meta_key) {
    			case '_acf_tour_customer_name' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_start_date' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_adults_nbr' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    			case '_acf_tour_child_nbr' :
    				$new_display_key = '[your_display_key]'; //CHANGE with your own value
    			break;
    		}
    		if( isset( $item[$new_meta_key] ) ) {
    			$product_name .= sprintf(
    			'<ul><li>%s: %s</li></ul>',
    			__( $new_display_key, 'RemSEO' ),
    			esc_html( $item[$new_meta_key] )
    			);
    		}
    	}
    	return $product_name;
    }
  • Hi there,

    I already made something similar but not with ACF datas.

    Maybe you could try something like below.
    Let me know if it helps.

    Rémi

    <?php
    
    // Add custom cart item data when a product is added to cart from product page
    add_filter( 'woocommerce_add_cart_item_data', 'my_custom_cart_datas', 10, 3 );
    function my_custom_cart_datas( $cart_item_data, $product_id, $variation_id ) {
    	if( !isset( $_POST['new_post_data'] ) ) {
    		$cart_item_data['new_post_data'] = get_field('name', $order_id);;
    	}
    	
    	return $cart_item_data;
    }
    
    // Displaying custom cart datas in cart
    add_filter( 'woocommerce_get_item_data', 'my_display_custom_cart_datas', 10, 2 );
    function my_display_custom_cart_datas( $item_data, $cart_item_data ) {
    	if( isset( $cart_item_data['new_post_data'] ) ) {
    		$item_data[] = array(
    			'name' => __( 'Displayed key', 'my-text-domain' ),
    			'value' => wc_clean($cart_item_data['new_post_data'])
    		);
    	}
    	return $item_data;
    }
    
    // Add custom meta to item (=product) in order (visible in order in back-end)
    add_action( 'woocommerce_checkout_create_order_line_item', 'my_custom_order_line', 10, 4 );
    function my_custom_order_line( $item, $cart_item_key, $values, $order ) {
    	if( isset( $values['new_post_data'] ) ) {
    		$item->add_meta_data(
    			'_new_item_line_meta',
    			$values['new_post_data'],
    			true
    		);
    	}
    }
    
    //Modify our custom meta key display
    add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
    function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
        // Change displayed label for specific order item meta key
        if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_new_item_line_meta' ) {
            $display_key = __("Displayed key", "my-text-domain" );
        }
        return $display_key;
    }
    
    // Add custom cart item data to emails
    add_filter( 'woocommerce_order_item_name', 'my_custom_data_in_email', 10, 2 );
    function my_custom_data_in_email( $product_name, $item ) {
    	if( isset( $item['new_post_data'] ) ) {
    		$product_name .= sprintf(
    		'<ul><li>%s: %s</li></ul>',
    		__( 'Displayed key', 'my-text-domain' ),
    		esc_html( $item['_new_item_line_meta'] )
    		);
    	}
    	return $product_name;
    }
  • Hi Simon,

    I guess ‘field_64e76f9210338’ is your group field key.
    If that’s the case, i don’t think you need it if you only want to update your repeater inside your group.

    Maybe you could try something like the code below.
    Let me know if it helps you.

    Cheers

    <?php
    //be sure that's your repeater key
    $repeater_key = 'field_64e765bfc42ef';
    $repeater_name = 'your_rep_name';
    
    //$rep_count will be used to create a new repeater OR add rows to an existing repeater
    $rep_count = count(get_field($repeater_name)); //not sure if it works with field key
    
    foreach ($event->lineup as $item) {
    	$updated_rep = []; //Not sure if this is needed - it won't hurt
    	
    	//Create an entirely new repeater
    	if($rep_count === 0) {
    		$new_rep = array(
    			array(
    				'field_64e770e717754' => $item->details,
    				'field_64e770f317755' => $item->time
    			),
    		);
    		update_field( $repeater_key, $new_rep, $post_id );
    	} 
    	//Or keep previous values and add new rows
    	elseif($rep_count >= 1) {
    		$new_row = array(
    			'field_64e770e717754' => $item->details,
    			'field_64e770f317755' => $item->time
    		);
    		add_row($repeater_key, $new_row, $post_id);
    	}
    }
  • Hi there,

    This is an old thread but if my code could be useful, i let it here.

    IMPORTANT : Please test it in a dev environment before.
    I adapted and didn’t test the multisite adaptations.
    It works on a single wordpress installation on ACF 5.1.5+ (tested on acf 6.2)

    Note 1 : i hooked on ‘admin_init’ but maybe there is a better hook.
    Note 2 : Tested on WP 6.2 only but i guess it would work far below as we only use get_post() and wp_update_post()

    Feel free to correct it and share it 🙂

    
    <?php
    //Modify ACF group/fields setting and save it globally (DB + local JSON)
    add_action('admin_init', 'modify_acf_settings');
    function modify_acf_settings() {
    	//Working on specific fields/posts/groups - REPLACE with your own keys
    	$acf_posts_to_update = array(1234);
    	$acf_groups_to_sync = array('group_12345678abcde'); //Used only if you're using local JSON
    	$acf_fields_to_update = array( 'field_12345678abcde' );
    	$stored_posts = array();
    	$stored_acf_groups = array();
    	
    	//You could modify any field settings from here
    	//@see https://www.advancedcustomfields.com/resources/acf-update_field/
    	foreach($acf_fields_to_update as $field) {
    		$is_field_exists = get_field_object($field);
    		if($is_field_exists !== false) {
    			$filter_identifier = strval('acf/update_field/key='.$field);
    			add_filter($filter_identifier, function($field) {
    				//$field['required'] = true;
    				//etc...
    			}, 10, 1);
    		}
    	}
    	
    	//Then we use WP native functions to modify ACF posts
    	$updated_post = false;
    	$i = 0;
    	foreach($acf_posts_to_update as $post_to_update) {
    		$post_obj = get_post($post_to_update);
    		//You could grab and modify any datas returned with get_post()
    		// Example -> $post_content = $post_obj->post_content;
    		
    		if($post_obj !== null) {
    			//Here i want to active a disable group if woocommerce is active
    			//All we have to do is change the post status (as ACF groups are basically custom post types)
    			if (&& in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    				$current_status = $post_obj->post_status;
    				if($current_status == 'acf-disabled') {
    					$post_obj->post_status = 'publish';
    				}
    				//Don't forget to update post and validate/re-serialize modified datas if needed
    				wp_update_post( $post_obj );
    				$updated_post = true;
    				$stored_post[$i] = $post_obj;
    			} else {
    				//Disable it if woocommerce is not active and the group is publish
    				$current_status = $post_obj->post_status;
    				if($current_status == 'publish') {
    					$post_obj->post_status = 'acf-disabled';
    				}
    				wp_update_post( $post_obj );
    				$updated_post = true;
    				$stored_post[$i] = $post_obj;
    			}
    		}
    	$i++;
    	}
    	
    	//Last step - Sync modified posts with JSON files
    	//note : using $updated_post but you could use any check
    	$update_modified_group = false;
    	if ( $updated_post === true ) {
    		$i = 0;
    		foreach($acf_groups_to_sync as $acf_group) {
    			$group_to_save = acf_get_field_group($acf_group);
    			$group_to_save['active'] = 1;
    			$update_modified_group = acf_write_json_field_group( $group_to_save );
    			//Add an admin notice is group was not savec correctly
    			if( boolval($update_modified_group) !== true ) {
    				add_action( 'admin_notices', 'acf_not_sync_notice' );
    			} else {
    				$stored_acf_groups[$i] = $group_to_save;
    			}
    		$i++;
    		}
    	}
    	
    	//Finally, we will sync changes in other $blog in a multisite network
    	$current_blog_id = get_current_blog_id();
    	$sites = get_sites( 
    		array( 
    			'number' => 50,
    			'site__not_in' => $current_blog_id, // exclude current site from the loop
    		) 
    	);
    	foreach( $sites as $site ) {
    		switch_to_blog( $site->blog_id );
    		if( is_array($stored_post) && !empty($stored_post) ) {
    			foreach($stored_post as $post) {
    				wp_update_post( $post );
    			}
    		}
    		
    		if( is_array($stored_acf_groups) && !empty($stored_acf_groups) ) {
    			foreach($stored_acf_groups as $group) {
    				$update_modified_group = acf_write_json_field_group( $group );
    			}
    		}
    	}
    	
    	//Don't forget to restore environnement
    	restore_current_blog();
    }
    
    function acf_not_sync_notice() {
        ?>
        <div class="notice error my-acf-notice is-dismissible" >
            <p><?php _e( 'Some ACF fields were not synced correctly. Please save the group fields manually.', 'my-text-domain' ); ?></p>
        </div>
    
        <?php
    }
  • Hi there,

    Wordpress has a built-in function to determine the post ID from a given URL : url_to_postid( string $url );
    It will return the post ID or 0 on failure.

    1) So first, you’ll probably target your clicked url with javascript and store it.
    2) Then you could use the wordpress function to get the post ID and your acf values.
    3) Finally, you launch your modal if you get all your needed datas.

    Let me know if it helps,

    RemSEO

  • Hi there,

    If i understand your problem: you can see your fields in back-office but not in front ?

    If you want to add content with ACF, you’ll have to modify your “Client template” file.
    The fields you add in back-end are not active in front, until you add them in your templates.

    If you’re used to child theme, it’s pretty basic:
    1) copy the template you want to modify in your child theme (with the same directory path).
    2) Use ACF functions to get you values (get_field() and get_sub_field() functions do the work).
    3) Add your new $values in your template.

    Let me know if it helps,

    RemSEO

  • Hi there,

    Here, you’re calculating a new count at each loop.
    By default a $variable will reset at each loop’s iteration.

    To calculate all iterations, you’ll need to concatenate your counter and you increment it at the end of each loop.

    Please, try something like this:

    <?php global $post; foreach ( $groups as $term_id => $posts ) { $term = get_term( $term_id );?>
                      
    	<?php foreach ( $posts as $post ) { setup_postdata( $post ); ?>
    	
    	<?php if( have_rows('irl_today_data') ):?>
    		  <?php $count = 0;
    		  while ( have_rows('irl_today_data') ) : the_row();
    		  $acf_loop_count .= $count; //Continue your count at each loop
    		  ?>
    		  
    			<?php if( get_row_layout() == 'irl_today_website' ):?>
    			  
    			<?php endif;?>
    			<?php if( get_row_layout() == 'irl_today_social_media' ):?>
    			  
    			<?php endif;?>
    			
    			<?php echo $acf_loop_count; //Echo inside the loop to output your counter at each loop (4 5 6) ?>
    			
    		  <?php $count++; endwhile;?>
    		  
    		  <?php echo $acf_loop_count; //Echo outside the loop to output your final value (6) ?>
    		  
    	<?php endif;?>
    		
    	<?php endif; ?>
    <?php } } wp_reset_postdata();?>

    Let me know if it helps,

    RemSEO

  • Hi Nicolas,

    Normally, you could use acf conditional logic in front end.

    Anyway, with this Jquery snippet you could hide/show you fields only when “Joueur” is selected :

    $( function(){
    
    //Hide all acf fields targeting their class
    $('.acf-user-register-fields').hide();
    
    // Get the value from the selected option in a dropdown and show previously hidded fields
    var selectedVal = $( "#wp_rar_user_role option:selected" ).val();
    	if(selectedVal == 'joueur') {
    		$('.acf-user-register-fields').show();
    	}
    	console.log(selectedVal);
    
    });

    Tested and working with jquery 3.x

    I’m not sure it’s the best way to do it, but let me know if it helps.

    RemSEO

  • Hi Nicolas,

    You can dynamically display fields in your template, based on the value selected in your “Profil” field.

    Assuming you’re using a selection field with a single value selectable.
    It mays work if you keep the “returned value” setting as “value” and you fill your “choices” setting with value only (not “value : label”).

    So you could have something like:

    if (get_field('your_selection_field_name') == 'Joueur') {
    $condition = true;
    }
    
    //Display your selection field first
    echo the_field('your_selection_field_name');
    
    //Then, display your conditional fields based on your condition
    if ($condition == true;) {
      echo $conditional_field_1;
      echo $conditional_field_2;
      echo $conditional_field_3;
      //Etc...
    }

    Let me know if it helps,

    RemSEO

  • Thanks to a friend, i finally managed to organize my datas.

    When you’re creating an array to store your values in a loop, this array doesn’t reset at each loop.
    You just have to reset it after each nested loop.

    And voilà! You have some custom $cart_object datas, ready to use to modify prices based on whatever you want.
    Maybe there is a cleaner way to do it, but for now, it’s working as attended for WordPress 4.9.x/5.x.x and Woocommerce 3.5+.

    If it could be usefull for anyone here is the corrected code

    add_filter( 'woocommerce_add_cart_item_data', 'woocommunity_custom_cart_item_data', 20, 3 );
    function woocommunity_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    
    //Targeting right product using function parameter
    //And counting main loop to organize datas in their matrix
    if(have_rows('custom_prices_grids', $product_id)):
    $matrice_count = 0;
    while(have_rows('custom_prices_grids', $product_id)): the_row();
    	
    	//General limitation - already good to go
    	$var_limits = get_sub_field('custom_prices_variation_limitations');
    	$role_limits = get_sub_field('custom_prices_role_limitations');
    	
    	//Targeting layout - really facultative here as i have a single layout
    	if( get_row_layout() == 'custom_prices_grid_details' ):
    		
    		//Reorder each field in arrays
    		while(have_rows('custom_prices_quantity_steps')): the_row();
    
    			$min_qty[] = intval(get_sub_field('custom_prices_quantity_min'));
    			$max_qty[] = intval(get_sub_field('custom_prices_quantity_max'));
    			$custom_price[] = intval(get_sub_field('custom_prices_qty_new_price'));
    			$fixed_price_var[] = intval(get_sub_field('custom_prices_qty_fixed_value'));
    			$percent_price_var[] = intval(get_sub_field('custom_prices_qty_percent_value'));
    			$var_type[] = get_sub_field('price_type_calculation');
    		
    		endwhile;
    		
    		//Storing those fields in their matrice as $cart_item_datas
    		$cart_item_data['matrice-'.$matrice_count]['promo_type'] = $var_type;
    		$cart_item_data['matrice-'.$matrice_count]['min_qty'] = $min_qty;
    		$cart_item_data['matrice-'.$matrice_count]['max_qty'] = $max_qty;
    		$cart_item_data['matrice-'.$matrice_count]['new_price'] = $custom_price;
    		$cart_item_data['matrice-'.$matrice_count]['fixed_vars'] = $fixed_price_var;
    		$cart_item_data['matrice-'.$matrice_count]['percent_vars'] = $percent_price_var;
    		$cart_item_data['matrice-'.$matrice_count]['limits_by_var'] = $var_limits;
    		$cart_item_data['matrice-'.$matrice_count]['limits_by_role'] = $role_limits;
    		
    		//Cleaning arrays at each loop
    		//Contrary to single values stored - Our arrays don't reset at each loop
    		unset($var_type); 
    		unset($min_qty); 
    		unset($max_qty); 
    		unset($custom_price); 
    		unset($fixed_price_var); 
    		unset($percent_price_var); 
    		unset($var_limits); 
    		unset($role_limits);
    		
    	$matrice_count++;
    	endif;
    	
    endwhile; endif;
    
    return $cart_item_data;
    }
  • Hi there,

    I think you just have to echo your $term only.

    <?php echo $term; ?>

    When you register a relashionship field (taxonomy), you have an option called “returning value”.
    By default, this field return an “ID value”.

    If you want to use several properties of your taxonomy (like description and title), you have to check the returning value as an Object.

    Then you target the right property (name, description etc…)

    Let me know if it helps,

    RemSEO.

  • Hi there,

    Your code seems good.
    As you said, if you’re calculating your “total_return” into first snippet only, your calculation is working.

    Why don’t you update all your field in a single snippet (your first one) ?

    If you really need to use different snippets for each field, maybe you could add different priorities to your actions.
    See documentation: https://www.advancedcustomfields.com/resources/acf-save_post/

    So keep your first snippet without modifications (default priority is 10).
    And try adding a priority of 15 for your second snippet :
    add_action('save/post' 'my_acf_update_total_return', 15);

    Let me know if it helps,

    RemSEO

  • Hi Chris,

    If i understand well, you’re trying to integrate your shortcode into your template file, for a specific custom post type.

    I think it’s better to store your acf value first and use it in your shortcode after.

    You could use something like this :

    <?php
    //First, you have to tell that you want a value of a single cpt
    $ID = get_the_id();
    //Then, you store your targeted value as $variable
    $title = get_field('title', $ID);
    
       //Finally you use your $variable to build your custom shortcode
       echo do_shortcode('[wbcr_php_snippet id="790" name="$title"]');
    
    ?>

    Let me know if it helps,

    RemSEO

  • Hi Simon,

    Looks like you’ll have to use the get_row_layout() function.
    Here is the ACF documentation: https://www.advancedcustomfields.com/resources/get_row_layout/

    You could have something like this :

    EDIT: use “wp_enqueue_script()” to add your scripts and not “get_template_part()” function as i said in my first reply.

    if(have_rows('flexible_field_name')):
    while(have_rows('flexible_field_name')): the_row();
      
      //Don't forget to get your actual layout name
      if( get_row_layout() == 'slider' ):
        wp_register_script( 'unique_name', get_stylesheet_directory_uri() . '/js/your_file.js');
        wp_enqueue_script( 'unique_name' );
      endif;
    
      if( get_row_layout() == 'gallery' ):
        wp_register_script( 'unique_name', get_stylesheet_directory_uri() . '/js/your_file.js');
        wp_enqueue_script( 'unique_name' );
      endif;
    
    endwhile;
    endif;

    Let me know if it helps,

    RemSEO.

  • No problem Michael 😉

    I think your white space is optional here.

    <?php $customclasses = $bgcolor . get_field('justering'); ?>

    It will output: class1 class2

    But if you don’t add space in your concatenation operator:
    <?php $customclasses = $bgcolor.get_field('justering'); ?>

    It will output: class1class2

  • Hi there,

    Could you please try something like this :

    <?php
    $group = get_field('group_field');
    $heading = $group['heading'];
    $text = $group['text'];
    $repeater = $group['links_list']; //
    ?>
    
    <h3><?php echo $heading; ?></h3>
    <?php echo $text; ?>
    <?php if( have_rows($repeater) ) : ?>
    <ul class="links-list">
        <?php while( have_rows($repeater) ) : the_row(); $link = get_sub_field('link'); ?>
        <li><a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></li>
        <?php endwhile; ?>
    </ul>
    <?php endif;?>

    Explanation

    As a repeater field, the group field is a row of datas, but it contains a single row.
    (as it’s explained in the documentation: https://www.advancedcustomfields.com/resources/group/)

    In your code, you tried to have the repeater fields datas, without indicating in which $row your repeater field is (your group field).

    Let me know if it helps.

  • Hey Michael,

    I think i indicated a wrong syntax in my first reply (my bad!)

    Please, try with this snippet :

    <?php $field = get_field(‘field_name’); ?>
    <article id=”post-<?php the_ID(); ?>” <?php post_class(‘et_pb_post clearfix’ . $no_thumb_class . $overlay_class . $field) ?>>

    To keep it simple, the post_class() function could take two optional parameters : $class and $post_id.
    (you don’t need the $post_id as you want to add a class in all blog posts).

    To add multiple $variables to the $class parameter, Divi simply use the php concatenation.
    (see php documentation about concatenation, it’s useful : https://www.php.net/manual/en/language.operators.string.php).

    So with the snippet above, you stock the value of your field.
    Then you use it as a $variable like the other divi $variable.

    Let me know if it helps.

  • Hi there,

    Looks like you’ll need to use a child theme here.

    If you modify directly your theme, your modifications will be suppressed when you’ll update your theme.

    You’re lucky! The Divi Elegant Theme documentation is well done for that :
    https://www.elegantthemes.com/blog/divi-resources/divi-child-theme

    Just follow the instructions to overwrite properly your blog.php template.

    Then you could add your custom field value to post_class() with acf function “the_field()”.
    Your modified blog.php template will probably look like this :

    <article id=”post-<?php the_ID(); ?>” <?php post_class(‘et_pb_post clearfix’ . $no_thumb_class . $overlay_class . the_field(‘your_field_name’) ); ?>>

    You can also check the WordPress documentation about the post_class() function: https://developer.wordpress.org/reference/functions/post_class/

    Hope it helps.

  • Hi,

    I’m not sure, but you could probably use the “acf/load_field” filter.
    https://www.advancedcustomfields.com/resources/acf-load_field/

    And a small function like :

    function read_only_field($field) {
    $field[‘readonly’] = true;
    return $field;
    }
    add_filter(‘acf/load_field’, ‘read_only_field’);

    You could target your field using the filter :

    add_filter(‘acf/load_field/name=field_name’, ‘read_only_field’);
    add_filter(‘acf/load_field/key=field_key’, ‘read_only_field’);

  • Hi,

    The ACF number field will return an integer.
    In php you’ll never have some decimal with an integer value, you have to work with float values.

    Maybe you could simply try something like this

    <?php
    $price_html = $product->get_price(); //It will return a string
    $x = get_field(‘custom-price’); //Integer
    $float_price = floatval($price_html); //Float
    $float_custom_price = floatval($x); //Float
    $finalPrice = ($float_price + $float_custom_price); //Float
    echo $finalPrice;
    ?>

    But this will only display your custom price.
    If you want to actually modify your price you have to use woocommerce filters.
    In product page :

    // Simple, grouped and external products
    add_filter(‘woocommerce_product_get_price’, ‘custom_price’, 99, 2 );
    add_filter(‘woocommerce_product_get_regular_price’, ‘custom_price’, 99, 2 );
    // Variations
    add_filter(‘woocommerce_product_variation_get_regular_price’, ‘custom_price’, 99, 2 );
    add_filter(‘woocommerce_product_variation_get_price’, ‘custom_price’, 99, 2 );

  • Hi,

    This post is a bit old but it mights be useful for others.

    This plugin is working for me (ACF 5.8.5 / WordPress 4.x or 5.x)
    It uses the same functions describe in the documentation: https://www.advancedcustomfields.com/resources/creating-a-new-field-type/

    As far as i know it’s update for all versions of ACF 5.
    (There is a callback file for ACF4, i don’t know if it’s working).

    BUT

    You could only use the ID return value, the object return doesn’t work (i don’t know why).

    Basically it uses the global variable $wp_roles and the method $wp_roles->roles to get all roles.

    It uses also a get_role() function to display the name in back-end.

    If you want a role object using this plugin, you could retrieve it with the ID’s returned.
    It’s not the best way but it’s working.

    Finally it could be really usefull to integrate this field in a future version 😉

Viewing 24 posts - 1 through 24 (of 24 total)