• Resolved richstrauss

    (@richstrauss)


    This should be so simple. But I’ve searched, I’ve tried… Now am bleary-eyed…

    I’ve extended the Woocommerce ‘Product’ post type using Pods Framework, and named my pod ‘Product’.

    I have (among others) two file upload fields, for PDF files:

    Product Label = product_label
    Scientific Support = scientific_support

    I need to display them in a Woocommerce PHP file.

    And output it like this if the file is uploaded:

    <a href="{@product_label}"><img src="#"></a>
    <a href="{@scientific_support}"><img src="#"></a>

    But with PHP…

    And if nothing is uploaded, then show nothing.

    Thanks in advance!

    https://www.ads-software.com/plugins/pods/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    The lingo above you’re using is a Pods Template. You can use a Pods Template, with the Auto Template functionality, just select the location as ‘After’ to display your Pods Content after the ‘post-content’ for the Product. If you’re in the Template Editor, you can also use the reference in the lower right for picking the img src selections. Take a look at the following PodsCast for pointers:

    https://pods.io/2015/05/03/podscast-005-templates-shortcodes-widgets-displaying-data-in-pods-pt-3/

    If you want to put them in a WooCommerce PHP template, you’ll need to know PHP and be able to edit those particular templates. WooCommerce uses Hooks & Filters, and that’s the preferred method for incorporating items into their templates (because they change their code so often). You can get with WooCommerce support about which fields or hooks you’d need to modify to include some additional meta field data in your Product display. You’d be outputting the fields using get_post_meta() against the current post:

    https://developer.www.ads-software.com/reference/functions/get_post_meta/

    ie, get_post_meta($post->ID, 'product_label');

    Thread Starter richstrauss

    (@richstrauss)

    Hi Jim,

    Thanks for the reply!

    Actually, I have already created a Pod Template and selected ‘After’.

    Here is a screenshot of the Pod Template:

    https://www.screencast.com/t/K1hrRREp

    Here is a screenshot of the product on the front-end:

    https://www.screencast.com/t/dvaXxQZEIWv

    You can see I indicated where I want to insert image download links in the WooCommerce product-image.php template, below the actual image.

    Here are the two fields in my Pod:

    https://www.screencast.com/t/dHtvkqMraEc

    1. Product Label
    2. Scientific Support

    These two are PDF uploads for which I want to display a image download button each, on condition that a file is uploaded:

    <a href="/value_of_pod_field.pdf"><img src ="/image.jpg"></a>

    If a file has not been uploaded for either field, then don’t display that button.

    I am dangerous when it comes to PHP ??

    Found some code snippets here and there, and tried but nothing worked.

    <?php if(!empty(($scientificsupport = get_post_meta($post->ID, 'scientific_support', true)))) {?>
    Scientific Support: <?php echo preg_replace('|https?://|', '', $scientificsupport ); ?>
    <?php $site = preg_replace('{/$}', '', $scientificsupport);} ?>
    <?php $label = get_post_meta( get_the_ID(), 'product_label', true ); ?>
    <a href="<?php echo $label; ?>">Linked Label Button Here</a>
    <?php $science = pods('scientific_support', $id); ?>
    <a href="<?php echo $science->field( 'scientific_support' ); ?>">Linked Science Button Here</a>
    <?php $science = get_post_meta($post->ID, 'scientific_support', true);
    if($science != '') {
    echo  $science;
     } ?>

    Am a bit confused…

    So I’m hoping you can help me with the right code, please?

    Thanks in advance!

    Plugin Contributor Jim True

    (@jimtrue)

    Looking at your screens casts, you want the product images downloads to show up in a spot that is outside of where your Pods Template is displaying at present. From the screenshot, it looks like you have the Pods Template displaying in tabs the way you intended, correct?

    You should but your download spec buttons WITHIN the template and then you can handle them on the first tab page. If you want them outside of the Pods Template, integrated with the WooCommerce product display template, you’ll need to get with the WooCommerce folks to find out what file to edit or what hook to edit in order to get your ‘buttons’ displaying where you’d like and then you’d also need to write the code to display those buttons.

    We, unfortunately, don’t have the time or resources to write your custom code for you as we’re a free software with free support. I’d put your buttons in the Pods Template, since you’ve already got that part figured out. Otherwise, reach out to the WooCommerce to at least get the right code location for what you’re looking for and then come back with a more specific question. Otherwise, we’re also just shooting in the dark.

    Thread Starter richstrauss

    (@richstrauss)

    Hi Jim,

    The reason I want to put those buttons where I want to put them, is because of an aesthetic consideration, as I want to fill that white space.

    I don’t think it’s got anything to do with Woocommerce, I know exactly in which file it needs to go, and where.

    The code should go in the Woocommerce product-image.php file right at the bottom before the closing </div> tag.

    I’ve already put that file in the ‘woocommerce’ folder inside of my child theme folder.

    All I need to know is:

    1. How to call the field with an image tag attached to it (in PHP)
    2. How to call it conditionally based on whether a file is uploaded or not (in PHP)

    Is that still too vague?

    I want to use Pods for other client projects too, and I am willing and eager to learn and really want to.

    I understand that it’s a free plugin, and I understand that your resources are limited.

    But I have a hunch that what I’m looking for is Pods bare bones basics, and doesn’t need a ton of custom coding.

    Forgive me if I’m wrong.

    Rich

    Plugin Contributor Jim True

    (@jimtrue)

    Rich,

    It does actually require custom coding (all scripting specific for someone is custom coding) but I can point you in the right directions to learn what you’re trying to do, which is what I’ve been attempting to do in my replies above.

    You need to test your fields to determine if they ‘exist’ or are ‘set’ and then you need to grab the id of the image field related to those two fields so you can get the URL from the Media Library (file/media and field attachments are related by ID to the Media Library).

    You’re already on the ‘post’ page of the Product (and that’s what you’ve extended), so you need to grab the field information into your script with get_post_meta (as noted above 2 replies ago).

    You then check the values of variable return with either isset() or empty(), check this article for examples:
    https://zainal.wordpress.com/2006/04/25/8/

    To get the URL of the image, you can pass the array above to wp_get_attachment_url (WordPress function) or to pods_image_url (Pods function):

    https://codex.www.ads-software.com/Function_Reference/wp_get_attachment_url
    https://pods.io/docs/code/field-functions/pods-image-url/

    Thread Starter richstrauss

    (@richstrauss)

    Hi Jim, I didn’t get the email notification of your last post (guess I didn’t check the box last time), so apologies for the delay!

    I actually came back here to say I’ve just purchased Caldera Easy Pods, and have a feeling that’s going to pull me through so to speak ?? Am digging into the videos and docs and feeling around…

    It looks awesome, such amazing tools. Am just starting to setup Pods on a bigger and quite exciting project, so will report back here once I get back to this particular issue, and hopefully (probably) with the PHP code in question ??

    Thank you so much for your patience and for the great work!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Conditional display of fields using PHP’ is closed to new replies.