• Resolved hefterbrumi

    (@hefterbrumi)


    Hi!

    I am trying to display relational data on a page, however I have some flaws.

    First I used pods templates:

    [if complementary_products]
    <span>Pieces of collection</span>
    <ul class="products column-2">
    [each complementary_products]
    <li>
    <a href="{@permalink,esc_url}">
    <span><img src={@post_thumbnail_url,esc_url}></span>
    <span>{@post_title,esc_attr}</span>
    <span>{@artist}</span>
    <span>{@_price}</span>
    </a>
    <div class="et_pb_button_module_wrapper">
    <a href="add-to-cart={@id,esc_attr}" data-quantity="1" class="et_pb_button button add_to_cart_button ajax_add_to_cart" data-product_id="{@id,esc_attr}">Add to basket</a>
    </div></li>	
    [/each]
    </ul>
    [/if]

    This works fine, except the price value, that displays as a raw number. For this I though to replace {@_price} to: <?php wc_price(pods_field (_price))?> however I realized php is deprecated in pods templates. Therefore I created a shortcode to use as follows:

    function pieces_of_collection () {
      $pod = pods( 'product', get_the_id() );
      $related = $pod->field( 'complementary_products' );
      if ( ! empty( $related ) ) {
          $wrap_start = ('<span>Pieces of the collection</span><ul class="products column-2">');
          foreach ( $related as $rel ) {
            $id = $rel[ 'ID' ];
            $permalink = get_permalink( $id );
            $img_url = pods_field_display ('post_thumbnail_url', $id);
            $title = get_the_title( $id, 'post_title', true );
            $artist_name = pods_field_display ('artist', $id);
            $price = get_post_meta( $id, '_price', true );
            $price_formatted = wc_price($price);
    
            $pieces_of_collection .= ('<li><a href="'.esc_url($permalink).'"><span><img src='.$img_url.'></span><span>'.$title.'</span>
            <span>'.$artist_name.'</span><span class=><span>'.$price_formatted.'</span></span></a><div><a href="add-to-cart='.$id.'" data-quantity="1" class="et_pb_button button add_to_cart_button ajax_add_to_cart" data-product_id="'.$id.'">add to basket</a>
            </div></li>');
          }
          $wrap_end = ('</ul>');
        return $wrap_start . $pieces_of_collection . $wrap_end;
      }
    }
    add_shortcode( 'func_pieces_of_collection', 'pieces_of_collection' );

    Now the price shows great however, pods_field_display function does not work in the loop. It repeats itself. Get_post_meta function works great but it returns unformatted raw data. Can I get some help with this?

    • This topic was modified 1 year, 11 months ago by hefterbrumi.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Paul Clark

    (@pdclark)

    You can run a magic tag through a custom formatting function just as you’ve done with esc_attr() —?by adding a comma between the field name and the function name.

    So, for example, defining a custom function, format_price() in a plugin or your theme’s functions.php:

    <?php
    
    function format_price( $price ) {
    	return number_format( $price, 2 );
    }

    Then it can be applied to {_@price} by writing {@_price,format_price}

    Thread Starter hefterbrumi

    (@hefterbrumi)

    Wow, that’s awsome, I didn’t know that. Thanks Paul, I will give it a try.

    Thread Starter hefterbrumi

    (@hefterbrumi)

    Followup:

    With woocommerce {@_price} gives an array with one value therefore the code returns 1.

    I am not sure if this is how it should be but it works:

    function format_price( $price ) {
    	return wc_price( $price[0] );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Related field raw data problem’ is closed to new replies.