Viewing 5 reply threads
  • Author
    Posts
    • #1588

      I’m having a hard time with this one.
      I have a Custom Post Type Product with the following metabox:

      piklist('field', array(
          'type' => 'group',
          'field' => 'pricing_tier_group',
          'label' => __('Pricing Tier'),
          'template' => 'field',
          'add_more' => true,
          'fields' => array(
              array(
                  'type' => 'text',
                  'field' => 'tier_name',
                  'label' => 'Tier Name',
                  'columns' => 8,
              ),
              array(
                  'type' => 'text',
                  'field' => 'tier_price',
                  'label' => __('Price'),
                  'value' => '$',
                  'columns' => 4,
              ),
              array(
                  'type' => 'text',
                  'field' => 'example_url',
                  'label' => __('Example URL'),
                  'columns' => 12,
              ),
              array(
                  'type' => 'group',
                  'field' => 'tier_features',
                  'add_more' => true,
                  'fields' => array(
                      array(
                          'type' => 'text',
                          'field' => 'feature_name',
                          'label' => __('Feature name'),
                          'columns' => 12
                      ),
                  ),
              ),
              
          )
          
      ));

      I would like to display it in the following template:

      <footer class="product-footer product-tiers">
              <div class="grid">
                  <div class="grid__item one-whole desk-one-half">
                      <div class="product-tier first">
                          <div class="grid__item one-half">
                              <h3 class="product-tier-name"> TIER NAME </h3>
                              <span class="tier-price"> TIER PRICE </span>
                              <a href= EXAMPLE URL> View Example </a>
                          </div><!--
                          --><div class="grid__item one-half">
                              <ul class="check-list">
                                  <li> FEATURE 1 </li>
                                  <li> FEATURE 2 </li>
                                  <li> FEATURE 3 </li>
                              </ul>
                          </div>
                      </div> <!-- end product-tier -->
                  </div><!--
              -->
              </div> <!-- end grid row -->
          </footer>

      This represents a template part to the main product display template which has the WordPress Loop
      with the Product Title (the_title()) and Product Description (the_content()).

      I having trouble accessing the values especially of the tier_features nested add_more group. The documentation only provides help with simple single field add_more display. When I tried using this code nothing shows.

      So to be clear, the Front end should should display the following:
      Products

      Product One
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
      Basic (Tier 1)——–Feature 1 ————— Pro(Tier 2)———Feature 1
      $0000.000————-Feature 2 ————— $0000.000———–Feature 2
      View Example———-Feature 3 ————— View Example——–Feature 3

      Product Two

      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
      Basic (Tier 1)——–Feature 1 ————— Pro(Tier 2)———Feature 1
      $0000.000————-Feature 2 ————— $0000.000———–Feature 2
      View Example———-Feature 3 ————— View Example——–Feature 3


      Product Three

      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
      Basic (Tier 1)——–Feature 1 ————— Pro (Tier 2)———Feature 1
      $0000.000————-Feature 2 ————— $0000.000———–Feature 2
      View Example———-Feature 3 ————— View Example——–Feature 3

      etc.
      Thanks for any help.

    • #1597
      Steve
      Keymaster

      Have you read the docs on the Add-more field and how to build a template part.

    • #1599

      Yes I did and I tried that as mentioned in my post above. I haven’t gotten it to work.
      One of the latest attempts is presented below (This is in a shortcode funtion):

      $loop = new WP_Query(
      		    array(
      		        'post_type' => 'cjwd_product',
      		        'orderby' => 'title'
      		    )
      		);
      		
      		if ( $loop->have_posts() ) {
      		    $output = '<div class="products">';
      		    
      		    while( $loop->have_posts() ) {
      		        $loop->the_post();
      		        
      		        $meta = get_post_meta( get_the_id(), 'pricing_tier_group', true );
      		       //var_dump($meta);
      		    }
      		    
      		   $output .= '
      		       <article class="product">
                          <header class="product-header">
                              <div class="grid">
                                  <div class="grid__item one-whole desk-one-half">
                                      <h2 class="product-name"> ' . get_the_title() . '</h2>
                                      <p class="product-description"> ' . get_the_content() . '</p>
                                  </div>
                              </div>
                          </header> ' .
      
                             piklist("content-product", array( "data" => $meta, "loop" => "data" ) )
                      . '</article>';
      		}
      		
      		 return $output;

      And the template part for testing (template name: content-product.php):
      <li><?php echo $data['pricing_tier_group']; ?></li>

    • #1606
      Marcus
      Member

      I haven’t used ADD-MORE’s within a grouped field structure yet. (what I mean by that, is I create simple groups, where the group has no field name, and all the fields within are then just arrays)

      So how I do it is let’s say I have the following:

      piklist('field', array(
      	'type' => 'group'
      	,'add_more' => true
      	,'label' => 'Week Bookings'
      	,'description' => 'Here you can book each week individually, including days off'
      	,'fields' => array (
      		array (
      			'type' => 'select'
      			,'field' => 'mae_sched_choose_week'
      			,'label' => 'Week'
      			,'columns' => 6
      			, 'choices' => piklist(
      				$wpdb->get_results("SELECT 
      				w.ID AS weekID, CONCAT('Week ',w.menu_order, ' (',REPLACE(wc1.meta_value,', ".$weekyear."',''),' - ',wc2.meta_value,')') AS weekTitle 
      				FROM {$wpdb->prefix}posts w
      				LEFT JOIN {$wpdb->prefix}posts wp ON wp.post_title='".$weekyear."' AND wp.post_type=w.post_type
      				LEFT JOIN {$wpdb->prefix}postmeta wc1 ON w.ID = wc1.post_id AND wc1.meta_key='mae_wks_start_week'
      				LEFT JOIN {$wpdb->prefix}postmeta wc2 ON w.ID = wc2.post_id AND wc2.meta_key='mae_wks_end_week'
      				WHERE w.post_parent = wp.ID AND w.post_type='weeks'
      				ORDER BY w.menu_order"), array("weekID","weekTitle")
      			)
      			,'on_post_status' => array(
      			  'value' => 'publish'
      			)
      		)
      		,array (
      			'type' => 'text'
      			,'field' => 'mae_sched_days_on'
      			,'label' => 'Days of Camp'
      			,'columns' => 6
      			,'value' => 'mon,tue,wed,thu,fri'
      			,'on_post_status' => array(
      			  'value' => 'publish'
      			)
      		)
      	)
      ));
      

      Notice there is no field name on the group level.
      But there is on the child levels.

      So I can retrieve the information quite simply by:

      get_post_meta($postid, 'mae_sched_choose_week', false);
      get_post_meta($postid, 'mae_sched_days_on', false);

      This returns an array of the first and second fields within my add more.

      You should be able to do the same with yours, provided its in post meta.
      You would grab the parent group field, which would contain all the arrays for the add_mores below it in just one get.
      get_post_meta($postid, 'pricing_tier_group', false);

      Would get all the arrays associated with that group.

      Hope this helps.

      Marcus

    • #1608
      Steve
      Keymaster

      This is much, much easier than you think.

      FIRST, LET’S LOOK AT THE ARRAY OUTPUT:
      Each “add-more” array level is going to need it’s own template file. So, you will have a main one, and then another for tier_features.

      Array
      (
          [tier_name] => Array
              (
                  [0] => Tier Name 1
                  [1] => Tier Name 2
              )
      
          [tier_price] => Array
              (
                  [0] => $1
                  [1] => $2
              )
      
          [example_url] => Array
              (
                  [0] => www.1.com
                  [1] => www.2.com
              )
      
          [tier_features] => Array
              (
                  [0] => Array
                      (
                          [feature_name] => Array
                              (
                                  [0] => feature 1-1
                                  [1] => feature 1-2
                              )
      
                      )
      
                  [1] => Array
                      (
                          [feature_name] => Array
                              (
                                  [0] => feature 2-1
                                  [1] => feature 2-2
                              )
      
                      )
      
              )
      
      )

      YOU DON’T NEED TO WRITE YOUR OWN LOOP, BECAUSE PIKLIST WILL LOOP THROUGH THE ARRAY.
      In the existing WordPress loop in your theme, add this code. It would probably go where the_content() is being displayed.

      // Get the post meta information
      $pricing = get_post_meta($post->ID, 'pricing_tier_group', true);
      
      // 1. Display the data using the file pricing_tier_group_template.php.
      // 2. Assign your $pricing parameter (from above) to data.
      // 3. Loop through data.
      piklist('pricing_tier_group_template', array('data' => $pricing, 'loop' => 'data'));

      YOUR FIRST PAGE TEMPLATE: pricing_tier_group_template.php
      -Display data from the first level of your add-more array: (i.e. echo $data['tier_name'])
      -To display the second level of your add-more array, you need to create another page template.
      -The second level is showing the list items, so the template should be called in the ul.
      1. Display the data using the file pricing_tier_feature_template.php.
      2. Assign your $data[‘tier_features’] parameter.
      3. Loop through data.
      piklist('pricing_tier_feature_template', array('data' => $data['tier_features'], 'loop' => 'data'));

      <footer class="product-footer product-tiers">
              <div class="grid">
                  <div class="grid__item one-whole desk-one-half">
                      <div class="product-tier first">
                          <div class="grid__item one-half">
                              <h3 class="product-tier-name"><?php echo $data['tier_name']; ?></h3>
                              <span class="tier-price"><?php echo $data['tier_price']; ?></span>
                              <a href= EXAMPLE URL><?php echo $data['example_url']; ?></a>
                          </div><!--
                          --><div class="grid__item one-half">
                              <ul class="check-list">
                                  <?php piklist('pricing_tier_feature_template', array('data' => $data['tier_features'], 'loop' => 'data')); ?>
                              </ul>
                          </div>
                      </div> <!-- end product-tier -->
                  </div><!--
              -->
              </div> <!-- end grid row -->
          </footer>

      THE SECOND PAGE TEMPLATE, pricing_tier_feature_template.php IS JUST ONE LINE:

      <li><?php echo $data['feature_name']; ?></li>

      Let me know if this helps.

      I will update the docs to better reflect multiple levels of add-mores.

      • #1611

        Thanks a million Steve for clearing that up.
        Yea the docs need to be updated with this example because I would’ve never thought to call piklist’s get template part for the features from within main template.

        You should also mention in the docs that when doing it with a plugin, the main template file needs to go in the parts folder of the plugin folder structure and initial call to the main template file can be in your theme template files.

        Thanks again so much.

    • #1610
      Marcus
      Member

      And this is why I love it when Steve or Kevin post. LOL

      Marcus

Viewing 5 reply threads
  • You must be logged in to reply to this topic.