Created
April 11, 2022 05:12
-
-
Save kartikparmar/19b9974f0aee65a443f2f8e5021ed575 to your computer and use it in GitHub Desktop.
Adding Base Cost to the Booking Price.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Display Fields | |
function woocommerce_product_base_cost_fields() { | |
global $woocommerce, $post; | |
echo '<div class="product_custom_field">'; | |
// Custom Product Number Field. | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_bkap_booking_base_cost', | |
'placeholder' => '0.00', | |
'label' => __( 'Base Cost', 'woocommerce' ), | |
'type' => 'number', | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
), | |
) | |
); | |
echo '</div>'; | |
} | |
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_product_base_cost_fields' ); | |
// Save Fields. | |
function woocommerce_product_base_cost_fields_save( $post_id ) { | |
// Custom Product Number Field. | |
$woocommerce_custom_product_number_field = $_POST['_bkap_booking_base_cost']; | |
if ( ! empty( $woocommerce_custom_product_number_field ) ) { | |
update_post_meta( $post_id, '_bkap_booking_base_cost', esc_attr( $woocommerce_custom_product_number_field ) ); | |
} | |
} | |
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_base_cost_fields_save', 10, 1 ); | |
// Adding Base Cost to Booking Price. | |
function bkap_booking_price_with_base_cost( $price, $product_id ) { | |
$base_cost_value = get_post_meta( $product_id, '_bkap_booking_base_cost', true ); | |
if ( isset( $base_cost_value ) ) { | |
$price = $price + (float) $base_cost_value; | |
} | |
return $price; | |
} | |
add_filter( 'bkap_modify_booking_price', 'bkap_booking_price_with_base_cost', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment