0

I'm building a site for ordering food online. It's not a shop (visitors will not be able to pay online, just to order for delivery). My initial idea was to define all the food and drink items as custom post types. But I haven't figured out how to set up product variations. For example, a pizza may be small, medium or large, and depending on the size selected, its price is different. Or a salad may have Italian or French dressing, which also influences its price.

Can this be achieved using WP custom post types?

2
  • This seems like exactly the kind of thing eCommerce plugins are meant for. Have you considered using one of those, and focusing your custom development efforts on allowing "checking out" without paying? That might be far easier than building all the variation functionality. Nov 6, 2015 at 19:03
  • @tnorthcutt it would be the exact problem eCommerce would solve if it weren't for two things: 1. The client wants it to only be ordering online, not paying (payment processing online); 2. I don't get to choose how I'll do it, unfortunately; I can give suggestions, but ultimately I need to do it as I'm told. Nov 6, 2015 at 23:59

2 Answers 2

1

Use it can be done using CUSTOM META BOX. Add following to function.php:

/* Adds pizza size meta box to the post editing screen 
******************************************************/
function prfx_custom_meta() {
    add_meta_box( 'prfx_meta', __( 'Pizza Size', 'prfx-textdomain' ), 'prfx_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );


/* Outputs the content of the meta box 
**************************************/
function prfx_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
    $prfx_stored_meta = get_post_meta( $post->ID );
    ?>

    <p>
    <label for="meta-text" class="prfx-row-title" style="clear:both; float: left; width:100%;" ><?php _e( '<b>Enter Pizza Size</b> ', 'prfx-textdomain' )?></label>
        <textarea name="meta-text" id="meta-text" maxlength="250" style="clear:left; float: left; width: 100%;" rows="4" cols="90"><?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?></textarea>
        <p>You can write like: Available in: 21" and 16" only</p>
    </p>

    <?php
}


/* Saves the custom meta input 
******************************/
function prfx_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and sanitizes/saves if needed
    if( isset( $_POST[ 'meta-text' ] ) ) {
        update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
    }

}
add_action( 'save_post', 'prfx_meta_save' );

And then goto piece of code where you would like to show pizza sizes e.g I want to show it on single.php, so I will add the following code after description:

<?php $meta_value = get_post_meta( get_the_ID(), 'meta-text', true );

// Checks and displays the retrieved value
if( !empty( $meta_value ) )
    echo $meta_value;
else 
    echo "No pizza sizes available";
?>
0

Yes, this is exactly the purpose for custom post types. To handle your product variations, several custom fields can be added, specific to each post type.

A common solution for the custom fields is Elliot Condon's excellent Advanced Custom Fields plugin. No plugin is necessary for this, however, and there are other options out there for easy custom field management.

Writing the code for your own metaboxes (to enclose groups of product options) and other headaches are managed by the plugin instead.

Your Answer

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

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