首先,您需要访问表单数据并正确保存它。例如,您正在检查$_POST[\'source\']
已设置,但没有输入name="source"
以你的形式,所以update_post_meta
未执行。即使是它update_post_meta
将被执行,您正在尝试存储$_POST[\'price\']
但在你的形式中,同样没有price
输入
快速适应:
class Price_Metabox
{
/**
* This is our constructor
*
* @return Price_Metabox
*/
public function __construct() {
add_action( \'add_meta_boxes\', array( $this, \'metabox_add\' ) );
add_action( \'save_post\', array( $this, \'price_save_postdata\') );
}
/**
* Add the Metabox
*
*/
public function metabox_add() {
// Add Metabox
add_meta_box(
\'prodInfo-meta\',
"New Dress Shoe Attributes",
array( $this, \'render_meta_elemnts\' ),
\'post\',
\'normal\',
\'low\'
);
}
/**
* Build the Metabox
*
* @param object $post
*
*/
public function render_meta_elemnts( $post ) {
$values = get_post_custom( $post->ID );
$price = isset( $values[\'metadata_price\'] ) ? esc_attr( $values[\'metadata_price\'][0] ) : \'\';
$sale = isset( $values[\'metadata_sale\'] ) ? \'checked\' : \'\';
$salepercentage = isset( $values[\'metadata_sale_percentage\'] ) ? esc_attr( $values[\'metadata_sale_percentage\'][0] ) : \'\';
$finalprice = isset( $values[\'metadata_final_price\'] ) ? esc_attr( $values[\'metadata_final_price\'][0] ) : \'\';
wp_nonce_field( plugin_basename( __FILE__ ), \'prodInfo_noncename\' )
?>
<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" \'.$sale.\'/>\'; ?></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><?php echo \'<label for="metadata_final_price">Final price : $</label>\'; ?></td>
<td><?php echo \'<input name="metadata_final_price" value="\'. $finalprice . \'" id="metadata_final_price" />\'; ?></td>
</tr>
</table>
<?php
}
/**
* Save post meta data
*
* This function will save the images ids as post meta data (comma separated string).
*
*/
public function price_save_postdata($post_id){
// First we need to check if the current user is authorised to do this action.
//Currently capabilities of property post type is the same as normal post type
if ( isset($_POST[\'post_type\']) && \'post\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_post\', $post_id ) ) return;
}
// Secondly we need to check if the user intended to change this value.
if ( !isset( $_POST[\'prodInfo_noncename\'] ) || ! wp_verify_nonce( $_POST[\'prodInfo_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Thirdly we can save the value to the database
if(isset($_POST[\'metadata_price\']) ):
//Don\'t forget sanitize
update_post_meta($post_id, \'metadata_price\', intval( $_POST[\'metadata_price\'] ) );
else:
if (isset($post_id)) {
delete_post_meta($post_id, \'metadata_price\');
}
endif;
if(isset($_POST[\'metadata_sale\']) ):
//Don\'t forget sanitize
update_post_meta($post_id, \'metadata_sale\', 1 );
else:
if (isset($post_id)) {
delete_post_meta($post_id, \'metadata_sale\');
}
endif;
if(isset($_POST[\'metadata_sale_percentage\']) ):
//Don\'t forget sanitize
update_post_meta($post_id, \'metadata_sale_percentage\', intval( $_POST[\'metadata_sale_percentage\'] ) );
if ( $_POST[\'metadata_sale_percentage\'] > 0 ) {
$finalprice = $_POST[\'metadata_price\'] - ($_POST[\'metadata_sale_percentage\'] * $_POST[\'metadata_price\'])/100;
update_post_meta($post_id, \'metadata_final_price\', $finalprice );
} else {
delete_post_meta($post_id, \'metadata_final_price\');
}
else:
if (isset($post_id)) {
delete_post_meta($post_id, \'metadata_sale_percentage\');
}
endif;
}
}
// Instantiate our class
$Price_Metabox = new Price_Metabox();
然后,显示元值:
$meta = get_post_custom($post->ID);
echo \'Price: \' . $meta[\'metadata_price\'][0];
if($meta[\'metadata_sale\'][0] > 0){
echo \'Discount (%): \' . $meta[\'metadata_sale_percentage\'][0];
echo \'Final price: \' . $meta[\'metadata_final_price\'][0];
}
请记住,这只是一个快速代码。它可能有错误,或者可能需要对生产环境进行一些改进。
此外,我建议使用分类术语将产品分组为折扣,并为价格、折扣等使用自定义字段。