260 lines
11 KiB
PHP
260 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* OONT THEME Theme functions and definitions
|
|
*
|
|
* @link OBtps://developer.wordpress.org/themes/basics/theme-functions/
|
|
*
|
|
* @package OONT THEME
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
/**
|
|
* Define Constants
|
|
*/
|
|
define( 'CHILD_THEME_OONT_THEME_VERSION', '1.4.6' );
|
|
define( 'SOFTOBA_RETURN_POST_TYPE', 'return_request' );
|
|
define( 'SOFTOBA_EXCHANGE_POST_TYPE', 'exchange_request' );
|
|
define('SOFTOBA_THEME_DIR', get_stylesheet_directory());
|
|
define('OB_CHILD_THEME_URI', get_stylesheet_directory());
|
|
define('OB_CHILD_STYLE_URI', get_stylesheet_directory_uri());
|
|
define('OB_THEME_CLASSES', OB_CHILD_THEME_URI . '/inc/classes');
|
|
define('SOFTOBA_TEMPLATE_PATH', OB_CHILD_THEME_URI . '/inc/templates/');
|
|
define('SOFTOBA_ASSETS_URL', OB_CHILD_THEME_URI . '/assets');
|
|
|
|
// FILE CONSTANT
|
|
$wp_upload_dir = wp_upload_dir ();
|
|
! defined ( 'SOFTOBA_UPLOADS_DIR' ) && define ( 'SOFTOBA_UPLOADS_DIR', $wp_upload_dir[ 'basedir' ] . '/softoba/' );
|
|
! defined ( 'SOFTOBA_UPLOADS_URL' ) && define ( 'SOFTOBA_UPLOADS_URL', $wp_upload_dir[ 'baseurl' ] . '/softoba/' );
|
|
! defined ( 'SOFTOBA_ONE_KILOBYTE_IN_BYTES' ) && define ( 'SOFTOBA_ONE_KILOBYTE_IN_BYTES', 1024 );
|
|
! defined ( 'SOFTOBA_UPLOAD_ERR_ALL_FILES_OK' ) && define ( 'SOFTOBA_UPLOAD_ERR_ALL_FILES_OK', 20 );
|
|
! defined ( 'SOFTOBA_UPLOAD_ERR_NOT_A_IMAGE' ) && define ( 'SOFTOBA_UPLOAD_ERR_NOT_A_IMAGE', 21 );
|
|
! defined ( 'SOFTOBA_UPLOAD_ERR_WRONG_IMAGE_FORMAT' ) && define ( 'SOFTOBA_UPLOAD_ERR_WRONG_IMAGE_FORMAT', 22 );
|
|
|
|
|
|
// Disable srcset and sizes attributes
|
|
add_filter('wp_calculate_image_srcset', '__return_false');
|
|
add_filter('wp_calculate_image_sizes', '__return_false');
|
|
|
|
/**
|
|
* Enqueue styles
|
|
*/
|
|
function softoba_enqueue_styles() {
|
|
|
|
wp_enqueue_style( 'oont-theme-css', OB_CHILD_STYLE_URI . '/style.css', array('astra-theme-css'), CHILD_THEME_OONT_THEME_VERSION, 'all' );
|
|
wp_enqueue_style( 'oont-font-css', '//fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;500;600&display=swap', array(), null, 'all' );
|
|
wp_enqueue_style( 'oont-custom-css', OB_CHILD_STYLE_URI . '/assets/css/custom.css', array('astra-theme-css'), CHILD_THEME_OONT_THEME_VERSION, 'all' );
|
|
wp_enqueue_style( 'oont-alaskardev-css', OB_CHILD_STYLE_URI . '/assets/css/alaskardev.css', array('astra-theme-css'), CHILD_THEME_OONT_THEME_VERSION, 'all' );
|
|
|
|
|
|
|
|
|
|
// Enqueue JS
|
|
wp_enqueue_script( 'oont-script', OB_CHILD_STYLE_URI . '/assets/js/oont.js', array( 'jquery' ), CHILD_THEME_OONT_THEME_VERSION, true );
|
|
wp_enqueue_script( 'oont-wc', OB_CHILD_STYLE_URI . '/assets/js/wc.js', array( 'jquery' ), CHILD_THEME_OONT_THEME_VERSION, true );
|
|
wp_localize_script('oont-script', 'softoba_objects', array(
|
|
'ajaxurl' => admin_url('admin-ajax.php'),
|
|
'confirmText' => esc_html__('Are your sure?', 'astra'),
|
|
));
|
|
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'softoba_enqueue_styles' );
|
|
|
|
function softoba_admin_enqueue_scipts() {
|
|
wp_enqueue_style( 'oont-admin-css', OB_CHILD_STYLE_URI . '/assets/css/admin/oont-admin.css', array(), CHILD_THEME_OONT_THEME_VERSION, 'all' );
|
|
// Admin scripts
|
|
wp_enqueue_script( 'oont-admin-script', OB_CHILD_STYLE_URI . '/assets/js/admin/oont-admin.js', array( 'jquery', 'jquery-blockui', 'jquery-tiptip', 'accounting' ), CHILD_THEME_OONT_THEME_VERSION, true );
|
|
$params = array(
|
|
'i18n_do_refund' => esc_html__( 'Are you sure you want to process this refund? This action cannot be undone.',
|
|
'astra' ),
|
|
'ajax_url' => admin_url( 'admin-ajax.php', apply_filters( 'softoba_ajax_url_scheme_backend', '' ) ),
|
|
'order_item_nonce' => wp_create_nonce( 'order-item' ),
|
|
'create_coupon_nonce' => wp_create_nonce( 'create-coupon' ),
|
|
'change_status_nonce' => wp_create_nonce( 'change-status' ),
|
|
'softoba_submit_message' => wp_create_nonce( 'softoba-submit-message' ),
|
|
'softoba_update_messages' => wp_create_nonce( 'softoba-update-messages' ),
|
|
'i18n_mon_decimal_error' => sprintf( esc_html__( 'Please enter in monetary decimal format (%s) without thousand separators and
|
|
currency symbols.', 'astra' ), wc_get_price_decimal_separator() ),
|
|
'i18n_country_iso_error' => esc_html__( 'Please enter in country code with two capital letters.', 'astra' ),
|
|
'i18_sale_less_than_regular_error' => esc_html__( 'Please enter in a lower value than the regular price.',
|
|
'astra' ),
|
|
'mon_decimal_point' => wc_get_price_decimal_separator(),
|
|
'currency_format_num_decimals' => wc_get_price_decimals(),
|
|
'currency_format_symbol' => get_woocommerce_currency_symbol( 'KWD' ),
|
|
'currency_format_decimal_sep' => esc_attr( wc_get_price_decimal_separator() ),
|
|
'currency_format_thousand_sep' => esc_attr( wc_get_price_thousand_separator() ),
|
|
'currency_format' => esc_attr( str_replace( array( '%1$s', '%2$s' ), array( '%s', '%v' ), get_woocommerce_price_format() ) ), // For accounting JS
|
|
'create_coupon' => esc_html_x( 'Do you want to offer a coupon for:', 'Money amount after semicolon', 'astra' ),
|
|
'reject' => esc_html__( 'Are you sure you want to reject this request? This action cannot be undone.', 'astra' ),
|
|
'close_request' => esc_html__( "Are you sure you want to close this request? This action cannot be undone. Messages system will be closed and you won't be able to change the request status anymore.", 'astra' ),
|
|
'fill_fields' => esc_html__( 'Please enter a message', 'astra' ),
|
|
'success_message' => esc_html__( 'Message submitted successfully', 'astra' )
|
|
);
|
|
wp_localize_script('oont-admin-script', 'softoba_admin_objects', $params );
|
|
}
|
|
add_action( 'admin_enqueue_scripts', 'softoba_admin_enqueue_scipts' );
|
|
|
|
if ( class_exists( 'WooCommerce' ) ) {
|
|
// Include helpers funciton
|
|
require_once OB_CHILD_THEME_URI . '/inc/helpers.php';
|
|
require_once OB_CHILD_THEME_URI . '/inc/wc.php';
|
|
|
|
/**
|
|
* Load Theme classes
|
|
*/
|
|
if ( ! function_exists( 'softoba_load_classes' ) ) {
|
|
function softoba_load_classes() {
|
|
$classes = array(
|
|
'Ajax.php',
|
|
'Theme.php',
|
|
'Registry.php',
|
|
'Cpt.php',
|
|
'Return_request.php',
|
|
'Exchange_request.php',
|
|
'Wc_Product_Extend.php'
|
|
);
|
|
|
|
foreach ( $classes as $class ) {
|
|
require OB_THEME_CLASSES . DIRECTORY_SEPARATOR . $class;
|
|
}
|
|
}
|
|
}
|
|
softoba_load_classes();
|
|
|
|
new SOFTOBA_Theme();
|
|
|
|
}
|
|
|
|
// function softoba_enforce_underscore_before_meta_keys($meta_key, $object_id, $meta_type, $_meta_key, $single) {
|
|
// // Check if the object ID is for the exchange_request post type
|
|
// if ('exchange_request' === get_post_type($object_id)) {
|
|
// // Ensure the meta key starts with an underscore
|
|
// if (strpos($meta_key, '_') !== 0) {
|
|
// $meta_key = '_' . $meta_key;
|
|
// }
|
|
// }
|
|
// return $meta_key;
|
|
// }
|
|
// add_filter('update_post_metadata', 'softoba_enforce_underscore_before_meta_keys', 10, 5);
|
|
|
|
|
|
// Set default product type to "simple" when creating or updating an exchange request
|
|
function softoba_ex_set_default_product_type($post_id) {
|
|
if ('exchange_request' == get_post_type($post_id)) {
|
|
// Add or update custom meta key with an underscore prefix
|
|
if (get_post_meta($post_id, '_price', true) === '') {
|
|
add_post_meta($post_id, '_price', '3', true);
|
|
add_post_meta($post_id, '_product_type', 'simple', true);
|
|
} else {
|
|
update_post_meta($post_id, '_price', '3');
|
|
update_post_meta($post_id, '_product_type', 'simple');
|
|
}
|
|
}
|
|
}
|
|
add_action('save_post', 'softoba_ex_set_default_product_type');
|
|
|
|
add_filter('woocommerce_get_price','soba_woocommerce_get_price',20,2);
|
|
function soba_woocommerce_get_price($price, $post){
|
|
if ($post->post->post_type === SOFTOBA_EXCHANGE_POST_TYPE ){
|
|
$price = get_post_meta($post->id, "exchange_price", true);
|
|
$price = isset( $price ) ? ( floatval( $price ) ) : 3;
|
|
}
|
|
return $price;
|
|
}
|
|
|
|
add_filter('woocommerce_product_get_price', 'my_cpt_woocommerce_product_get_price', 10, 2 );
|
|
function my_cpt_woocommerce_product_get_price( $price, $product ) {
|
|
|
|
$product_id = $product->get_id();
|
|
|
|
if ( get_post_type( $product_id ) === SOFTOBA_EXCHANGE_POST_TYPE ) {
|
|
$price = get_post_meta( $product_id, 'exchange_price', true );
|
|
$price = isset( $price ) ? ( floatval( $price ) ) : 0;
|
|
}
|
|
|
|
return $price;
|
|
}
|
|
// adding custom search result page.
|
|
function custom_search_redirect() {
|
|
// Check if the current page is a search results page with no results
|
|
if (is_search() && !have_posts()) {
|
|
// Replace 'your-no-results-page-slug' with the slug of your "no results found" page
|
|
$no_results_page = home_url('/search-result/');
|
|
wp_redirect($no_results_page);
|
|
exit;
|
|
}
|
|
}
|
|
add_action('template_redirect', 'custom_search_redirect');
|
|
|
|
add_action( 'woocommerce_before_add_to_cart_quantity', 'woo_show_some_text2', 20 );
|
|
function woo_show_some_text2() {
|
|
if(get_field('dates')){
|
|
echo '<div class="delivery">The delivery will start on ';
|
|
echo the_field('dates').'</div>';
|
|
}}
|
|
|
|
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
|
|
function woocommerce_custom_single_add_to_cart_text() {
|
|
if(!empty(get_field('dates'))){
|
|
return __( 'Pre Order', 'woocommerce' );
|
|
}
|
|
else{
|
|
return __( 'ADD TO CART', 'woocommerce' );
|
|
}
|
|
}
|
|
|
|
function product_image_slider_shortcode($atts) {
|
|
$atts = shortcode_atts([], $atts);
|
|
$product_id = get_the_ID();
|
|
|
|
// Ensure WooCommerce is active and the current post is a product.
|
|
if (!function_exists('wc_get_product') || get_post_type($product_id) !== 'product') {
|
|
return '<p>This is not a valid product.</p>';
|
|
}
|
|
|
|
$product = wc_get_product($product_id);
|
|
if (!$product) {
|
|
return '<p>Invalid product.</p>';
|
|
}
|
|
|
|
// Get gallery image IDs only (excluding the featured image).
|
|
$gallery_image_ids = $product->get_gallery_image_ids();
|
|
|
|
// Return early if no gallery images exist.
|
|
if (empty($gallery_image_ids)) {
|
|
return '<p>No gallery images available for this product.</p>';
|
|
}
|
|
|
|
// Check if there is more than one gallery image
|
|
$is_slider = count($gallery_image_ids) > 1;
|
|
|
|
$product_link = get_permalink($product_id); // Get the product link
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="product-slider-container">
|
|
<div class="product-image-slider">
|
|
<?php foreach ($gallery_image_ids as $image_id) : ?>
|
|
<div class="slider-item">
|
|
<a href="<?php echo esc_url($product_link); ?>">
|
|
<?php echo wp_get_attachment_image($image_id, 'large'); ?>
|
|
</a>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php if ($is_slider) : ?>
|
|
<button class="slider-prev"><i class="fa-solid fa-chevron-left"></i></button>
|
|
<button class="slider-next"><i class="fa-solid fa-chevron-right"></i></button>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
add_shortcode('product_image_slider', 'product_image_slider_shortcode');
|
|
|
|
|
|
|
|
function enqueue_slider_assets() {
|
|
wp_enqueue_style('product-slider-style', get_stylesheet_directory_uri() . '/assets/css/product-slider.css',[], CHILD_THEME_OONT_THEME_VERSION);
|
|
wp_enqueue_script('product-slider-script', get_stylesheet_directory_uri() . '/assets/js/product-slider.js', ['jquery'], CHILD_THEME_OONT_THEME_VERSION, true);
|
|
}
|
|
add_action('wp_enqueue_scripts', 'enqueue_slider_assets');
|