Untitled
unknown
plain_text
7 months ago
13 kB
5
Indexable
<?php
namespace TinySolutions\cptwooint\PluginsSupport\BaBooking;
// Do not allow directly accessing this file.
use Automattic\WooCommerce\Internal\Admin\Orders\PageController;
use TinySolutions\cptwooint\Helpers\Fns;
use TinySolutions\cptwooint\Traits\SingletonTrait;
if ( ! defined( 'ABSPATH' ) ) {
exit( 'This script cannot be accessed directly.' );
}
/**
* LPInit
*/
class BabeInit {
/**
* Singleton
*/
/**
* Singleton
*/
use SingletonTrait;
/**
* Class Constructor
*/
private function __construct() {
add_filter( 'babe_checkout_content', [ $this, 'checkout_page_prepare' ], 15 );
add_filter( 'cptwoo_product_get_price', [ $this, 'bb_cptwoo_product_get_price' ], 20, 3 );
add_action( 'babe_order_created', [ $this, 'after_created_order' ], 15, 1 );
add_action( 'woocommerce_checkout_order_processed', [ $this, 'checkout_order_processed' ] );
add_action( 'woocommerce_order_status_completed', [ $this, 'wc_payment_for_bb' ], 10, 1 );
add_action( 'cmb2_admin_init', [ $this, 'order_metabox' ], 10, 1 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', [ $this, 'customer_completed_order' ], 15, 2 );
}
/**
* @param bool $isEnable bool.
* @param object $order order.
*
* @return false
*/
public function customer_completed_order( $isEnable, $order ) {
if ( ! $order instanceof \WC_Order ) {
return $isEnable;
}
$items = $order->get_items();
foreach ( $items as $item ) {
$bb_order = get_post( $item['product_id'] );
if ( ! Fns::is_supported( $bb_order->post_type ) ) {
continue;
}
$isEnable = false;
}
return $isEnable;
}
/**
* @return void
*/
public static function order_metabox() {
$page_controller = new PageController();
$prefix = '_';
$cmb = new_cmb2_box(
[
'id' => 'order_metabox_extra',
'title' => __( 'WooCommerce Order', 'ba-book-everything' ),
'object_types' => [ \BABE_Post_types::$order_post_type ],
'context' => 'side',
'priority' => 'high',
]
);
$order_id = $cmb->object_id();
$order_number = get_post_meta( $order_id, '_wc_payment_id', true );
$name = 'N/A';
if ( absint( $order_number ) ) {
$name = __( 'WooCommerce Order Id: ', 'ba-book-everything' ) . '<br/><a style="padding:10px 0;display: inline-block;" href="' . esc_url( $page_controller->get_edit_url( absint( $order_number ) ) ) . '"> ' . absint( $order_number ) . ' </a>';
}
$cmb->add_field(
[
'name' => $name,
'id' => $prefix . 'connect_to_wc_order',
'type' => 'title',
]
);
}
/**
* @param number $order_id Order Id.
* @return void
*/
public function wc_payment_for_bb( $order_id ) {
$wc_order = wc_get_order( $order_id );
$items = $wc_order->get_items();
$hasBaOrder = false;
foreach ( $items as $item ) {
$bb_order = get_post( $item['product_id'] );
if ( \BABE_Post_types::$order_post_type !== $bb_order->post_type ) {
continue;
}
if ( ! Fns::is_supported( $bb_order->post_type ) ) {
continue;
}
$hasBaOrder = true;
\BABE_Order::update_order_status( $item['product_id'], 'completed' );
// payment_deferred.
}
if ( $hasBaOrder ) {
add_filter( 'woocommerce_email_enabled_new_order', '__return_false' ); // Disable new order emails.
add_filter( 'woocommerce_email_enabled_customer_processing_order', '__return_false' ); // Disable customer processing emails.
add_filter( 'woocommerce_email_enabled_customer_completed_order', '__return_false' ); // Disable customer completed emails.
}
}
/**
* @param number $order_id Order Id.
* @return void
*/
public function checkout_order_processed( $order_id ) {
$wc_order = wc_get_order( $order_id );
$items = $wc_order->get_items();
$email = $wc_order->get_billing_email();
$first_name = $wc_order->get_billing_first_name();
$last_name = $wc_order->get_billing_last_name();
$phone_number = $wc_order->get_billing_phone();
$hasBaOrder = false;
foreach ( $items as $item ) {
$bb_order = get_post( $item['product_id'] );
if ( \BABE_Post_types::$order_post_type !== $bb_order->post_type ) {
continue;
}
if ( ! Fns::is_supported( $bb_order->post_type ) ) {
continue;
}
$hasBaOrder = true;
update_post_meta( $item['product_id'], '_wc_payment_id', $order_id );
update_post_meta( $item['product_id'], 'email', $email );
update_post_meta( $item['product_id'], 'first_name', $first_name );
update_post_meta( $item['product_id'], 'last_name', $last_name );
update_post_meta( $item['product_id'], 'phone', $phone_number );
$user_info = wp_get_current_user();
$customer_id = $user_info->ID;
if ( $customer_id < 1 ) {
$meta = [];
$meta['email'] = $email;
$meta['username'] = $email;
$meta['first_name'] = $first_name;
$meta['last_name'] = $last_name;
$customer_id = \BABE_Users::create_customer( $email, $meta, $meta['username'] );
}
\BABE_Order::update_order_customer( $item['product_id'], $customer_id );
\BABE_Order::update_order_status( $item['product_id'], 'payment_deferred' );
}
if ( $hasBaOrder ) {
add_filter( 'woocommerce_email_enabled_new_order', '__return_false' ); // Disable new order emails.
add_filter( 'woocommerce_email_enabled_customer_processing_order', '__return_false' ); // Disable customer processing emails.
add_filter( 'woocommerce_email_enabled_customer_completed_order', '__return_false' ); // Disable customer completed emails.
}
}
/**
* @return float
*/
public function bb_cptwoo_product_get_price( $price, $product, $post_type ) {
if ( 'order' !== $post_type ) {
return $price;
}
if ( ! Fns::is_supported( $post_type ) ) {
return $price;
}
$amount_to_pay = get_post_meta( $product->get_id(), '_regular_price', true );
if ( ! $amount_to_pay ) {
$total_with_coupon = \BABE_Order::get_order_total_amount( $product->get_id() );
$prepaid_received = \BABE_Order::get_order_prepaid_received( $product->get_id() ) - \BABE_Order::get_order_refunded_amount( $product->get_id() );
$amount_to_pay = $total_with_coupon - $prepaid_received;
}
return $amount_to_pay;
}
/**
* @return void
*/
public function after_created_order( $order_id ) {
$total_with_coupon = \BABE_Order::get_order_total_amount( $order_id );
$prepaid_received = \BABE_Order::get_order_prepaid_received( $order_id ) - \BABE_Order::get_order_refunded_amount( $order_id );
$amount_to_pay = $total_with_coupon - $prepaid_received;
update_post_meta( $order_id, '_regular_price', $amount_to_pay );
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $order_id, '1' );
}
/**
* @param string $content Content.
* @return mixed|string
*/
public function checkout_page_prepare( $content ) {
add_filter( 'woocommerce_is_checkout', '__return_true' );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
// Check if WooCommerce session is active and cart is not empty.
if ( ! WC()->session || WC()->cart->is_empty() ) {
return __( 'Session expired. Please log in again or restart your checkout process.', 'ba-book-everything' );
}
$output = $content;
$args = (array) wp_parse_args(
$_GET,
[
'order_id' => 0,
'order_num' => '',
'order_hash' => '',
]
);
// is order data valid.
$order_id = absint( $args['order_id'] );
if ( ! WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $order_id ) ) ) {
// No, it isn't in cart!
$this->after_created_order( $order_id );
}
if ( \BABE_Order::is_order_valid( $order_id, $args['order_num'], $args['order_hash'] ) ) {
// get order meta.
$order_meta = \BABE_Order::get_order_meta( $order_id );
if ( empty( $order_meta ) ) {
return $output;
}
$args['total_amount'] = $order_meta['_total_amount'];
$args['prepaid_amount'] = $order_meta['_prepaid_amount'];
$args['payment_model'] = $order_meta['_payment_model'];
$args['order_currency'] = $order_meta['_order_currency'];
$order_status = $order_meta['_status'];
// clear order meta.
$order_meta = \BABE_Order::clear_order_meta( $order_meta );
$args['meta'] = $order_meta;
if ( 'payment_expected' === $order_status || 'draft' === $order_status ) {
if ( ! isset( $order_meta['first_name'] ) ) {
// get user meta if user is logged in.
$user_info = wp_get_current_user();
if ( null != $user_info && $user_info->ID > 0 ) {
$args['meta']['email'] = $user_info->user_email;
$args['meta']['email_check'] = $user_info->user_email;
$args['meta']['first_name'] = $user_info->first_name;
$args['meta']['last_name'] = $user_info->last_name;
$contacts = get_user_meta( $user_info->ID, 'contacts', 1 );
if ( is_array( $contacts ) ) {
$args['meta'] += $contacts;
}
}
} else {
$args['meta']['email_check'] = $args['meta']['email'];
}
// Select Action.
if ( 'payment_expected' === $order_status || ( 'draft' === $order_status && 'auto' === \BABE_Settings::$settings['order_availability_confirm'] ) ) {
$args['action'] = 'to_pay';
} else {
$args['action'] = 'to_av_confirm';
}
$output .= $this->checkout_form( $args );
} // end if payment_expected or draft.
}
if ( empty( $output ) ) {
$output = 'Invalid Order. Please Order Again';
}
return $output;
}
/**
* Add checkout form to page.
*
* @param array $args
* @return string
*/
public function checkout_form( $args ) {
$output = '';
$input_fields = [];
$args = wp_parse_args(
$args,
[
'order_id' => 0,
'order_num' => '',
'order_hash' => '',
'total_amount' => 0,
'prepaid_amount' => 0,
'payment_model' => 'full',
'order_currency' => '',
'action' => 'to_pay', // to_pay or to_av_confirm.
'meta' => [],
]
);
$args['meta'] = wp_parse_args(
$args['meta'],
[
'first_name' => '',
'last_name' => '',
'email' => '',
'email_check' => '',
'phone' => '',
]
);
$order_id = $args['order_id'];
$order_num = $args['order_num'];
$order_hash = $args['order_hash'];
$action = $args['action'];
$total_amount = $args['total_amount'];
$prepaid_amount = $args['prepaid_amount'];
$payment_model = $args['payment_model'];
$currency = $args['order_currency'] ?: \BABE_Order::get_order_currency( $order_id );
$args['meta'] = apply_filters( 'babe_checkout_args', $args['meta'], $args );
/* translators: %s is a order number */
$output .= '<h2>' . sprintf( __( 'Order #%s', 'ba-book-everything' ), $order_num ) . '</h2>';
$output .= \BABE_html::order_items( $order_id );
$output = apply_filters( 'babe_checkout_after_order_items', $output, $args );
// fields.
foreach ( $args['meta'] as $field_name => $field_content ) {
if ( in_array( $field_name, [ 'extra_guests', 'billing_address' ], true ) ) {
continue;
}
$add_content_class = $field_content ? 'checkout_form_input_field_content' : '';
$input_fields[ $field_name ] = '
<div class="checkout-form-block">
<div class="checkout_form_input_field ' . $add_content_class . '">
<label class="checkout_form_input_label">' . \BABE_html::checkout_field_label( $field_name ) . '</label>
<input type="text" class="checkout_input_field checkout_input_required" name="' . $field_name . '" id="' . $field_name . '" value="' . $field_content . '" ' . apply_filters( 'babe_checkout_field_required', '', $field_name ) . '/>
<div class="checkout_form_input_underline"><span class="checkout_form_input_ripple"></span></div>
</div>
</div>';
}
$output .= '<div id="babe_search_result_refresh">
<i class="fas fa-spinner fa-spin fa-3x"></i>
</div>';
$output = apply_filters( 'babe_checkout_form_html', $output, $args );
add_action(
'woocommerce_checkout_order_review',
function () use ( $output ) {
echo wp_kses_post( $output );
},
10
);
// Get checkout object.
ob_start();
if ( ! WC()->cart->is_empty() ) {
woocommerce_output_all_notices();
echo do_shortcode( '[woocommerce_checkout]' );
wp_enqueue_script( 'wc-checkout' );
wp_enqueue_script( 'woocommerce' );
wp_enqueue_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_script( 'selectWoo' );
?>
<style>
div#checkout_form_block h3#order_review_heading {
display: none;
}
</style>
<?php
}
$checkout_output = ob_get_clean();
if ( $output ) {
$output = '
<div id="checkout_form_block">
' . $checkout_output . '
</div>';
}
return $output;
}
}Editor is loading...
Leave a Comment