Support

Account

Home Forums General Issues Deleting values in one Relationship field based on Values in another

Unread

Deleting values in one Relationship field based on Values in another

  • Problem: Delete wish list items on a Consumer record if those items are saved to a sales Catalog. Consumer, Catalog, and Items (Daylilies) are all CPTs. Wish List and Catalog Items are Relationship fields. I wanted to delete items from a consumer wish list if those items were saved to a catalog associated to that consumer. My code is below. I followed the code in the Bidirectional Relationships tutorial, although my process was slightly different. It took me a while to get this to work, but I was so jived when it did. The gotcha for me was not to use the filter, but to trigger off the save_post action of the new or updated catalog record.

    Disclaimer: The code could probably use some tweaking. I am providing it because I couldn’t find many examples of coding against relationship fields. It’s just an example. Have fun with it.

    Consumer post has wish list items stored in a Relationship field.
    Catalog post references consumer and has catalog items in a Relationship field.

    If Catalog item matched Wish List item for that Consumer, delete the Wish List Item.

    add_action(‘save_post’, ‘daylilies_save_catalog_check_wish_list’,10,3);
    function daylilies_save_catalog_check_wish_list($post_id){

    // Don’t run this code if this post is not a catalog post type
    if(! $post_id){
    return;
    }

    // If there is a post IdD Determine the Post Type
    if($post_id){
    $post_post_type = get_post_type($post_id);
    }

    // Return if anything but a catalog post type
    if(“catalog” != $post_post_type){
    return;
    }

    // Call the function that will remove wish list items if they match items in this catalog
    daylilies_remove_items_from_wish_list($post_id);
    return;
    }
    So, if post being saved is a catalog post, let’s do some work:

    function daylilies_remove_items_from_wish_list($catalogID){

    // Run this function after saving a catalog
    // Function will remove catalog items from a consumer wish list
    // if that consumer is associated with the catalog being saved

    // Look to see if this catalog has an associated consumer
    $this_catalog_consumer = get_field(‘dl_catalog_consumer’, $catalogID);

    // If there is a consumer, grab the array of catalog items
    // Populate another array with the wish list items from this consumer
    if($this_catalog_consumer){
    $this_catalog_items = get_field(‘dl_catalog_items’,$catalogID);
    $this_consumer_id = $this_catalog_consumer[0]->ID;

    $field = get_field_object(‘dl_interested_in’, $this_consumer_id);
    $field_key = $field[‘key’];

    $consumer_wish_list_items = get_field(‘dl_interested_in’, $this_catalog_consumer[0]->ID);
    $consumer_wish_list_serial = get_field(‘dl_interested_in’, $this_catalog_consumer[0]->ID, false);

    // Loop through the wish list items
    foreach($consumer_wish_list_items as $wish_list_item):
    $wish_list_item_ID = $wish_list_item->ID;

    // Loop through the catalog items
    // See if there is a match to the wish list item
    foreach($this_catalog_items as $catalog_item):
    $catalog_item_ID = $catalog_item->ID;

    if($wish_list_item_ID == $catalog_item_ID){
    // unset this item from the consumer wish list
    // Find the array index for this catalog_item_id in the wish list array
    $pos = array_search($catalog_item_ID, $consumer_wish_list_serial);

    // Remove that position (key and value) from the array
    unset($consumer_wish_list_serial[$pos]);

    // Now update the Wish List Relationship field value for this consumer
    update_field($field_key, $consumer_wish_list_serial, $this_consumer_id);

    }
    endforeach;
    endforeach;
    }
    return $consumer_wish_list_serial;

    }

    I hope this is helpful. You can check out my app at gardentracker.com. I LOVE ACF!

Viewing 1 post (of 1 total)

The topic ‘Deleting values in one Relationship field based on Values in another’ is closed to new replies.