0

I've used this check meta box in my functions.php but I don't understand how I can get the meta data. I'm trying to output if a product is still available or not.

Metabox setup:

/* Array of the meta box options. */
$meta_boxes = array(
    'affiliate-url' => array( 'name' => 'Affiliate_url', 'title' => __('Affiliate URL (generated)', 'hybrid'), 'type' => 'text' ),  
    'original-url' => array( 'name' => 'Original_url', 'title' => __('Original URL', 'hybrid'), 'type' => 'text' ),         
    'product-id' => array( 'name' => 'Product_ID', 'title' => __('Product ID', 'hybrid'), 'type' => 'text' ),           
    'normal-price' => array( 'name' => 'Normal_price', 'title' => __('Normal Price', 'hybrid'), 'type' => 'text' ),     
    'discount-price' => array( 'name' => 'Discount_price', 'title' => __('Discount Price', 'hybrid'), 'type' => 'text' ),
    'sizes-available' => array(
        'name' => 'Sizes_available',
        'title' => __('Currently available:', 'hybrid'),
        'type' => 'check',
        'options' => array(
            'XS' => 'Extra Small',
            'S' => 'Small',
            'M' => 'Medium',
            'L' => 'Large',
            'XL' => 'Extra Large',
            'XXL' => 'Extra Extra Large',                                               
),

Template setup:

                            <div class="col-md-6 col-xs-6 product-info">
                                <h5>AVAILABLE IN</h5>
                                <ul>
                                        <li>Small</li>
                                        <li><strike>Medium</strike> <small>(sold out)</small></li>
                                        <li>Large</li>
                                        <li>Xtra Large</li>
                                        <li>Xtra Xtra large</li>    
                                </ul>                                                                                                                   
                            </div>

I need to output the list and place <strike>$size</strike> <small> (sold out)</small>.

Normaly I use <?php if ( get_post_meta($post->ID, 'Sizes_available', true) ) : ?> to check if something is filled in the meta box but this doesn't apply for a 'checked' output.

Can anybody help?

1 Answer 1

1

This will be made in the side of the template. First get all the sizes into an array:

$allSizes = array(

    'XS' => 'Extra Small',
    'S' => 'Small',
    'M' => 'Medium',
    'L' => 'Large',
    'XL' => 'Extra Large',
    'XXL' => 'Extra Extra Large',

);

echo '<ul>';
$outStock = get_post_meta($post->ID, 'Sizes_available', true);

Then let's compare if one of the sizes is out of stock and output accordingly:

foreach($allSizes as $key => $size){
    if(in_array($key, $outStock)){
        echo "<li><strike>$size</strike> <small>(sold out)</small></li>";
    }
    else{
        echo "<li>$size</li>";
    }
}
echo "</ul>";
4
  • Hi Macro, This gives me the array but won't strike if something is sold out yet..
    – rwzdoorn
    Jul 2, 2015 at 9:13
  • Yes, complete list without a strike. In WP I've checked 'medium' and saved the post.
    – rwzdoorn
    Jul 2, 2015 at 9:24
  • Array ( [0] => M [1] => L ) . (and thank you for helping me out!)
    – rwzdoorn
    Jul 2, 2015 at 9:32
  • I have edited the code to add the $key, maybe that fixes the problem. It should.
    – marcovega
    Jul 2, 2015 at 9:33

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.