Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Fix #6: Add support for multiple prices for each menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWr committed Oct 7, 2016
1 parent 4c8def9 commit 988f325
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 15 deletions.
36 changes: 36 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/* Input controls for custom post type options */
.fdm-input-control {
position: relative;
margin: 1em 0;
}
.fdm-input-control:last-child {
Expand All @@ -32,6 +33,41 @@
display: inline;
width: auto;
}
.fdm-input-control .fdm-input-delete {
position: absolute;
top: 50%;
right: 0;
width: 20px;
height: 22px;
transform: translateY(-50%);
padding: 0 0.5em;
background: #eee;
border-left: 1px solid #ddd;
color: red;
text-align: center;
text-decoration: none;
overflow: hidden;
}
.fdm-input-control .fdm-input-delete:before {
content: "×";
width: 20px;
line-height: 20px;
text-align: center;
font-size: 16px;
font-weight: 700;
}
.fdm-input-control .fdm-input-delete:hover,
.fdm-input-control .fdm-input-delete:focus {
background: red;
}
.fdm-input-control .fdm-input-delete:hover:before,
.fdm-input-control .fdm-input-delete:focus:before {
color: #fff;
}
#fdm-input-prices .fdm-input-control:first-child .fdm-input-delete {
display: none;
}


/**
* Menu Organizer
Expand Down
25 changes: 25 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ jQuery( function ( $ ) {
return;
}

// Re-usable function to remove a price entry field
function removePrice() {
$( this ).closest( '.fdm-input-control' ).remove();
return false;
}

// Add a price entry field
$( '#fdm-price-add', $prices ).click( function( e ) {
var $price_input = $prices.find( '.fdm-input-control' ).last(),
$new_price_input = $price_input.clone();

$new_price_input.find( 'input[name="fdm_item_price[]"]' ).val( '' );

$price_input.after( $new_price_input );

// Reset click handlers
$( '.fdm-input-delete', $prices ).off()
.click( removePrice );

return false;
} );

// Remove a price entry field
$( '.fdm-input-delete', $prices ).click( removePrice );

} );

/**
Expand Down
25 changes: 25 additions & 0 deletions assets/js/src/menu-item-prices.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,29 @@ jQuery( function ( $ ) {
return;
}

// Re-usable function to remove a price entry field
function removePrice() {
$( this ).closest( '.fdm-input-control' ).remove();
return false;
}

// Add a price entry field
$( '#fdm-price-add', $prices ).click( function( e ) {
var $price_input = $prices.find( '.fdm-input-control' ).last(),
$new_price_input = $price_input.clone();

$new_price_input.find( 'input[name="fdm_item_price[]"]' ).val( '' );

$price_input.after( $new_price_input );

// Reset click handlers
$( '.fdm-input-delete', $prices ).off()
.click( removePrice );

return false;
} );

// Remove a price entry field
$( '.fdm-input-delete', $prices ).click( removePrice );

} );
6 changes: 4 additions & 2 deletions fdm-templates/content/price.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div class="fdm-item-price-wrapper">
<span class="fdm-item-price"><?php echo $this->price; ?></span>
</div>
<?php foreach( $this->prices as $price ) : ?>
<div class="fdm-item-price"><?php echo $price; ?></div>
<?php endforeach; ?>
</div>
48 changes: 38 additions & 10 deletions includes/class-custom-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,31 @@ public function show_item_price() {

// Retrieve values for this if it exists
global $post;
$price = get_post_meta( $post->ID, 'fdm_item_price', true );
$prices = get_post_meta( $post->ID, 'fdm_item_price' );

// Always add at least one price input field
if ( empty( $prices ) ) {
$prices = array( '' );
}

?>

<div class="fdm-input-controls fdm-input-side-panel">
<div class="fdm-input-control">
<label for="fdm_item_price"><?php echo __( 'Price', 'food-and-drink-menu' ); ?></label>
<input type="text" name="fdm_item_price" id="fdm_item_price" value="<?php echo esc_attr( $price ); ?>">
<div id="fdm-input-prices" class="fdm-input-group">
<?php foreach( $prices as $key => $price ) : ?>
<div class="fdm-input-control">
<label for="fdm_item_price" class="screen-reader-text"><?php echo __( 'Price', 'food-and-drink-menu' ); ?></label>
<input type="text" name="fdm_item_price[]" value="<?php echo esc_attr( $price ); ?>">
<a href="#" class="fdm-input-delete">
<?php esc_html_e( 'Remove this price' ); ?>
</a>
</div>
<?php endforeach; ?>
<div class="fdm-input-group-add">
<a href="#" id="fdm-price-add">
<?php esc_html_e( 'Add Price' ); ?>
</a>
</div>
</div>

<?php do_action( 'fdm_show_item_price' ); ?>
Expand Down Expand Up @@ -501,12 +518,23 @@ public function save_meta( $post_id ) {

// Save the metadata
foreach ($meta_ids as $meta_id => $sanitize_callback) {
$cur = get_post_meta( $post_id, $meta_id, true );
$new = isset( $_POST[$meta_id] ) ? call_user_func( $sanitize_callback, $_POST[$meta_id] ) : '';
if ( $new && $new != $cur ) {
update_post_meta( $post_id, $meta_id, $new );
} elseif ( $new == '' && $cur ) {
delete_post_meta( $post_id, $meta_id, $cur );

if ( $meta_id == 'fdm_item_price' ) {
delete_post_meta( $post_id, $meta_id );
$new = isset( $_POST[$meta_id] ) ? array_map( $sanitize_callback, $_POST[$meta_id] ) : array();
foreach( $new as $new_entry ) {
if ( $new_entry !== '' ) {
add_post_meta( $post_id, $meta_id, $new_entry );
}
}
} else {
$cur = get_post_meta( $post_id, $meta_id, true );
$new = isset( $_POST[$meta_id] ) ? call_user_func( $sanitize_callback, $_POST[$meta_id] ) : '';
if ( $new && $new != $cur ) {
update_post_meta( $post_id, $meta_id, $new );
} elseif ( $new == '' && $cur ) {
delete_post_meta( $post_id, $meta_id, $cur );
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions views/View.Item.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public function render() {
* @since 1.1
*/
public function print_elements( $location ) {

$output = '';

foreach( $this->elements_order as $element ) {
if ( isset( $this->elements[$element] ) && $this->elements[$element] == $location ) {

Expand Down Expand Up @@ -138,7 +138,14 @@ public function load_item() {

$settings = get_option( 'food-and-drink-menu-settings' );
if ( !$settings['fdm-disable-price'] ) {
$this->price = get_post_meta( $this->id, 'fdm_item_price', true );
$this->prices = get_post_meta( $this->id, 'fdm_item_price' );

// Load a single price string to be compatible with custom templates
// created before v1.5.
$this->price = join(
apply_filters( 'fdm_prices_separator', _x( '/', 'Separator between multiple prices.', 'food-and-drink-menu' ) ),
$this->prices
);
}

do_action( 'fdm_load_item', $this );
Expand Down

0 comments on commit 988f325

Please sign in to comment.