0

I have the code below (not written by myself) which has a li data-value populated - however this is not showing next to the date on the product page, the date is showing correctly but the location does not appear when you select the dropdown.

This is a quite a bit above my knowledge and there is a lot of code in the functions php file which I do not understand, so I was wondering if there was anything obvious?

An example page using this code is here

<div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" id="datebox" class="form-control">
    <input type="hidden" id="locationbox" class="form-control">
    <div class="input-group-btn open">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
                        <li data-value="Norwich, Norfolk" style="
    width: 100%;
"><a href="#">Sunday, 09 January 2022</a></li>
                        <li data-value="Leicester"><a href="#">Friday, 29 October 2021</a></li>
                    </ul>
    </div>
    </div>
    </div>

The code from the functions php is below. I can see where this is being generated but I do not know how to portray the location on to the page so it is visible.

function custom_meta_option() {

    $_date1 = get_post_meta(get_the_ID(),'_date1', true);
    $_date2 = get_post_meta(get_the_ID(),'_date2', true);
    $_date3 = get_post_meta(get_the_ID(),'_date3', true);
    
    $_location1 = get_post_meta(get_the_ID(),'_location1', true);
    $_location2 = get_post_meta(get_the_ID(),'_location2', true);
    $_location3 = get_post_meta(get_the_ID(),'_location3', true);
    
    $product_id = get_the_ID();
    $status = get_post_meta( $product_id, '_stock_status', true );
    
    /*$terms = get_the_terms ( $product_id, 'product_cat' );
    foreach ( $terms as $term ) {
         echo $cat_id = $term->term_id;
    }*/

    if($status == 'instock' && !empty($_date1) || !empty($_date2) || !empty($_date3))
    {
    ?>    
    <div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" ID="datebox" Class="form-control"></input>
    <input type="hidden" ID="locationbox" Class="form-control"></input>
    <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
            <?php if($_date1): ?>
            <li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_date1; ?></a></li>
            <?php endif; if($_date2):?>
            <li data-value="<?php echo $_location2; ?>"><a href="#"><?php echo $_date2; ?></a></li>
            <?php endif; if($_date3):?>
            <li data-value="<?php echo $_location3; ?>"><a href="#"><?php echo $_date3; ?></a></li>
            <?php endif; ?>
        </ul>
    </div>
    </div>
    </div>
    <?php
    }
    //echo '<p>No dates currently available</p>';
}
0

the data-value doesnt show, is to use. if you want to show it, it should be inside the a tag, once it is php code with variables must be inside the php "tags"

so should be something like this

<li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_date1.' '.$_location1; ?></a></li>

this will show this "Friday, 29 October 2021 Leicester" if you want first the location can be done like this

<li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_location1.' - '.$_date1; ?></a></li>

this will show this "Leicester - Friday, 29 October 2021" I had put a ' - ' because seems better. However its a choise.

the inteire code with the last example should be this one


    $_date1 = get_post_meta(get_the_ID(),'_date1', true);
    $_date2 = get_post_meta(get_the_ID(),'_date2', true);
    $_date3 = get_post_meta(get_the_ID(),'_date3', true);
    
    $_location1 = get_post_meta(get_the_ID(),'_location1', true);
    $_location2 = get_post_meta(get_the_ID(),'_location2', true);
    $_location3 = get_post_meta(get_the_ID(),'_location3', true);
    
    $product_id = get_the_ID();
    $status = get_post_meta( $product_id, '_stock_status', true );
    
    /*$terms = get_the_terms ( $product_id, 'product_cat' );
    foreach ( $terms as $term ) {
         echo $cat_id = $term->term_id;
    }*/

    if($status == 'instock' && !empty($_date1) || !empty($_date2) || !empty($_date3))
    {
    ?>    
    <div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" ID="datebox" Class="form-control"></input>
    <input type="hidden" ID="locationbox" Class="form-control"></input>
    <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
            <?php if($_date1): ?>
            <li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_location1.' - '.$_date1; ?></a></li>
            <?php endif; if($_date2):?>
            <li data-value="<?php echo $_location2; ?>"><a href="#"><?php echo $_location2.' - '.$_date2; ?></a></li>
            <?php endif; if($_date3):?>
            <li data-value="<?php echo $_location3; ?>"><a href="#"><?php echo $_location3.' - '.$_date3; ?></a></li>
            <?php endif; ?>
        </ul>
    </div>
    </div>
    </div>
    <?php
    }
    //echo '<p>No dates currently available</p>';
}
1
  • Brilliant - so simple yet so difficult when you don't know how. Thank you.
    – Dtorr1981
    Oct 14 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.