Using WP 3.8.1, I have a Custom Post Type
which is called "Sport" and it has a Metdabox - medata data as
I do not need to set up online shopping but I world like to add Price of my products to any Sport CPT and enable updating the Price in the WP table by Checking the Sale on checkbox and specifying sale amount or presentage. so the out put looks like I know some part of job can be done by JavaScript(Like adding Sale class to Price or adding sale icon to box) on checkbox but on WP side I also need to update the table to take new price also be able to list all on sale Items for each CPT.
Can you please let me know how to do this? and this is the code:
<?php
/* Custom Meta Boxex */
add_action('add_meta_boxes', 'cpt_metadata_add');
add_action('save_post', 'save_options');
function cpt_metadata_add()
{
add_meta_box(
"prodInfo-meta",
"New Dress Shoe Attributes ",
"render_meta_elemnts",
"sport",
"normal",
"low"
);
}
function render_meta_elemnts()
{
global $post;
$values = get_post_custom( $post->ID );
$price = isset( $values['metadata_price'] ) ? esc_attr( $values['metadata_price'][0] ) : '';
$sale = isset( $values['metadata_sale'] ) ? esc_attr( $values['metadata_sale'][0] ) : '';
$saleamount = isset( $values['metadata_sale_amount'] ) ? esc_attr( $values['metadata_sale_amount'][0] ) : '';
$salepercentage = isset( $values['metadata_sale_percentage'] ) ? esc_attr( $values['metadata_sale_percentage'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table>
<tr>
<td><?php echo '<label for="metadata_price">Price : $</label>'; ?></td>
<td><?php echo '<input name="metadata_price" value="'. $price . '" id="metadata_price" />'; ?></td>
<td style="width:25px;"></td>
<td><?php echo '<label for="metadata_sale">Sale On :</label> <input type="checkbox" name="metadata_sale" id="metadata_sale" <?php checked( $sale, 'on' ); ?> />'; ?></td>
<td><?php echo '<label for="metadata_sale_percentage">Sale Percentage : %</label>'; ?></td>
<td><?php echo '<input name="metadata_sale_percentage" value="'. $salepercentage . '" id="metadata_sale_percentage" />'; ?></td>
<td style="width:25px;"></td>
<td><?php echo '<label for="metadata_sale_amount">Sale Amount : $</label>'; ?></td>
<td><?php echo '<input name="metadata_sale_amount" value="'. $saleamount . '" id="metadata_sale_amount" />'; ?></td>
</tr>
</table>
<?php
}
function save_options()
{
global $post;
if (!isset($_POST['source']) || $post->post_type != 'sport'){
return $post;
}
update_post_meta($post->ID, "price", $_POST['price']);
}
Update:
<?php
$args = array( 'post_type' => 'sport');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_custom($post->ID);
the_title();
echo $meta['price'][0];
endwhile;
?>
Update 2
<?php
$args = array( 'post_type' => 'sport');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_custom($post->ID);
if(metadata_sale = checked) {
echo $meta['metadata_price'][0] - $meta['metadata_sale_percentage'][0];
}
else{
echo $meta['metadata_price'][0];
}
endwhile;
?>