oont-contents/themes/old-oont-themev2/inc/wc.php
2025-02-08 15:10:23 +01:00

78 lines
No EOL
3.4 KiB
PHP

<?php
function softoba_enqueue_wc_files() {
$selectors = array(
'body.single-product form.cart:not(.in_loop)',
'body.single-product form.bundle_form',
'body.singular-product form.cart',
);
$localized = array(
'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
'actionAdd' => 'softoba_add_item_cart',
'actionRemove' => 'softoba_remove_item_cart',
'actionUpdate' => 'softoba_update_item_cart',
'loader' => apply_filters( 'oont_loader_image_url', SOFTOBA_ASSETS_URL . '/images/loader.gif' ),
'is_mobile' => wp_is_mobile(),
'form_selectors' => apply_filters( 'oont_form_selectors_filter', implode( ',', $selectors ) ),
'open_popup_selectors' => apply_filters( 'oont_open_popup_selectors', '#softoba-mini-cart' ),
'touchstart_on_suggested_products' => apply_filters( 'oont_enable_touchstart_on_suggested_products', true ),
'touchstart_on_popup_actions_buttons' => apply_filters( 'oont_enable_touchstart_on_popup_actions_buttons', 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-wc', 'oont_wc_handle', apply_filters( 'oont_frontend_script_localized_args', $localized ) );
}
add_action( 'wp_enqueue_scripts', 'softoba_enqueue_wc_files' );
/**
* Action ajax for add to cart in single product page
*
* @access public
* @since 1.0.0
* @author OBAID RAHMAN
*/
if( ! function_exists( 'add_item_cart_ajax' ) ) {
function add_item_cart_ajax() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] !== 'softoba_add_item_cart' || ! isset( $_REQUEST['product_id'] ) ) {
return;
}
$product_id = absint( $_REQUEST['product_id'] );
// Get WooCommerce error notice.
$error_notices = wc_get_notices( 'error' );
if ( $error_notices ) {
// Print notices if any!
ob_start();
foreach ( $error_notices as $value ) {
$value = ( is_array( $value ) && isset( $value['notice'] ) ) ? $value['notice'] : $value;
$value && wc_print_notice( $value, 'error' );
}
$error = ob_get_clean();
wp_send_json( array( 'error' => $error ) );
} else {
// Clear other notices.
wc_clear_notices();
// Trigger action for added to cart in AJAX.
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
WC_AJAX::get_refreshed_fragments();
}
}
add_action( 'wp_loaded', 'add_item_cart_ajax', 30 );
}
// Force load the custom mini cart template
function use_custom_mini_cart_template($template, $template_name, $template_path) {
if ($template_name === 'cart/mini-cart.php') {
$template = locate_template(array('woocommerce/cart/mini-cart.php'));
}
return $template;
}
add_filter('woocommerce_locate_template', 'use_custom_mini_cart_template', 999, 5);