xah icon

cost price field

xah | PRO | 11/23/21 06:59:04 AM UTC (Edited) | 0 ⭐ | 376 👁️ | Never ⏰ | []
PHP |

980 B

|

None

|

0 👍

/

0 👎

// Not mine
 
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'cost_price', 'class' => 'wc_input_price short', 'label' => __( 'Cost Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}
 
add_action( 'save_post', 'wc_cost_save_product' );
function wc_cost_save_product( $product_id ) {
 
     // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature
     if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
        return;
 
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['cost_price'] ) ) {
        if ( is_numeric( $_POST['cost_price'] ) )
            update_post_meta( $product_id, 'cost_price', $_POST['cost_price'] );
    } else delete_post_meta( $product_id, 'cost_price' );
}

Comments