| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/inc/ |
Upload File : |
<?php
/**
* AJAX endpoints for cart actions, category loading, and product search.
*/
/**
* AJAX handler for adding products to cart
*/
function matreshka_ajax_add_to_cart() {
// Check nonce for security
check_ajax_referer( 'matreshka_add_to_cart_nonce', 'nonce' );
// Get product ID and quantity
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;
$quantity = isset( $_POST['quantity'] ) ? absint( $_POST['quantity'] ) : 1;
// Validate product ID
if ( $product_id <= 0 ) {
wp_send_json_error( array(
'message' => __( 'Invalid product ID', 'matreshka' ),
) );
}
// Check if WooCommerce is active
if ( ! function_exists( 'WC' ) ) {
wp_send_json_error( array(
'message' => __( 'WooCommerce is not active', 'matreshka' ),
) );
}
// Add product to cart
$cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity );
if ( $cart_item_key ) {
WC()->cart->calculate_totals();
WC()->cart->set_session();
WC()->cart->maybe_set_cart_cookies();
// Success - return cart data
wp_send_json_success( array(
'message' => __( 'Product added to cart', 'matreshka' ),
'cart_count' => WC()->cart->get_cart_contents_count(),
'cart_total' => WC()->cart->get_total( 'edit' ),
'cart_total_html' => WC()->cart->get_total(),
'cart_hash' => WC()->cart->get_cart_hash(),
'cart_url' => wc_get_cart_url(),
'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array() ),
) );
} else {
$notices = function_exists( 'wc_get_notices' ) ? wc_get_notices( 'error' ) : array();
$message = __( 'Could not add product to cart', 'matreshka' );
if ( ! empty( $notices ) ) {
$last_notice = end( $notices );
if ( is_array( $last_notice ) && ! empty( $last_notice['notice'] ) ) {
$message = wp_strip_all_tags( $last_notice['notice'] );
} elseif ( is_string( $last_notice ) ) {
$message = wp_strip_all_tags( $last_notice );
}
}
// Error adding to cart
wp_send_json_error( array(
'message' => $message,
) );
}
}
add_action( 'wp_ajax_matreshka_add_to_cart', 'matreshka_ajax_add_to_cart' );
add_action( 'wp_ajax_nopriv_matreshka_add_to_cart', 'matreshka_ajax_add_to_cart' );
/**
* AJAX handler for getting cart count
*/
function matreshka_ajax_get_cart_count() {
// Check nonce for security
check_ajax_referer( 'matreshka_add_to_cart_nonce', 'nonce' );
// Check if WooCommerce is active
if ( ! function_exists( 'WC' ) ) {
wp_send_json_error( array(
'message' => __( 'WooCommerce is not active', 'matreshka' ),
) );
}
// Return cart count and total
wp_send_json_success( array(
'cart_count' => WC()->cart->get_cart_contents_count(),
'cart_total' => WC()->cart->get_total( 'edit' ),
'cart_total_html' => WC()->cart->get_total(),
) );
}
add_action( 'wp_ajax_matreshka_get_cart_count', 'matreshka_ajax_get_cart_count' );
add_action( 'wp_ajax_nopriv_matreshka_get_cart_count', 'matreshka_ajax_get_cart_count' );
/**
* AJAX handler for checking if product is in cart
*/
function matreshka_ajax_is_product_in_cart() {
// Check nonce for security
check_ajax_referer( 'matreshka_add_to_cart_nonce', 'nonce' );
// Check if WooCommerce is active
if ( ! function_exists( 'WC' ) ) {
wp_send_json_error( array(
'message' => __( 'WooCommerce is not active', 'matreshka' ),
) );
}
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;
if ( ! $product_id ) {
wp_send_json_error( array(
'message' => __( 'Invalid product ID', 'matreshka' ),
) );
}
// Check if product is in cart
$in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
$in_cart = true;
break;
}
}
// Return result
wp_send_json_success( array(
'in_cart' => $in_cart,
'cart_url' => wc_get_cart_url(),
) );
}
add_action( 'wp_ajax_matreshka_is_product_in_cart', 'matreshka_ajax_is_product_in_cart' );
add_action( 'wp_ajax_nopriv_matreshka_is_product_in_cart', 'matreshka_ajax_is_product_in_cart' );
/**
* AJAX handler for getting product categories
*/
function matreshka_ajax_get_categories() {
// Check nonce for security
check_ajax_referer( 'matreshka_add_to_cart_nonce', 'nonce' );
// Check if WooCommerce is active
if ( ! function_exists( 'WC' ) ) {
wp_send_json_error( array(
'message' => __( 'WooCommerce is not active', 'matreshka' ),
) );
}
// Get product categories
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'pad_counts' => true,
'parent' => 0, // Only top-level categories
) );
if ( is_wp_error( $categories ) ) {
wp_send_json_error( array(
'message' => __( 'Error loading categories', 'matreshka' ),
) );
}
$categories_data = array();
foreach ( $categories as $category ) {
// Skip Misc category
if ( $category->slug === 'misc' ) {
continue;
}
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
// Get category color from ACF or meta
$color = function_exists('get_field') ? get_field('category_color', 'product_cat_' . $category->term_id) : '';
if ( ! $color ) {
$color = '#f5f5f5'; // Default color
}
$categories_data[] = array(
'id' => $category->term_id,
'name' => $category->name,
'url' => get_term_link( $category ),
'image' => $image ? $image : '',
'color' => $color,
);
}
wp_send_json_success( array(
'categories' => $categories_data,
) );
}
add_action( 'wp_ajax_matreshka_get_categories', 'matreshka_ajax_get_categories' );
add_action( 'wp_ajax_nopriv_matreshka_get_categories', 'matreshka_ajax_get_categories' );
/**
* AJAX handler for searching products
*/
function matreshka_ajax_search_products() {
// Check nonce for security
check_ajax_referer( 'matreshka_add_to_cart_nonce', 'nonce' );
// Check if WooCommerce is active
if ( ! function_exists( 'WC' ) ) {
wp_send_json_error( array(
'message' => __( 'WooCommerce is not active', 'matreshka' ),
) );
}
$query = isset( $_POST['query'] ) ? sanitize_text_field( $_POST['query'] ) : '';
if ( empty( $query ) ) {
wp_send_json_error( array(
'message' => __( 'Empty search query', 'matreshka' ),
) );
}
// Search products
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
's' => $query,
'post_status' => 'publish',
);
$products_query = new WP_Query( $args );
$products_data = array();
if ( $products_query->have_posts() ) {
while ( $products_query->have_posts() ) {
$products_query->the_post();
$product = wc_get_product( get_the_ID() );
if ( ! $product ) {
continue;
}
// Get product category
$categories = wp_get_post_terms( get_the_ID(), 'product_cat' );
$category_name = ! empty( $categories ) ? $categories[0]->name : '';
// Get product image
$image = wp_get_attachment_image_url( $product->get_image_id(), 'thumbnail' );
if ( ! $image ) {
$image = wc_placeholder_img_src();
}
$products_data[] = array(
'id' => $product->get_id(),
'name' => $product->get_name(),
'url' => get_permalink( $product->get_id() ),
'image' => $image,
'category' => $category_name,
'price' => $product->get_price_html(),
);
}
wp_reset_postdata();
}
wp_send_json_success( array(
'products' => $products_data,
) );
}
add_action( 'wp_ajax_matreshka_search_products', 'matreshka_ajax_search_products' );
add_action( 'wp_ajax_nopriv_matreshka_search_products', 'matreshka_ajax_search_products' );