0

I've got WordPress site for which I've set up a custom post_type along with some custom fields created with CMB2 to display extra stuff within my custom posts.

When editing a single item for my custom post_type, it has a repeatable fields group set up in order to store prices and additional info, those are the fields:

price_tag, product_title, promo_desc, promo_price, check_promo // All fields exist within the CMB2 repeatable group, what increases the level of difficult for a new comer to php development like me...

What I need is a correct logic to output the promo_price field only if the checkbox field check_promo value = yes, if = no, then does not display/echo the promo_price field's value, then continue the logic to output the promo_price field's value only if is a specific day of the week, in this case, I thought that perhaps the WordPress Codex for Formatting Date and Time might help...

This is how I'm trying to retrieve and display them:

$foodPrices = get_post_meta( $post->ID, '_prefix_food_price_group', true ); // repeatable fields group
// A checkbox "Show promo prices?" with values no, yes exists within the metabox _prefix_food_price_group
$promoDay = date('l'); // returns Sunday

<?php       
 foreach( (array) $foodPrices as $key => $value ){
     if(!( isset( $value['price_tag'] ) ))
        continue;   

        echo '<div class="col-xs-12 col-md-4">';

        echo '<div id="price-box" class="panel">';
        echo '<div class="panel-heading text-center">';

        echo '<div id="prices-current">';

        echo ' '. $value['price_tag'] .' ';


     // if is Sunday
     if ( $promoDay == 'Sunday') {
     // display promo price if checkbox value =yes
      if( in_array( 'yes', $foodPrices ) ) {
        echo ' '. $value['promo_price'] .' ';   
      }          
     }
        echo '</div>';
        echo '<p>Serve de 1 a 2 pessoas</p>';
        echo '</div>';
        echo '<div class="panel-footer">';
     if(!( isset( $value['price_title'] ) ))
        continue;
        echo '<div id="pricetitle" class="col-xs-6 text-center">' . $value['price_title'] . '</div>';

     if(!( isset( $value['price_desc'] ) ))
        continue;
        echo '<div id="pricedesc" class="col-xs-6 text-center">' . $value['price_desc'] . '</div>';

       echo '<div class="clearfix">';
       echo '</div>';
       echo '</div>';         
       echo '</div>';
       echo '</div>';
 }
?> 

So far everything works as intended, I can successfully display the fields on my theme template. However, it seems I'm not looping correctly through the repeatable fields and/or setting the right logic in order to achieve this.

Any help is very much appreciated.

5
  • Here's a screenshot of what I've so far achieved: cloudup.com/cGEb7xLazBi Sep 13, 2015 at 1:56
  • where do you set $PromoPrice?
    – Milo
    Sep 13, 2015 at 2:02
  • Hey Milo. I'll edit it. $PromoPrice is for an old single custom field. My set up now uses a repeatable fields group where the fields lies within $foodPrices. Txt for pointing out. Sep 13, 2015 at 2:06
  • CMB related questions should be asked at the appropriate support forum. Sep 13, 2015 at 6:13
  • I don't think it is a question specific to CMB plugin. How to get the day of the week in WordPress needs specific WordPress functions in order to respect WordPress datetime zone configuration.
    – cybmeta
    Sep 17, 2015 at 8:40

1 Answer 1

1

You are not checking the current date correctly. You need to use current_time() funciton to get current datetime according to WordPress configuration. Then, using PHP date() you can check what day of week current date is:

// Get current date time of WordPress in Unix timestamp format
$timestamp = current_time( 'timestamp' );

// Get day of the week of current date
// See http://php.net/manual/es/function.date.php
$dw = date( "w", $timestamp);

// Check day of the week: 0 for Sunday to 6 for Saturday
if( $dw == 0 ) {

    // Display content if day of the week is Sunday

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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