Skip to content

Instantly share code, notes, and snippets.

@dwainm
Last active February 18, 2017 22:09
Show Gist options
  • Save dwainm/b0032c772f822bc3689aca56447a5611 to your computer and use it in GitHub Desktop.
Save dwainm/b0032c772f822bc3689aca56447a5611 to your computer and use it in GitHub Desktop.
Generate WooCommerce bookings unit tests given a product ID.
<?php
/**
* Bookings
*/
add_action( 'init', function(){
// WC_Stamps_API::get_client();
//add_filter( 'wc_bookings_calendar_default_to_current_date', '__return_false' );
//if ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) return;
//booking_product_to_code_print_meta_booking(9454); die;
return;
$posts = get_posts( array( 'posts_per_page'=> '-1', 'post_type' => 'product', 'field' => 'ID' ) );
$bookings= array();
foreach ( $posts as $post ) {
$product = wc_get_product( $post->ID );
if ( 'booking' == $product->get_type() ) {
$bookings[] = $product;
}
}
foreach ( $bookings as $booking) {
$function_title = 'test_' . implode( '_', explode( ' ', preg_replace('/[^A-Za-z0-9\-]/', '', $booking->get_title() ) ) );
echo "public function $function_title() {" . '<br>';
booking_product_to_code_print_meta_booking( $booking->get_id() );
echo '<br><br>';
echo '} <br><br>';
}
die;
}
);
function booking_product_to_code_print_meta_booking( $bookable_product_id ) {
$default_values = array(
'_wc_booking_min_duration' => '1',
'_wc_booking_resources_assignment' => '',
'_wc_booking_max_duration' => '1',
'_wc_booking_enable_range_picker' => 'no',
'_wc_booking_calendar_display_mode' => '',
'_wc_booking_qty' => '1',
'_wc_booking_has_persons' => 'no',
'_wc_booking_has_resources' => 'no',
'_wc_booking_resources_assignment' => 'customer',
'_wc_booking_duration_type' => 'fixed',
'_wc_booking_duration' => '1',
'_wc_booking_duration_unit' => 'month',
'_wc_booking_cancel_limit' => '1',
'_wc_booking_cancel_limit_unit' => 'month',
'_wc_booking_max_date' => '12',
'_wc_booking_max_date_unit' => 'month',
'_wc_booking_min_date' => '',
'_wc_booking_min_date_unit' => 'month',
'_wc_booking_default_date_availability' => 'non-available',
'_wc_booking_first_block_time' => '',
'_wc_booking_check_availability_against' => '',
//buffer settings
'_wc_booking_buffer_period' => '',
//Cost
'_wc_booking_pricing' => array(),
'_wc_booking_base_cost' => '',
// persons specific
'_wc_booking_min_persons_group' => '1', // max
'_wc_booking_max_persons_group' => '12', // min
'_wc_booking_person_cost_multiplier' => 'no',
'_wc_booking_has_person_types' => 'no', // person types (e.g. different price for children)
'_wc_booking_person_qty_multiplier' => 'no', // count person as booking
);
$output = '$meta_values = array(' . '<br>';
$keys = array_keys( $default_values );
foreach ( $keys as $key ) {
$value = get_post_meta( $bookable_product_id, $key, true );
$default_value = $default_values[ $key ];
if ( $value !== $default_value ) {
$output .= "'" . $key . "' => '" . $value . "'," . '<br>';
}
}
$rules = get_post_meta( $bookable_product_id, '_wc_booking_availability', true );
$output .= booking_to_test_get_rules_meta_output( $rules );
$output .= ');' . '<br><br>';
$output .= '$booking_product = Factory\Booking_Product::create( $meta_values );' . '<br>';
$product = new WC_Product_Booking( $bookable_product_id );
// Resources
$resources = $product->get_resources();
if ( ! empty( $resources ) ) {
foreach ( $resources as $resource ) {
$output .= booking_to_code_get_resource_output( $resource );
}
}
// Persons
$person_types = $product->get_person_types();
if ( ! empty( $person_types ) ) {
foreach ( $person_types as $person_type ) {
$output .= booking_to_code_get_person_type_output( $person_type );
}
}
echo $output;
}
function booking_to_code_get_resource_output( $resource ) {
$output = '$resource_meta = array(' . '<br>';
$rules = (array) get_post_meta( $resource->ID, '_wc_booking_availability', true );
$output .= booking_to_test_get_rules_meta_output( $rules );
$output .= ');' . '<br><br>';
$output .= 'Factory\Booking_Resource::create( array(), $resource_meta, $booking_product->get_id() );' . '<br>';
return $output;
}
function booking_to_code_get_person_type_output( $person_type ) {
$meta_kvs = array(
'cost' => '',
'block_cost' => '',
'min' => '',
'max' => '',
);
foreach ( $meta_kvs as $k => $v ) {
$value = get_post_meta( $person_type->ID, $k, true );
if ( false !== $value ) {
$meta_kvs[ $k ] = $value;
}
}
$output = '$person_meta = ';
$output .= var_export( $meta_kvs, true );
$output .= ';' . '<br><br>';
$output .= 'Factory\Booking_Person::create( $booking_product->get_id() , $person_meta );' . '<br>';
return $output;
}
function booking_to_test_get_rules_meta_output( $rules ) {
$output = '';
if ( ! empty( $rules ) ) {
$output .= "'_wc_booking_availability' => array(" . '<br>';
foreach ( $rules as $index => $rule ) {
$output .= $index . ' => ' . var_export( $rule, true ) . ',<br>';
}
$output .= '),' . '<br>';
}
return $output;
}
function readable_date( ) {
include dir( WC_BOOKINGS_MAIN_FILE );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment