我向 WooCommerce 添加了一个自定义价格字段。该字段按预期出现,但它不保存输入的值。
我functions.php文件中的代码是:
/* Add custom price field to general page */
function wc_cost_product_field() {
woocommerce_wp_text_input( array( 'id' => 'wholesaler_price', 'class' => 'wc_input_price short', 'label' => __( 'Wholesaler price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function pcc_save_custom_price( $post_id ) {
// Grab the custom price from $_POST
$custom_price = isset( $_POST[ 'wholesale_price' ] ) ? sanitize_text_field( $_POST[ 'wholesale_price' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// Save the custom price using WooCommerce built-in functions
$product->update_meta_data( 'wholesale_price', $custom_price );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'pcc_save_custom_price' );
慕婉清6462132