Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created April 11, 2022 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikparmar/19b9974f0aee65a443f2f8e5021ed575 to your computer and use it in GitHub Desktop.
Save kartikparmar/19b9974f0aee65a443f2f8e5021ed575 to your computer and use it in GitHub Desktop.
Adding Base Cost to the Booking Price.
<?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