• Resolved Kir 2012

    (@kir-2012)


    Hi, my apologies if I’ve not been able to find an instance of this having arisen before, I found some similar questions but nothing quite the same that I could use to get to a solution…

    To explain what I’m trying to do with pods just for context, I’m using WCMP plugin (Woocommerce Market Place Multivendor, it runs with Woocommerce), and I’ve added a tab in the vendor dashboard for product vendors to select and display other users of a certain role type on their (CPT) ticket.

    I’ve successfully added the facility for my vendors to choose data for other fields here, which have been created using pods, and displayed them in the ticket, such as to and from dates and image uploads… so I think somewhere along the line I’m perhaps just completely misunderstanding how to use relationships with pods.

    So far, I’ve extended the CPT ‘ticket’ (products, renamed) with a field called whats_onz and made this a relationship field type, relating to users.

    In Relationship Options for that field, selection type is multi select as I’d like a vendor to choose multiple other users to display on their product.

    I’ve chosen list view so that I can display the avatar (guessing that’s what’s meant by icon?)

    In Display Field in Selection List I’ve got {@user_nicename}

    So what I’m trying to achieve, is a multi-select box in the dashboard where users can choose from a list of users, and they will all appear listed on their ticket with an avatar and their username.

    Here’s the wcmp code just for getting the tab content up there, which all works fine as usual, but I need to find what I should be including in here to create that relationship pods multi select form.

    I’ve looked through the documentation and support but I can’t seem to work it out, here’s the code –

    <?php 
    function add_custom_product_data_contents( $pro_class_obj, $product, $post ) {
        //prnt_r($product->get_id());die;
        $hh = get_post_meta($product->get_id() , 'whats_onz' );
        //print_r($hh);
       ?>
    
       
          <div role="tabpanel" class="tab-pane fade" id="see-more"> <!-- just make sure tabpanel id should replace with your added tab target -->
           <div class="row-padding">
    
    //what should I do here to search for users?          
     <div class="form-group">
                   <label class="control-label col-sm-3 col-md-3">More:</label>
                   <div class="col-mzd-6 col-sm-9">
                       <input type="search" name="whats_onz" class="form-control" value= <?php print_r($hh[0]) ?> />
                 
                    
                   </div>
               </div>
           </div>
       </div>
       <?php
    }
    add_action( 'wcmp_product_tabs_content', 'add_custom_product_data_contents', 10, 3 );

    Thanks so much for any help,
    Kind regards

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter Kir 2012

    (@kir-2012)

    Update: What I’m trying to do is add the very same multi select box that’s in the post edit screen for the cpt in admin, but in the above tab instead, so it can be used front end by the vendor as they add their tickets.

    Thread Starter Kir 2012

    (@kir-2012)

    Also about how to display as links in template, I am adding the code to my single product to display, and when I add the following I get the list as it should be, but it isn’t linked:

    <?php
    $terms =  wp_get_post_terms( $post->ID, 'whats_onz', array( 'fields' => 'all' ) );
    $count = count( $terms );
    if ( $count > 0 ) {
    echo '<h3>More Info:</h3>';
    $pod = pods();
    echo $pod->display( 'whats_onz );
    echo'<br><br>';
    }

    How do we change this code to make the output into links please?
    Thanks for your help

    Thread Starter Kir 2012

    (@kir-2012)

    To add that I saw this https://docs.pods.io/displaying-pods/template-tags/each-loop-tag/ but I couldn’t seem to get it working, the each tags always showed up on the page on the front end.
    Thanks

    Plugin Support Paul Clark

    (@pdclark)

    To render a frontend form with display and processing handled for you, see https://docs.pods.io/code/pods/form/

    Otherwise, one has to render a form and process its data manually; doable, but is significantly more complex.

    For getting a link to a term, pass the term objects or IDs from wp_get_post_terms to https://developer.www.ads-software.com/reference/functions/get_term_link/

    Plugin Support Paul Clark

    (@pdclark)

    Regarding the [each] tag, it is not a shortcode on its own. It’s meant for use within the content of a [pods] shortcode or a Pods Template. PHP will be more performant and adaptable to custom needs, especially if you are already in a PHP context, as it does not have additional overhead associated with converting text to code, and offers all programming structures available in PHP, Pods, and WordPress core, while template tags such as [each] will be limited to a subset of those languages due to the nature of convenience methods and processing text templates into pre-defined code with regular expressions.

    Thread Starter Kir 2012

    (@kir-2012)

    Thanks so much for the thorough reply, I’ll go through it and see how I get on ??

    Thread Starter Kir 2012

    (@kir-2012)

    Hi Paul, I got the form done using your reference material, thanks for that!
    All that’s left is to link the display of the info on the front end which I haven’t managed to work out yet. On my dummy ‘add ticket’ form I’ve saved two options, and I’m getting two bullet points on the ticket front end showing, but the problem is they’re blank – can you see what I’m doing wrong here? Here’s the code, the relationship field is whats_onz:

    $terms =  wp_get_post_terms( $post->ID, 'whats_onz', array( 'fields' => 'all' ) );
    $count = count( $terms );
    if ( $count > 0 ) {
    echo '<h3>What's On:</h3>';
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li>';
        echo '<a href="' . esc_url( get_term_link( $term ) ) . '" title="'. esc_attr( sprintf( __( 'View all listings with the category %s', 'my_localization_domain' ), $term->name ) ) . '" alt="'. esc_attr( sprintf( __( 'View all listings with the category %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
        echo '</li>';
    }
    echo '</ul><br>';
    }
    

    Thanks for your help!

    Plugin Support Paul Clark

    (@pdclark)

    $terms = wp_get_post_terms( $post->ID, 'whats_onz', array( 'fields' => 'all' ) );
    $count = count( $terms );
    
    if ( $count > 0 ) {
        echo '<h3>What\'s On:</h3><ul>';
    
        foreach ( $terms as $term ) {
            printf(
                '<li><a href="%s" title="%s" alt="%s">%s</a></li>',
                esc_url( get_term_link( $term ) ),
                esc_attr( sprintf( __( 'View all listings with the category %s', 'use_a_real_domain_or_dont_localize' ), $term->name ) ),
                esc_attr( sprintf( __( 'View all listings with the category %s', 'use_a_real_domain_or_dont_localize' ), $term->name ) ),
                esc_html( $term->name )
            );
        }
    
        echo '</ul><br/>';
    }

    The single-quote being used as an apostrophe in What’s On needs to be escaped, because the string is contained by single quotes. I am surprised it didn’t crash. Using printf() or sprintf() will also be clearer / less prone to error when calculating variables within strings.

    One could also omit the <br/> after the <ul> and instead increase CSS margin-bottom on <ul> within the relevant wrapper, such as .post-content ul { margin-bottom: 15px; }

    Thread Starter Kir 2012

    (@kir-2012)

    Hi Paul, ah sorry, yes it definitely would crash live, I had just copy pasted the whole code from a previous field I’m displaying to add it here, and I changed the title to ‘What’s On’ on autopilot as I re-read it before posting here…. I’d have caught that before I put it live. That’s not the issue though, it doesn’t work for me, I can’t put my finger on it

    Thread Starter Kir 2012

    (@kir-2012)

    Hi again, I think I spoke too soon regarding the form, it seems as though the data isn’t saving as it should be. I’ve noticed that although the form is appearing, after I save the selections disappear.
    I’m wondering if it’s an issue with WCMP woocommerce marketplace multivendor plugin ( https://www.ads-software.com/plugins/dc-woocommerce-multi-vendor/ ) saving the data from the tab I’ve created for their dashboard, or whether it’s to do with the pods code I’ve used in their tab, here’s what I used for adding the pods form to select the users:

       <?php
    $pods = pods( 'product' );
    $params = array( 'fields_only' => true, 'fields' => array('whats_onz') );
    echo $pods->form( $params );?>
                    

    And just in case it is helpful, here is the entire code, including that used to save the post meta to a post:

    /**
    * Add Custom Tab
    
    */
    
    function add_custom_product_data_tabs_3( $tabs ) {
       $tabs['more'] = array(
           'label'    => __( 'More Stuff...', 'more-stuff' ),
           'target'   => 'see-more',
           'class'    => array(),
           'priority' => 100,
       );
       return $tabs;
    }
    add_filter( 'wcmp_product_data_tabs', 'add_custom_product_data_tabs_3' );
    
    
    /**
    * Add Custom Tab Content
    */
    function add_custom_product_data_content_3( $pro_class_obj, $product, $post ) {
    
       ?>
          <div role="tabpanel" class="tab-pane fade" id="see-more"> <!-- just make sure tabpanel id should replace with your added tab target -->
           <div class="row-padding">
               <div class="form-group">
                   <label class="control-label col-sm-3 col-md-3">More Info:</label>
                   <div class="col-md-6 col-sm-9">
               
      <?php
     //the pods form
    $pods = pods( 'product' );
    $params = array( 'fields_only' => true, 'fields' => array('whats_onz') );
    echo $pods->form( $params );?>
                    
                   </div>
               </div>
           </div>
       </div>
       <?php
    }
    add_action( 'wcmp_product_tabs_content', 'add_custom_product_data_content_3', 10, 3 );
    
    
    /**
    * Save post data from custom tab 
    */
    
    function save_custom_product_data_3( $product, $post_data ) {
       if( isset($post_data['post_ID']) && isset($post_data['whats_onz'])){
           update_post_meta( absint( $post_data['post_ID'] ), 'whats_onz', $post_data['whats_onz']);
       }
    }
    add_action( 'wcmp_process_product_object', 'save_custom_product_data_3', 10, 2 );
    

    Does anything stick out that I’ve done wrong with the pods form that it might not save?

    Kind thanks

    Plugin Support Paul Clark

    (@pdclark)

    $pods = pods( 'product' ); is not passing an ID as the second argument. Therefore, the form will create a new content item. If one wants the form to edit content, pass an ID of which one to edit. If one wants to create new the first time, but then edit once one exists, query what has been published by the current user. If something has been published, pass the most recent ID. If something has not been published, pass null.

    Plugin Support Paul Clark

    (@pdclark)

    Also check in the admin whether the submitted content is saving or not. If the form is working, it will show a success dialogue in the front and content will appear in the back. If neither happens, it is an indication that the tabs from the other plugin may be loading in JavaScript, blocking pods->form() from loading its usual scripts.

    wcmp_process_product_object

    …should not be necessary unless you are using a different form action. Pods->form() outputs JS for submitting data and handles saving the data on its own, as long as it’s loaded in HTML, not JS.

    If the tabs plugin is JS-powered, it may be necessary to write a custom form, or experiment with such trickery as putting an iframe in the tab which points to a simple WP template containing the Pods form. The latter approach would cause the form to be isolated from any dynamic context the tabs might introduce which could break functionality.

    Thread Starter Kir 2012

    (@kir-2012)

    Hi, I’m making some progress, but can I check the default behaviour of a multi select form front end when you use pods form set up? Should it show the existing terms?
    Because when I check in admin, the data is saving, and it overwrites fine each time you save, but the saved items do not show up in the form to indicate what’s there, would expect the multi select to open prepopulated with the items already selected. I think that’s the bit I’m finding hard to sort. Can you show me a code example that might help me?
    Kind thanks

    Thread Starter Kir 2012

    (@kir-2012)

    Hi, I apologise in advance, but unfortunately I think I’m making more of a mess the longer I look at this, because I’m coming up with some strange behaviours!

    I’m thinking I should try to explain from the start, and then you might spot something I’ve missed or that I’m doing wrong ??

    I will also show my settings in the product field in pods admin, in case I’ve done something obvious incorrectly!

    So here goes: the aim is to make it possible for user role ‘vendor’ to choose in a form, from other users, and select them to appear on the product page.

    I have my CPT ‘product’ (thanks to woocommerce) and then I’ve extended this in pods admin with some fields, i’ve got this working fine for date fields, and an image field, but not for this relationship field.

    My relationship field is called whats_onz and I am trying to use it to relate products to users. Using this field, the idea is that my front end user can enter their front end ‘edit product’ form, and I will have added to that form, the facility to select from a multi-select form, some users to display on their product page. I am trying to achieve this with pods’ own forms.

    So I’ve got two jobs –
    Job 1 – To add a form to my edit product page via which the vendor can choose from any user to display (I know I can limit the roles in the field GUI in pods admin). The form should save selections from Add New and future Edits, and display those as selected options in the multi select field

    Job 2 – To display selections on the front end of the product single page as links to the chosen author pages with the name and avatar.

    Job 1, adding the form…. I’ve done this with the following code (below) and the form appears exactly as I would expect, as a multi select box, and the user roles I want vendors to choose from can be selected, by their user_nicename as I have set in product pods admin for the field. However, there is a loading spinner that starts as soon as the page renders, before you’ve touched into the multi select to select something. The spinner appears and just stays on. You can select your users, and they appear in the form with the little cross next to them as chosen options in the multi select, but then after saving, it’s all gone again front end, the multi select form is blank again. Although not back end, the changes always appear fine there. I thought of a workaround to just list the existing chosen options below the form if I couldn’t get the form to open in ‘edit’ as populated, but I realise that would of course be a bit clumsy, I couldn’t get it working anyway (see below).

    Also, after saving, an error message appears at the bottom of the page to say “Content, title, and excerpt are empty.”

    
              <?php
    
    $pod = pods( 'product', $id );
    
    
    $fields = array( 'whats_onz');
    
    echo $pod->form( $fields );
    
        ?>

    I wonder if I have something wrong in my settings because even a basic code to display just linked avatars (Job 2 above) isn’t working, as follows: (The result I get for this code on the page is just two bullet points).

    $terms = wp_get_post_terms( $post->ID, 'whats_onz', array( 'fields' => 'all' ) );
    $count = count( $terms );
    
    if ( $count > 0 ) {
        echo '<h3>What\'s On:</h3><ul>';
    
        foreach ( $terms as $term ) {
            printf(
                '<li><a href="%s" title="%s" alt="%s">%s</a></li>',
                esc_url( get_term_link( $term ) ),
                esc_attr( sprintf( __( 'View all listings with the category %s', 'use_a_real_domain_or_dont_localize' ), $term->name ) ),
                esc_attr( sprintf( __( 'View all listings with the category %s', 'use_a_real_domain_or_dont_localize' ), $term->name ) ),
                esc_html( $term->name )
            );
        }
    
        echo '</ul><br/>';
    }


    In my settings for the whats_onz field in product pods admin I’ve got:

    FIELD DETAILS
    Name = whats_onz
    Field Type = relationship
    Related Type = users
    Bi Directional field = whats_on1 [pod: user]

    RELATIONSHIP OPTIONS
    Selection Type = Multiple Select
    Input Type = Auto Complete
    Display Format = Item 1, Item 2 and Item 3
    Allow Add New = Yes
    Taggable = Yes
    Show Edit Links = Yes
    Show View Links = Yes
    Selection Limits = 0
    Display Field In Selection List = {@user_nicename}
    Limit List By Roles = Yes I’ve selected some roles
    The rest is empty …

    ADVANCED
    All left empty

    CONDITIONAL LOGIC
    All left empty

    REST API
    All left empty

    Thanks

    Thread Starter Kir 2012

    (@kir-2012)

    Update! I’ve cracked ‘job 2’, which was the display of a vendor’s selections of other users as links with avatars on the single product page, using templates, turns out I was completely misunderstanding how it worked. Apologies!

    I was able to create a new pods template, add the code into my template and then select that template using the auto templating tab in the pods admin for product, so that’s done!

    Now I just need to firm up the code for making my form appear, in order to choose the users, and then save each time a product is created or edited, so that they can appear in my template!

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Searching CPT Tickets related field in form front end’ is closed to new replies.