562 lines
No EOL
17 KiB
PHP
562 lines
No EOL
17 KiB
PHP
<?php
|
||
// Check email and order number is valid or not
|
||
if( ! function_exists( 'softoba_check_email_and_order_number' ) ) {
|
||
function softoba_check_email_and_order_number($order_id, $email) {
|
||
$order = wc_get_order($order_id);
|
||
if ($order && $order->get_billing_email() == $email) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// Check existing return request
|
||
if (!function_exists('softoba_check_existing_return_request')) {
|
||
function softoba_check_existing_return_request($order_id, $email) {
|
||
$args = array(
|
||
'post_type' => SOFTOBA_RETURN_POST_TYPE,
|
||
'meta_query' => array(
|
||
'relation' => 'AND',
|
||
array(
|
||
'key' => 'softoba_order_id',
|
||
'value' => $order_id,
|
||
'compare' => '=',
|
||
),
|
||
array(
|
||
'key' => 'softoba_billing_email',
|
||
'value' => $email,
|
||
'compare' => '=',
|
||
),
|
||
),
|
||
);
|
||
|
||
$query = new WP_Query($args);
|
||
$post_id = $query->have_posts() ? $query->posts[0] : false;
|
||
if ($post_id) {
|
||
return get_post_status($post_id);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Check existing exchange request
|
||
if (!function_exists('softoba_check_existing_exchange_request')) {
|
||
function softoba_check_existing_exchange_request($order_id, $email) {
|
||
$args = array(
|
||
'post_type' => SOFTOBA_EXCHANGE_POST_TYPE,
|
||
'meta_query' => array(
|
||
'relation' => 'AND',
|
||
array(
|
||
'key' => 'softoba_ex_order_id',
|
||
'value' => $order_id,
|
||
'compare' => '=',
|
||
),
|
||
array(
|
||
'key' => 'softoba_ex_billing_email',
|
||
'value' => $email,
|
||
'compare' => '=',
|
||
),
|
||
),
|
||
);
|
||
|
||
$query = new WP_Query($args);
|
||
$post_id = $query->have_posts() ? $query->posts[0] : false;
|
||
if ($post_id) {
|
||
return get_post_status($post_id);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Check existing exchange request payment status and return post ID
|
||
if (!function_exists('softoba_exchange_payment_status')) {
|
||
function softoba_exchange_payment_status($order_id, $email) {
|
||
$args = array(
|
||
'post_type' => SOFTOBA_EXCHANGE_POST_TYPE,
|
||
'meta_query' => array(
|
||
'relation' => 'AND',
|
||
array(
|
||
'key' => 'softoba_ex_order_id',
|
||
'value' => $order_id,
|
||
'compare' => '=',
|
||
),
|
||
array(
|
||
'key' => 'softoba_ex_billing_email',
|
||
'value' => $email,
|
||
'compare' => '=',
|
||
),
|
||
),
|
||
'fields' => 'ids', // Only retrieve post IDs
|
||
);
|
||
$query = new WP_Query($args);
|
||
$post_id = $query->have_posts() ? $query->posts[0] : false;
|
||
|
||
if ($post_id) {
|
||
$exchange_fee = get_post_meta($post_id, 'softoba_exchange_fee', true);
|
||
|
||
if ($exchange_fee === 'completed' || $exchange_fee === 'processing') {
|
||
return array('status' => 'completed', 'post_id' => $post_id);
|
||
}elseif( $exchange_fee === 'on-hold' ) {
|
||
return array( 'status' => 'on-hold' );
|
||
}else {
|
||
return array('status' => 'pending', 'post_id' => $post_id);
|
||
}
|
||
} else {
|
||
// No matching exchange request found
|
||
return array('status' => 'not-found', 'post_id' => false);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* Check if an order has variable product items
|
||
*
|
||
* @param int|WC_Order $order The order ID or WC_Order object
|
||
* @return bool
|
||
*/
|
||
if (!function_exists('softoba_has_variable_product_item')) {
|
||
function softoba_has_variable_product_item($order) {
|
||
if (is_numeric($order)) {
|
||
$order = wc_get_order($order);
|
||
}
|
||
|
||
if ($order instanceof WC_Order) {
|
||
foreach ($order->get_items() as $item) {
|
||
$product_id = $item->get_product_id();
|
||
$product = wc_get_product($product_id);
|
||
|
||
if ($product && $product->is_type('variable')) {
|
||
$variations = $product->get_available_variations();
|
||
|
||
if (!empty($variations)) {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
// Check order is exchangable or not
|
||
if( ! function_exists( 'softoba_check_order_exchangable' ) ){
|
||
function softoba_check_order_exchangable($order_id) {
|
||
$order = wc_get_order($order_id);
|
||
|
||
if (!$order) {
|
||
return false; // Order not found, return false
|
||
}
|
||
|
||
$date_created = strtotime($order->get_date_created());
|
||
$current_date = current_time('timestamp');
|
||
$return_period = 20 * 24 * 60 * 60; // 20 days in seconds
|
||
|
||
if (($current_date - $date_created) > $return_period) {
|
||
return true; // Order is older than 20 days
|
||
}
|
||
|
||
return false; // Order is not older than 20 days
|
||
}
|
||
}
|
||
|
||
function softoba_update_post_meta_raw($post_id, $meta_key, $meta_value) {
|
||
global $wpdb;
|
||
|
||
// Escape values to prevent SQL injection
|
||
$escaped_meta_key = esc_sql($meta_key);
|
||
$escaped_meta_value = esc_sql($meta_value);
|
||
|
||
// Run the raw query to update post meta
|
||
$query = $wpdb->prepare(
|
||
"INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES (%d, %s, %s)
|
||
ON DUPLICATE KEY UPDATE meta_value = %s",
|
||
$post_id,
|
||
$escaped_meta_key,
|
||
$escaped_meta_value,
|
||
$escaped_meta_value
|
||
);
|
||
|
||
$wpdb->query($query);
|
||
}
|
||
|
||
|
||
if ( ! function_exists( 'softoba_get_base_product_id' ) ) {
|
||
/**
|
||
* Retrieve the parent product ID for WC_Product_Variation instances
|
||
* or the product ID in the other cases.
|
||
*
|
||
* @param WC_Product $product The product.
|
||
*
|
||
* @return int
|
||
*/
|
||
function softoba_get_base_product_id( $product ) {
|
||
|
||
return $product instanceof WC_Data && $product->is_type( 'variation' ) ?
|
||
softoba_get_prop( $product, 'parent_id' ) :
|
||
softoba_get_prop( $product, 'id' );
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_get_prop' ) ) {
|
||
/**
|
||
* Retrieve a property.
|
||
*
|
||
* @param object $object The object.
|
||
* @param string $key The Meta Key.
|
||
* @param bool $single Return first found meta with key, or all.
|
||
* @param string $context What the value is for. Valid values are view and edit.
|
||
*
|
||
* @return mixed|null The related value or null (if the $object is not a valid object).
|
||
* @deprecated 3.5 | use the WooCommerce CRUD directly instead.
|
||
*/
|
||
function softoba_get_prop( $object, $key, $single = true, $context = 'view' ) {
|
||
$prop_map = softoba_return_new_attribute_map();
|
||
$is_wc_data = $object instanceof WC_Data;
|
||
|
||
if ( $is_wc_data ) {
|
||
$key = ( array_key_exists( $key, $prop_map ) ) ? $prop_map[ $key ] : $key;
|
||
$getter = false;
|
||
if ( method_exists( $object, "get{$key}" ) ) {
|
||
$getter = "get{$key}";
|
||
} elseif ( method_exists( $object, "get_{$key}" ) ) {
|
||
$getter = "get_{$key}";
|
||
}
|
||
|
||
if ( $getter ) {
|
||
return $object->$getter( $context );
|
||
} else {
|
||
return $object->get_meta( $key, $single );
|
||
}
|
||
} else {
|
||
$key = ( in_array( $key, $prop_map, true ) ) ? array_search( $key, $prop_map, true ) : $key;
|
||
|
||
if ( isset( $object->$key ) ) {
|
||
return $object->$key;
|
||
} elseif ( softoba_wc_check_post_columns( $key ) ) {
|
||
return $object->post->$key;
|
||
} else {
|
||
$object_id = 0;
|
||
$getter = $object instanceof WC_Customer ? 'get_user_meta' : 'get_post_meta';
|
||
|
||
if ( ! ! $object ) {
|
||
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id;
|
||
}
|
||
|
||
return ! ! $object_id ? $getter( $object_id, $key, true ) : null;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if ( ! function_exists( 'softoba_save_prop' ) ) {
|
||
/**
|
||
* Save prop or props.
|
||
*
|
||
* @param object $object The object.
|
||
* @param array|string $arg1 The key of the prop to set, or an array of props to set.
|
||
* @param false $arg2 The value to set, or false if you want to set an array of props.
|
||
* @param false $force_update Unused attribute.
|
||
*
|
||
* @deprecated 3.5 | use the WooCommerce CRUD directly instead.
|
||
*/
|
||
function softoba_save_prop( $object, $arg1, $arg2 = false, $force_update = false ) {
|
||
if ( ! is_array( $arg1 ) ) {
|
||
$arg1 = array(
|
||
$arg1 => $arg2,
|
||
);
|
||
}
|
||
|
||
$is_wc_data = $object instanceof WC_Data;
|
||
|
||
foreach ( $arg1 as $key => $value ) {
|
||
softoba_set_prop( $object, $key, $value );
|
||
|
||
if ( ! $is_wc_data ) {
|
||
|
||
if ( softoba_wc_check_post_columns( $key ) ) {
|
||
softoba_store_changes( $object->post, $key, $value );
|
||
} else {
|
||
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id;
|
||
|
||
update_post_meta( $object_id, $key, $value );
|
||
}
|
||
}
|
||
}
|
||
|
||
if ( $is_wc_data ) {
|
||
$object->save();
|
||
}
|
||
}
|
||
}
|
||
if ( ! function_exists( 'softoba_set_prop' ) ) {
|
||
/**
|
||
* Set prop or props.
|
||
*
|
||
* @param object $object The object.
|
||
* @param array|string $arg1 The key of the prop to set, or an array of props to set.
|
||
* @param false $arg2 The value to set, or false if you want to set an array of props.
|
||
*
|
||
* @deprecated 3.5 | use the WooCommerce CRUD directly instead.
|
||
*/
|
||
function softoba_set_prop( $object, $arg1, $arg2 = false ) {
|
||
if ( ! is_array( $arg1 ) ) {
|
||
$arg1 = array(
|
||
$arg1 => $arg2,
|
||
);
|
||
}
|
||
|
||
$prop_map = softoba_return_new_attribute_map();
|
||
$is_wc_data = $object instanceof WC_Data;
|
||
|
||
foreach ( $arg1 as $key => $value ) {
|
||
if ( $is_wc_data ) {
|
||
$key = ( array_key_exists( $key, $prop_map ) ) ? $prop_map[ $key ] : $key;
|
||
|
||
$setter = false;
|
||
if ( method_exists( $object, "set{$key}" ) ) {
|
||
$setter = "set{$key}";
|
||
} elseif ( method_exists( $object, "set_{$key}" ) ) {
|
||
$setter = "set_{$key}";
|
||
}
|
||
|
||
if ( $setter ) {
|
||
$object->$setter( $value );
|
||
} else {
|
||
$object->update_meta_data( $key, $value );
|
||
}
|
||
} else {
|
||
$key = ( in_array( $key, $prop_map, true ) ) ? array_search( $key, $prop_map, true ) : $key;
|
||
if ( ( strpos( $key, '_' ) === 0 ) ) {
|
||
$key = substr( $key, 1 );
|
||
}
|
||
|
||
if ( softoba_wc_check_post_columns( $key ) ) {
|
||
$object->post->$key = $value;
|
||
} else {
|
||
$object->$key = $value;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_return_new_attribute_map' ) ) {
|
||
/**
|
||
* Return the attribute map array.
|
||
*
|
||
* @return string[]
|
||
* @deprecated 3.5
|
||
*/
|
||
function softoba_return_new_attribute_map() {
|
||
return array(
|
||
'post_parent' => 'parent_id',
|
||
'post_title' => 'name',
|
||
'post_status' => 'status',
|
||
'post_content' => 'description',
|
||
'post_excerpt' => 'short_description',
|
||
// Orders --------------------.
|
||
'paid_date' => 'date_paid',
|
||
'_paid_date' => '_date_paid',
|
||
'completed_date' => 'date_completed',
|
||
'_completed_date' => '_date_completed',
|
||
'_order_date' => '_date_created',
|
||
'order_date' => 'date_created',
|
||
'order_total' => 'total',
|
||
'customer_user' => 'customer_id',
|
||
'_customer_user' => 'customer_id',
|
||
// Products ---------------------.
|
||
'visibility' => 'catalog_visibility',
|
||
'_visibility' => '_catalog_visibility',
|
||
'sale_price_dates_from' => 'date_on_sale_from',
|
||
'_sale_price_dates_from' => '_date_on_sale_from',
|
||
'sale_price_dates_to' => 'date_on_sale_to',
|
||
'_sale_price_dates_to' => '_date_on_sale_to',
|
||
'product_attributes' => 'attributes',
|
||
'_product_attributes' => '_attributes',
|
||
// Coupons ---------------------.
|
||
'coupon_amount' => 'amount',
|
||
'exclude_product_ids' => 'excluded_product_ids',
|
||
'exclude_product_categories' => 'excluded_product_categories',
|
||
'customer_email' => 'email_restrictions',
|
||
'expiry_date' => 'date_expires',
|
||
);
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_wc_check_post_columns' ) ) {
|
||
/**
|
||
* Check the post columns.
|
||
*
|
||
* @param string $key The key.
|
||
*
|
||
* @return bool
|
||
* @deprecated 3.5
|
||
*/
|
||
function softoba_wc_check_post_columns( $key ) {
|
||
$columns = array(
|
||
'post_author',
|
||
'post_date',
|
||
'post_date_gmt',
|
||
'post_content',
|
||
'post_title',
|
||
'post_excerpt',
|
||
'post_status',
|
||
'comment_status',
|
||
'ping_status',
|
||
'post_password',
|
||
'post_name',
|
||
'to_ping',
|
||
'pinged',
|
||
'post_modified',
|
||
'post_modified_gmt',
|
||
'post_content_filtered',
|
||
'post_parent',
|
||
'guid',
|
||
'menu_order',
|
||
'post_type',
|
||
'post_mime_type',
|
||
'comment_count',
|
||
);
|
||
|
||
return in_array( $key, $columns, true );
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_store_changes' ) ) {
|
||
/**
|
||
* Store changes
|
||
*
|
||
* @param object $object The object.
|
||
* @param string $key The key.
|
||
* @param false|mixes $value The value.
|
||
*
|
||
* @deprecated 3.5
|
||
*/
|
||
function softoba_store_changes( $object, $key, $value = false ) {
|
||
global $changed_objects;
|
||
|
||
$is_wc_data = $object instanceof WC_Data;
|
||
|
||
if ( $is_wc_data ) {
|
||
$object_reference = $object->get_id();
|
||
|
||
$changed_objects[ $object_reference ]['object'] = $object;
|
||
$changed_objects[ $object_reference ]['changes'][ $key ] = $value;
|
||
|
||
} else {
|
||
$changed_objects[ $object->ID ][ $key ] = $value;
|
||
}
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_get_request_statuses' ) ) {
|
||
function softoba_get_request_statuses() {
|
||
$request_statuses = array(
|
||
'softoba-new' => esc_html_x( 'New request', 'Request status', 'astra' ),
|
||
'softoba-processing' => esc_html_x( 'Processing', 'Request status', 'astra' ),
|
||
'softoba-on-hold' => esc_html_x( 'On hold', 'Request status', 'astra' ),
|
||
'softoba-approved' => esc_html_x( 'Approved', 'Request status', 'astra' ),
|
||
'softoba-refunded' => esc_html_x( 'Refunded', 'Request status', 'astra' ),
|
||
'softoba-rejected' => esc_html_x( 'Rejected', 'Request status', 'astra' ),
|
||
'trash' => esc_html_x( 'Request in Trash', 'Request status', 'astra' ),
|
||
);
|
||
return apply_filters( 'softoba_request_statuses', $request_statuses );
|
||
}
|
||
}
|
||
|
||
if ( ! function_exists( 'softoba_get_request_status_by_key' ) ) {
|
||
function softoba_get_request_status_by_key( $status_key ) {
|
||
$request_statuses = softoba_get_request_statuses();
|
||
return ! empty( $request_statuses[$status_key] ) ? $request_statuses[$status_key] : esc_html__( 'No status', 'astra' );
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Check if an order is completed or paid
|
||
*
|
||
* @param int $order_number The order number
|
||
* @return bool
|
||
*/
|
||
if( ! function_exists( 'softoba_is_order_completed_or_paid' ) ) {
|
||
function softoba_is_order_completed_or_paid($order_number) {
|
||
$order = wc_get_order($order_number);
|
||
|
||
if ($order) {
|
||
$order_status = $order->get_status();
|
||
|
||
if ( $order_status === 'completed' ) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// AJAX action for updating cart items
|
||
function update_cart_item_quantity() {
|
||
if (isset($_POST['item_key']) && isset($_POST['qty'])) {
|
||
$item_key = sanitize_key($_POST['item_key']);
|
||
$qty = intval($_POST['qty']);
|
||
|
||
if ($qty <= 0) {
|
||
WC()->cart->remove_cart_item($item_key);
|
||
} else {
|
||
WC()->cart->set_quantity($item_key, $qty);
|
||
}
|
||
|
||
WC()->cart->calculate_totals();
|
||
wp_send_json(array('success' => true));
|
||
} else {
|
||
wp_send_json(array('success' => false));
|
||
}
|
||
}
|
||
add_action('wp_ajax_update_cart', 'update_cart_item_quantity');
|
||
add_action('wp_ajax_nopriv_update_cart', 'update_cart_item_quantity');
|
||
|
||
|
||
// Add the softoba_static_cart_sidebar action
|
||
if (!function_exists('softoba_static_cart_sidebar')) :
|
||
function softoba_static_cart_sidebar() {
|
||
?>
|
||
<div class="softoba-overlay"></div>
|
||
<div id="cart-sidebar" class="softoba-static-sidebar">
|
||
<div class="cart-close softoba-sidebar-close">
|
||
<span class="softoba-tit-mycart softoba-sidebar-tit text-center">
|
||
<?php echo esc_html__('Item added to cart', 'astra'); ?>
|
||
</span>
|
||
<a href="javascript:void(0);" title="<?php esc_attr_e('Close', 'astra'); ?>" rel="nofollow">╳</a>
|
||
</div>
|
||
|
||
<div class="widget_shopping_cart_content">
|
||
<?php woocommerce_mini_cart(); ?>
|
||
<input type="hidden" name="softoba-mini-cart-empty-content" />
|
||
</div>
|
||
|
||
<?php if (isset($_REQUEST['softoba_cart_sidebar']) && $_REQUEST['softoba_cart_sidebar'] == 1) : ?>
|
||
<input type="hidden" name="softoba_cart_sidebar_show" value="1" />
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php
|
||
}
|
||
endif;
|
||
|
||
// Hook the action to a suitable location (e.g., wp_footer)
|
||
add_action('wp_footer', 'softoba_static_cart_sidebar');
|
||
|
||
|
||
// Add custom order status to order actions dropdown
|
||
// function softoba_add_custom_order_status_to_admin_order_list($order_statuses) {
|
||
// $order_statuses['wc-softoba-exchange-approve'] = 'Exchange Approved';
|
||
// $order_statuses['wc-softoba-exchange-request'] = 'Exchange Requested';
|
||
// return $order_statuses;
|
||
// }
|
||
// // add_filter('bulk_actions-edit-shop_order', 'softoba_add_custom_order_status_to_admin_order_list');
|
||
// add_filter('wc_order_statuses', 'softoba_add_custom_order_status_to_admin_order_list');
|
||
|
||
// // Handle order status transitions
|
||
// function softoba_handle_order_status_transitions($order_statuses) {
|
||
// $order_statuses['wc-softoba-exchange-request'] = array_merge($order_statuses['wc-softoba-exchange-request'], array('wc-softoba-exchange-approve')); // Allow transition from "Exchange Requested" to "Exchange Approved"
|
||
// return $order_statuses;
|
||
// }
|
||
// add_filter('wc_order_statuses', 'softoba_handle_order_status_transitions');
|