In woocommerce, I am using the following code to display an input text field on admin product edit pages, that allow me to add an additonal pricing option to the product:
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
woocommerce_wp_text_input( array( 'id' => 'repair_price', 'class' => 'wc_input_price short', 'label' => __( 'Repair Cost', '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['repair_price'] ) ) {
if ( is_numeric( $_POST['repair_price'] ) )
update_post_meta( $product_id, 'repair_price', $_POST['repair_price'] );
} else delete_post_meta( $product_id, 'repair_price' );
}
Now on the product page (in the front end) I need a checkbox "Repair cost $20", that will appear when the product custom field pricing option is filled with some price.
If customer add this option, it will add an additional cost of $20 on the product and it should also be reflected on the cart and checkout pages.
Any help will be appreciated.
In the following code, I have revisited a bit your actual code and added all necessary code to enable the checkbox option in single product page if the pricing option in the product is filled (for non variable products type that are not on sale).
When the option is selected by the customer, the product price is increased and when added to cart the price change is reflected in cart and checkout pages (see the screenshots at the end).
The code:
Code goes in function.php file of your active child theme (or active theme). tested and works.
Allow to save the repair price as order item meta data and display it everywhere on orders and email notifications:
Code goes in function.php file of your active child theme (or active theme). tested and works.