woocommerce-setup.php

I’ll build this as a strong inc/woocommerce/woocommerce-setup.php module for your RX theme: WooCommerce support, gallery features, shop layout controls, wrappers, product image settings, cart fragments, notices, breadcrumbs, tabs, related/upsell/cross-sell controls, schema-safe tweaks, and performance-friendly cleanup.

Below is a large advanced inc/woocommerce/woocommerce-setup.php file for your RX theme.

Create this file:

mkdir -p inc/woocommerce
touch inc/woocommerce/woocommerce-setup.php

Then include it from your functions.php:

require_once get_template_directory() . '/inc/woocommerce/woocommerce-setup.php';

Now paste this full code inside:

<?php
/**
 * RX Theme WooCommerce Setup
 *
 * File: inc/woocommerce/woocommerce-setup.php
 *
 * Advanced WooCommerce integration for RX Theme.
 *
 * Features:
 * - WooCommerce theme support
 * - Product gallery zoom/lightbox/slider
 * - Shop loop layout controls
 * - Product grid columns
 * - Products per page
 * - WooCommerce wrappers
 * - Breadcrumb customization
 * - Product tabs customization
 * - Related products, upsells, cross-sells controls
 * - Sale badge customization
 * - Cart fragments optimization
 * - WooCommerce CSS control
 * - Mini cart support
 * - AJAX add to cart support
 * - Checkout field customization
 * - Account menu customization
 * - Single product layout hooks
 * - Performance cleanup
 * - Accessibility-friendly markup helpers
 *
 * @package RX_Theme
 */

defined( 'ABSPATH' ) || exit;

if ( ! defined( 'RX_THEME_VERSION' ) ) {
	define( 'RX_THEME_VERSION', wp_get_theme()->get( 'Version' ) ?: '1.0.0' );
}

/**
 * Check if WooCommerce is active.
 *
 * @return bool
 */
function rx_is_woocommerce_active() {
	return class_exists( 'WooCommerce' );
}

/**
 * Stop loading WooCommerce functions if WooCommerce is not active.
 */
if ( ! rx_is_woocommerce_active() ) {
	return;
}

/**
 * Main WooCommerce setup.
 */
function rx_woocommerce_setup() {

	/**
	 * Enable WooCommerce theme support.
	 */
	add_theme_support(
		'woocommerce',
		array(
			'thumbnail_image_width' => 450,
			'single_image_width'    => 700,
			'product_grid'          => array(
				'default_rows'    => 4,
				'min_rows'        => 1,
				'max_rows'        => 12,
				'default_columns' => 4,
				'min_columns'     => 1,
				'max_columns'     => 6,
			),
		)
	);

	/**
	 * Product gallery features.
	 */
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'rx_woocommerce_setup' );

/**
 * Disable default WooCommerce stylesheet if you want full custom design.
 *
 * Return false to remove WooCommerce CSS.
 * Return true to keep it.
 *
 * @return bool
 */
function rx_woocommerce_keep_default_styles() {
	return true;
}

/**
 * Control WooCommerce default CSS.
 */
function rx_woocommerce_enqueue_styles( $styles ) {

	if ( rx_woocommerce_keep_default_styles() ) {
		return $styles;
	}

	unset( $styles['woocommerce-general'] );
	unset( $styles['woocommerce-layout'] );
	unset( $styles['woocommerce-smallscreen'] );

	return $styles;
}
add_filter( 'woocommerce_enqueue_styles', 'rx_woocommerce_enqueue_styles' );

/**
 * Enqueue WooCommerce-specific RX theme assets.
 */
function rx_woocommerce_assets() {

	if ( ! rx_is_woocommerce_active() ) {
		return;
	}

	$theme_uri = get_template_directory_uri();
	$theme_dir = get_template_directory();

	$css_file = '/assets/css/woocommerce.css';
	$js_file  = '/assets/js/woocommerce.js';

	if ( file_exists( $theme_dir . $css_file ) ) {
		wp_enqueue_style(
			'rx-woocommerce',
			$theme_uri . $css_file,
			array(),
			filemtime( $theme_dir . $css_file )
		);
	}

	if ( file_exists( $theme_dir . $js_file ) ) {
		wp_enqueue_script(
			'rx-woocommerce',
			$theme_uri . $js_file,
			array( 'jquery' ),
			filemtime( $theme_dir . $js_file ),
			true
		);

		wp_localize_script(
			'rx-woocommerce',
			'rxWooData',
			array(
				'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
				'cartUrl'        => wc_get_cart_url(),
				'checkoutUrl'    => wc_get_checkout_url(),
				'currencySymbol' => get_woocommerce_currency_symbol(),
				'isCart'         => is_cart(),
				'isCheckout'     => is_checkout(),
				'isProduct'      => is_product(),
				'nonce'          => wp_create_nonce( 'rx_woocommerce_nonce' ),
			)
		);
	}
}
add_action( 'wp_enqueue_scripts', 'rx_woocommerce_assets', 20 );

/**
 * Add body classes for WooCommerce pages.
 */
function rx_woocommerce_body_classes( $classes ) {

	if ( is_woocommerce() ) {
		$classes[] = 'rx-woocommerce-page';
	}

	if ( is_shop() ) {
		$classes[] = 'rx-shop-page';
	}

	if ( is_product() ) {
		$classes[] = 'rx-single-product-page';
	}

	if ( is_product_category() ) {
		$classes[] = 'rx-product-category-page';
	}

	if ( is_cart() ) {
		$classes[] = 'rx-cart-page';
	}

	if ( is_checkout() ) {
		$classes[] = 'rx-checkout-page';
	}

	if ( is_account_page() ) {
		$classes[] = 'rx-account-page';
	}

	return $classes;
}
add_filter( 'body_class', 'rx_woocommerce_body_classes' );

/**
 * Remove default WooCommerce wrappers.
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );

/**
 * RX opening WooCommerce wrapper.
 */
function rx_woocommerce_wrapper_start() {
	?>
	<main id="primary" class="site-main rx-woo-main">
		<div class="rx-container rx-woo-container">
			<div class="rx-woo-content">
	<?php
}
add_action( 'woocommerce_before_main_content', 'rx_woocommerce_wrapper_start', 5 );

/**
 * RX closing WooCommerce wrapper.
 */
function rx_woocommerce_wrapper_end() {
	?>
			</div>
		</div>
	</main>
	<?php
}
add_action( 'woocommerce_after_main_content', 'rx_woocommerce_wrapper_end', 50 );

/**
 * Customize WooCommerce breadcrumb.
 */
function rx_woocommerce_breadcrumb_defaults( $defaults ) {

	$defaults['delimiter']   = '<span class="rx-woo-breadcrumb-separator"> / </span>';
	$defaults['wrap_before'] = '<nav class="rx-woo-breadcrumb woocommerce-breadcrumb" aria-label="' . esc_attr__( 'Breadcrumb', 'rx-theme' ) . '">';
	$defaults['wrap_after']  = '</nav>';
	$defaults['before']      = '<span class="rx-woo-breadcrumb-item">';
	$defaults['after']       = '</span>';
	$defaults['home']        = esc_html__( 'Home', 'rx-theme' );

	return $defaults;
}
add_filter( 'woocommerce_breadcrumb_defaults', 'rx_woocommerce_breadcrumb_defaults' );

/**
 * Move breadcrumb priority.
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
add_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 8 );

/**
 * Shop page title wrapper start.
 */
function rx_woocommerce_shop_header_start() {

	if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
		echo '<header class="rx-woo-archive-header">';
	}
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_shop_header_start', 1 );

/**
 * Shop page title wrapper end.
 */
function rx_woocommerce_shop_header_end() {

	if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
		echo '</header>';
	}
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_shop_header_end', 12 );

/**
 * Products per page.
 */
function rx_woocommerce_products_per_page() {
	return 12;
}
add_filter( 'loop_shop_per_page', 'rx_woocommerce_products_per_page', 20 );

/**
 * Product grid columns.
 */
function rx_woocommerce_loop_columns() {
	return 4;
}
add_filter( 'loop_shop_columns', 'rx_woocommerce_loop_columns' );

/**
 * Related products arguments.
 */
function rx_woocommerce_related_products_args( $args ) {

	$args['posts_per_page'] = 4;
	$args['columns']        = 4;
	$args['orderby']        = 'rand';

	return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'rx_woocommerce_related_products_args' );

/**
 * Upsell products arguments.
 */
function rx_woocommerce_upsell_display_args() {
	woocommerce_upsell_display( 4, 4 );
}
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'rx_woocommerce_upsell_display_args', 15 );

/**
 * Cross-sell products arguments.
 */
function rx_woocommerce_cross_sells_columns( $columns ) {
	return 4;
}
add_filter( 'woocommerce_cross_sells_columns', 'rx_woocommerce_cross_sells_columns' );

function rx_woocommerce_cross_sells_total( $total ) {
	return 4;
}
add_filter( 'woocommerce_cross_sells_total', 'rx_woocommerce_cross_sells_total' );

/**
 * Product thumbnail wrapper start.
 */
function rx_woocommerce_product_thumbnail_wrap_start() {
	echo '<div class="rx-product-thumbnail-wrap">';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_product_thumbnail_wrap_start', 5 );

/**
 * Product thumbnail wrapper end.
 */
function rx_woocommerce_product_thumbnail_wrap_end() {
	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_product_thumbnail_wrap_end', 20 );

/**
 * Product content wrapper start.
 */
function rx_woocommerce_product_content_wrap_start() {
	echo '<div class="rx-product-content">';
}
add_action( 'woocommerce_shop_loop_item_title', 'rx_woocommerce_product_content_wrap_start', 5 );

/**
 * Product content wrapper end.
 */
function rx_woocommerce_product_content_wrap_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_product_content_wrap_end', 20 );

/**
 * Customize product title markup in loop.
 */
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

function rx_woocommerce_template_loop_product_title() {
	echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title rx-product-title' ) ) . '">';
	echo esc_html( get_the_title() );
	echo '</h2>';
}
add_action( 'woocommerce_shop_loop_item_title', 'rx_woocommerce_template_loop_product_title', 10 );

/**
 * Add product category above title.
 */
function rx_woocommerce_loop_product_category() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$terms = get_the_terms( $product->get_id(), 'product_cat' );

	if ( empty( $terms ) || is_wp_error( $terms ) ) {
		return;
	}

	$term = array_shift( $terms );

	if ( ! $term ) {
		return;
	}

	echo '<div class="rx-product-category">';
	echo '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
	echo '</div>';
}
add_action( 'woocommerce_shop_loop_item_title', 'rx_woocommerce_loop_product_category', 8 );

/**
 * Add short product excerpt in loop.
 */
function rx_woocommerce_loop_short_excerpt() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$excerpt = get_the_excerpt( $product->get_id() );

	if ( empty( $excerpt ) ) {
		return;
	}

	echo '<div class="rx-product-loop-excerpt">';
	echo esc_html( wp_trim_words( $excerpt, 15, '...' ) );
	echo '</div>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'rx_woocommerce_loop_short_excerpt', 7 );

/**
 * Customize sale badge.
 */
function rx_woocommerce_sale_flash( $html, $post, $product ) {

	if ( ! $product instanceof WC_Product ) {
		return $html;
	}

	if ( $product->is_type( 'variable' ) ) {
		return '<span class="onsale rx-sale-badge">' . esc_html__( 'Sale', 'rx-theme' ) . '</span>';
	}

	$regular_price = (float) $product->get_regular_price();
	$sale_price    = (float) $product->get_sale_price();

	if ( $regular_price > 0 && $sale_price > 0 ) {
		$percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );

		if ( $percentage > 0 ) {
			return '<span class="onsale rx-sale-badge">-' . esc_html( $percentage ) . '%</span>';
		}
	}

	return '<span class="onsale rx-sale-badge">' . esc_html__( 'Sale', 'rx-theme' ) . '</span>';
}
add_filter( 'woocommerce_sale_flash', 'rx_woocommerce_sale_flash', 10, 3 );

/**
 * Add new badge for recently published products.
 */
function rx_woocommerce_new_product_badge() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$post_date = get_the_time( 'U', $product->get_id() );
	$days      = 14;
	$newness   = $days * DAY_IN_SECONDS;

	if ( ( time() - $post_date ) < $newness ) {
		echo '<span class="rx-new-badge">' . esc_html__( 'New', 'rx-theme' ) . '</span>';
	}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_new_product_badge', 12 );

/**
 * Add stock badge in loop.
 */
function rx_woocommerce_stock_badge_loop() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	if ( ! $product->is_in_stock() ) {
		echo '<span class="rx-stock-badge rx-stock-out">' . esc_html__( 'Out of stock', 'rx-theme' ) . '</span>';
		return;
	}

	if ( $product->managing_stock() && $product->get_stock_quantity() <= 5 && $product->get_stock_quantity() > 0 ) {
		echo '<span class="rx-stock-badge rx-stock-low">' . esc_html__( 'Low stock', 'rx-theme' ) . '</span>';
	}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_stock_badge_loop', 13 );

/**
 * Change add to cart button classes.
 */
function rx_woocommerce_loop_add_to_cart_args( $args, $product ) {

	if ( isset( $args['class'] ) ) {
		$args['class'] .= ' rx-button rx-add-to-cart-button';
	} else {
		$args['class'] = 'rx-button rx-add-to-cart-button';
	}

	return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'rx_woocommerce_loop_add_to_cart_args', 10, 2 );

/**
 * Add wishlist placeholder button.
 *
 * This does not connect to a wishlist plugin by default.
 * You can connect it later with YITH, TI Wishlist, or custom AJAX.
 */
function rx_woocommerce_wishlist_placeholder_button() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	echo '<button type="button" class="rx-wishlist-button" data-product-id="' . esc_attr( $product->get_id() ) . '" aria-label="' . esc_attr__( 'Add to wishlist', 'rx-theme' ) . '">';
	echo '<span aria-hidden="true">♡</span>';
	echo '</button>';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_wishlist_placeholder_button', 12 );

/**
 * Add quick view placeholder button.
 */
function rx_woocommerce_quick_view_placeholder_button() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	echo '<button type="button" class="rx-quick-view-button" data-product-id="' . esc_attr( $product->get_id() ) . '" aria-label="' . esc_attr__( 'Quick view', 'rx-theme' ) . '">';
	echo esc_html__( 'Quick View', 'rx-theme' );
	echo '</button>';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_quick_view_placeholder_button', 13 );

/**
 * Single product summary wrapper start.
 */
function rx_woocommerce_single_summary_wrap_start() {
	echo '<div class="rx-single-product-summary-inner">';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_summary_wrap_start', 1 );

/**
 * Single product summary wrapper end.
 */
function rx_woocommerce_single_summary_wrap_end() {
	echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_summary_wrap_end', 99 );

/**
 * Add brand/category line on single product.
 */
function rx_woocommerce_single_product_meta_top() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$categories = wc_get_product_category_list( $product->get_id(), ', ' );

	if ( $categories ) {
		echo '<div class="rx-single-product-category">';
		echo wp_kses_post( $categories );
		echo '</div>';
	}
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_product_meta_top', 4 );

/**
 * Add product trust badges after add to cart.
 */
function rx_woocommerce_single_trust_badges() {
	?>
	<div class="rx-product-trust-badges">
		<div class="rx-trust-badge">
			<span class="rx-trust-icon" aria-hidden="true"></span>
			<span><?php esc_html_e( 'Secure checkout', 'rx-theme' ); ?></span>
		</div>
		<div class="rx-trust-badge">
			<span class="rx-trust-icon" aria-hidden="true"></span>
			<span><?php esc_html_e( 'Easy return policy', 'rx-theme' ); ?></span>
		</div>
		<div class="rx-trust-badge">
			<span class="rx-trust-icon" aria-hidden="true"></span>
			<span><?php esc_html_e( 'Quality checked product', 'rx-theme' ); ?></span>
		</div>
	</div>
	<?php
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_trust_badges', 35 );

/**
 * Product SKU availability section.
 */
function rx_woocommerce_single_stock_sku_box() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	echo '<div class="rx-product-stock-sku-box">';

	if ( wc_product_sku_enabled() && $product->get_sku() ) {
		echo '<div class="rx-product-sku">';
		echo '<strong>' . esc_html__( 'SKU:', 'rx-theme' ) . '</strong> ';
		echo esc_html( $product->get_sku() );
		echo '</div>';
	}

	echo '<div class="rx-product-availability">';
	echo '<strong>' . esc_html__( 'Availability:', 'rx-theme' ) . '</strong> ';
	echo wp_kses_post( wc_get_stock_html( $product ) );
	echo '</div>';

	echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_stock_sku_box', 25 );

/**
 * Customize product tabs.
 */
function rx_woocommerce_product_tabs( $tabs ) {

	if ( isset( $tabs['description'] ) ) {
		$tabs['description']['title']  = esc_html__( 'Description', 'rx-theme' );
		$tabs['description']['priority'] = 10;
	}

	if ( isset( $tabs['additional_information'] ) ) {
		$tabs['additional_information']['title']    = esc_html__( 'More Information', 'rx-theme' );
		$tabs['additional_information']['priority'] = 20;
	}

	if ( isset( $tabs['reviews'] ) ) {
		$tabs['reviews']['title']    = esc_html__( 'Reviews', 'rx-theme' );
		$tabs['reviews']['priority'] = 30;
	}

	$tabs['rx_shipping'] = array(
		'title'    => esc_html__( 'Shipping & Returns', 'rx-theme' ),
		'priority' => 40,
		'callback' => 'rx_woocommerce_shipping_returns_tab',
	);

	return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'rx_woocommerce_product_tabs' );

/**
 * Shipping and returns custom tab content.
 */
function rx_woocommerce_shipping_returns_tab() {
	?>
	<div class="rx-product-tab-content rx-shipping-returns-tab">
		<h2><?php esc_html_e( 'Shipping & Returns', 'rx-theme' ); ?></h2>
		<p>
			<?php esc_html_e( 'Shipping time, delivery cost, and return availability may depend on your location, product type, and store policy. Please check the checkout page for final delivery information.', 'rx-theme' ); ?>
		</p>
	</div>
	<?php
}

/**
 * Add custom product data tab: buying guide.
 */
function rx_woocommerce_buying_guide_tab( $tabs ) {

	$tabs['rx_buying_guide'] = array(
		'title'    => esc_html__( 'Buying Guide', 'rx-theme' ),
		'priority' => 50,
		'callback' => 'rx_woocommerce_buying_guide_tab_content',
	);

	return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'rx_woocommerce_buying_guide_tab' );

/**
 * Buying guide tab content.
 */
function rx_woocommerce_buying_guide_tab_content() {
	?>
	<div class="rx-product-tab-content rx-buying-guide-tab">
		<h2><?php esc_html_e( 'Buying Guide', 'rx-theme' ); ?></h2>
		<p>
			<?php esc_html_e( 'Before buying, check product size, compatibility, ingredients or material, warranty, return policy, and customer reviews. Choose the product that best matches your personal need and budget.', 'rx-theme' ); ?>
		</p>
	</div>
	<?php
}

/**
 * Single product image wrapper class.
 */
function rx_woocommerce_single_product_image_classes( $classes ) {

	$classes[] = 'rx-single-product-gallery';

	return $classes;
}
add_filter( 'woocommerce_single_product_image_gallery_classes', 'rx_woocommerce_single_product_image_classes' );

/**
 * Change product image thumbnail size.
 */
function rx_woocommerce_thumbnail_size() {
	return 'woocommerce_thumbnail';
}
add_filter( 'single_product_archive_thumbnail_size', 'rx_woocommerce_thumbnail_size' );

/**
 * Mini cart fragment update.
 */
function rx_woocommerce_cart_count_fragment( $fragments ) {

	ob_start();
	?>
	<span class="rx-cart-count">
		<?php echo esc_html( WC()->cart ? WC()->cart->get_cart_contents_count() : 0 ); ?>
	</span>
	<?php

	$fragments['span.rx-cart-count'] = ob_get_clean();

	return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'rx_woocommerce_cart_count_fragment' );

/**
 * Cart total fragment update.
 */
function rx_woocommerce_cart_total_fragment( $fragments ) {

	ob_start();
	?>
	<span class="rx-cart-total">
		<?php echo wp_kses_post( WC()->cart ? WC()->cart->get_cart_total() : wc_price( 0 ) ); ?>
	</span>
	<?php

	$fragments['span.rx-cart-total'] = ob_get_clean();

	return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'rx_woocommerce_cart_total_fragment' );

/**
 * RX mini cart HTML helper.
 *
 * Use this function in header.php:
 *
 * rx_woocommerce_header_cart();
 */
function rx_woocommerce_header_cart() {

	if ( ! rx_is_woocommerce_active() ) {
		return;
	}

	$count = WC()->cart ? WC()->cart->get_cart_contents_count() : 0;
	$total = WC()->cart ? WC()->cart->get_cart_total() : wc_price( 0 );
	?>
	<a class="rx-header-cart" href="<?php echo esc_url( wc_get_cart_url() ); ?>" aria-label="<?php esc_attr_e( 'View shopping cart', 'rx-theme' ); ?>">
		<span class="rx-cart-icon" aria-hidden="true">🛒</span>
		<span class="rx-cart-count"><?php echo esc_html( $count ); ?></span>
		<span class="rx-cart-total"><?php echo wp_kses_post( $total ); ?></span>
	</a>
	<?php
}

/**
 * Disable cart fragments on pages where cart is not needed.
 *
 * This improves performance.
 * Keep active on cart, checkout, account, product, and WooCommerce pages.
 */
function rx_woocommerce_optimize_cart_fragments() {

	if ( is_admin() ) {
		return;
	}

	if ( is_cart() || is_checkout() || is_account_page() || is_product() || is_woocommerce() ) {
		return;
	}

	if ( wp_script_is( 'wc-cart-fragments', 'enqueued' ) ) {
		wp_dequeue_script( 'wc-cart-fragments' );
	}
}
add_action( 'wp_enqueue_scripts', 'rx_woocommerce_optimize_cart_fragments', 99 );

/**
 * Change checkout fields.
 */
function rx_woocommerce_checkout_fields( $fields ) {

	if ( isset( $fields['billing']['billing_first_name'] ) ) {
		$fields['billing']['billing_first_name']['placeholder'] = esc_html__( 'First name', 'rx-theme' );
		$fields['billing']['billing_first_name']['priority']    = 10;
	}

	if ( isset( $fields['billing']['billing_last_name'] ) ) {
		$fields['billing']['billing_last_name']['placeholder'] = esc_html__( 'Last name', 'rx-theme' );
		$fields['billing']['billing_last_name']['priority']    = 20;
	}

	if ( isset( $fields['billing']['billing_email'] ) ) {
		$fields['billing']['billing_email']['placeholder'] = esc_html__( 'Email address', 'rx-theme' );
		$fields['billing']['billing_email']['priority']    = 30;
	}

	if ( isset( $fields['billing']['billing_phone'] ) ) {
		$fields['billing']['billing_phone']['placeholder'] = esc_html__( 'Phone number', 'rx-theme' );
		$fields['billing']['billing_phone']['priority']    = 40;
	}

	if ( isset( $fields['order']['order_comments'] ) ) {
		$fields['order']['order_comments']['placeholder'] = esc_html__( 'Notes about your order, delivery instruction, or special request.', 'rx-theme' );
	}

	return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'rx_woocommerce_checkout_fields' );

/**
 * Checkout field wrapper classes.
 */
function rx_woocommerce_form_field_args( $args, $key, $value ) {

	if ( isset( $args['class'] ) && is_array( $args['class'] ) ) {
		$args['class'][] = 'rx-form-row';
	}

	if ( isset( $args['input_class'] ) && is_array( $args['input_class'] ) ) {
		$args['input_class'][] = 'rx-input';
	}

	return $args;
}
add_filter( 'woocommerce_form_field_args', 'rx_woocommerce_form_field_args', 10, 3 );

/**
 * Customize account menu items.
 */
function rx_woocommerce_account_menu_items( $items ) {

	if ( isset( $items['dashboard'] ) ) {
		$items['dashboard'] = esc_html__( 'Dashboard', 'rx-theme' );
	}

	if ( isset( $items['orders'] ) ) {
		$items['orders'] = esc_html__( 'My Orders', 'rx-theme' );
	}

	if ( isset( $items['downloads'] ) ) {
		$items['downloads'] = esc_html__( 'Downloads', 'rx-theme' );
	}

	if ( isset( $items['edit-address'] ) ) {
		$items['edit-address'] = esc_html__( 'Addresses', 'rx-theme' );
	}

	if ( isset( $items['edit-account'] ) ) {
		$items['edit-account'] = esc_html__( 'Account Details', 'rx-theme' );
	}

	if ( isset( $items['customer-logout'] ) ) {
		$items['customer-logout'] = esc_html__( 'Logout', 'rx-theme' );
	}

	return $items;
}
add_filter( 'woocommerce_account_menu_items', 'rx_woocommerce_account_menu_items' );

/**
 * Add dashboard intro on my account page.
 */
function rx_woocommerce_account_dashboard_intro() {

	$current_user = wp_get_current_user();

	if ( ! $current_user || ! $current_user->exists() ) {
		return;
	}

	echo '<div class="rx-account-welcome">';
	echo '<h2>' . sprintf(
		esc_html__( 'Welcome, %s', 'rx-theme' ),
		esc_html( $current_user->display_name )
	) . '</h2>';
	echo '<p>' . esc_html__( 'From your account dashboard, you can view orders, manage addresses, and edit your account details.', 'rx-theme' ) . '</p>';
	echo '</div>';
}
add_action( 'woocommerce_account_dashboard', 'rx_woocommerce_account_dashboard_intro', 5 );

/**
 * Add cart page wrapper start.
 */
function rx_woocommerce_cart_wrapper_start() {
	echo '<div class="rx-cart-wrapper">';
}
add_action( 'woocommerce_before_cart', 'rx_woocommerce_cart_wrapper_start', 5 );

/**
 * Add cart page wrapper end.
 */
function rx_woocommerce_cart_wrapper_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_cart', 'rx_woocommerce_cart_wrapper_end', 50 );

/**
 * Add checkout page wrapper start.
 */
function rx_woocommerce_checkout_wrapper_start() {
	echo '<div class="rx-checkout-wrapper">';
}
add_action( 'woocommerce_before_checkout_form', 'rx_woocommerce_checkout_wrapper_start', 1 );

/**
 * Add checkout page wrapper end.
 */
function rx_woocommerce_checkout_wrapper_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_checkout_form', 'rx_woocommerce_checkout_wrapper_end', 50 );

/**
 * Change placeholder image.
 */
function rx_woocommerce_placeholder_img_src( $src ) {

	$placeholder = get_template_directory_uri() . '/assets/images/woocommerce-placeholder.png';
	$placeholder_path = get_template_directory() . '/assets/images/woocommerce-placeholder.png';

	if ( file_exists( $placeholder_path ) ) {
		return $placeholder;
	}

	return $src;
}
add_filter( 'woocommerce_placeholder_img_src', 'rx_woocommerce_placeholder_img_src' );

/**
 * Add lazy loading to WooCommerce images.
 */
function rx_woocommerce_product_get_image( $image, $product, $size, $attr, $placeholder ) {

	if ( false === strpos( $image, 'loading=' ) ) {
		$image = str_replace( '<img ', '<img loading="lazy" decoding="async" ', $image );
	}

	return $image;
}
add_filter( 'woocommerce_product_get_image', 'rx_woocommerce_product_get_image', 10, 5 );

/**
 * Improve product image alt text.
 */
function rx_woocommerce_product_image_alt( $attr, $attachment, $size ) {

	if ( empty( $attr['alt'] ) ) {
		$attr['alt'] = get_the_title();
	}

	return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'rx_woocommerce_product_image_alt', 10, 3 );

/**
 * Add custom sorting options.
 */
function rx_woocommerce_catalog_orderby( $sortby ) {

	$sortby['date']       = esc_html__( 'Newest products', 'rx-theme' );
	$sortby['price']      = esc_html__( 'Price: low to high', 'rx-theme' );
	$sortby['price-desc'] = esc_html__( 'Price: high to low', 'rx-theme' );
	$sortby['rating']     = esc_html__( 'Average rating', 'rx-theme' );
	$sortby['popularity'] = esc_html__( 'Popularity', 'rx-theme' );

	return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'rx_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'rx_woocommerce_catalog_orderby' );

/**
 * Set default catalog ordering.
 */
function rx_woocommerce_default_catalog_orderby() {
	return 'date';
}
add_filter( 'woocommerce_default_catalog_orderby', 'rx_woocommerce_default_catalog_orderby' );

/**
 * Add shop toolbar wrapper start.
 */
function rx_woocommerce_shop_toolbar_start() {
	echo '<div class="rx-shop-toolbar">';
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_shop_toolbar_start', 15 );

/**
 * Add shop toolbar wrapper end.
 */
function rx_woocommerce_shop_toolbar_end() {
	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_shop_toolbar_end', 35 );

/**
 * Move result count and ordering into toolbar.
 */
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

add_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

/**
 * Add no products found custom message.
 */
function rx_woocommerce_no_products_found_message() {
	echo '<div class="rx-no-products-found">';
	echo '<h2>' . esc_html__( 'No products found', 'rx-theme' ) . '</h2>';
	echo '<p>' . esc_html__( 'Please try another keyword, category, or filter.', 'rx-theme' ) . '</p>';
	echo '</div>';
}
remove_action( 'woocommerce_no_products_found', 'wc_no_products_found', 10 );
add_action( 'woocommerce_no_products_found', 'rx_woocommerce_no_products_found_message', 10 );

/**
 * Customize pagination args.
 */
function rx_woocommerce_pagination_args( $args ) {

	$args['prev_text'] = esc_html__( 'Previous', 'rx-theme' );
	$args['next_text'] = esc_html__( 'Next', 'rx-theme' );
	$args['type']      = 'list';

	return $args;
}
add_filter( 'woocommerce_pagination_args', 'rx_woocommerce_pagination_args' );

/**
 * Add archive description wrapper.
 */
function rx_woocommerce_archive_description_wrap_start() {

	if ( is_product_category() || is_product_tag() || is_shop() ) {
		echo '<div class="rx-woo-archive-description">';
	}
}
add_action( 'woocommerce_archive_description', 'rx_woocommerce_archive_description_wrap_start', 1 );

/**
 * Close archive description wrapper.
 */
function rx_woocommerce_archive_description_wrap_end() {

	if ( is_product_category() || is_product_tag() || is_shop() ) {
		echo '</div>';
	}
}
add_action( 'woocommerce_archive_description', 'rx_woocommerce_archive_description_wrap_end', 99 );

/**
 * Improve price HTML wrapper.
 */
function rx_woocommerce_get_price_html( $price, $product ) {

	if ( empty( $price ) ) {
		return $price;
	}

	return '<span class="rx-product-price-wrap">' . $price . '</span>';
}
add_filter( 'woocommerce_get_price_html', 'rx_woocommerce_get_price_html', 10, 2 );

/**
 * Add free shipping note on cart totals.
 */
function rx_woocommerce_cart_totals_after_order_total() {
	?>
	<tr class="rx-cart-note">
		<th><?php esc_html_e( 'Note', 'rx-theme' ); ?></th>
		<td><?php esc_html_e( 'Final shipping and tax are calculated at checkout.', 'rx-theme' ); ?></td>
	</tr>
	<?php
}
add_action( 'woocommerce_cart_totals_after_order_total', 'rx_woocommerce_cart_totals_after_order_total' );

/**
 * Add continue shopping button on cart.
 */
function rx_woocommerce_continue_shopping_button() {
	?>
	<a class="button rx-continue-shopping-button" href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>">
		<?php esc_html_e( 'Continue Shopping', 'rx-theme' ); ?>
	</a>
	<?php
}
add_action( 'woocommerce_before_cart_table', 'rx_woocommerce_continue_shopping_button', 5 );

/**
 * Change add to cart text.
 */
function rx_woocommerce_product_add_to_cart_text( $text, $product ) {

	if ( ! $product instanceof WC_Product ) {
		return $text;
	}

	if ( $product->is_type( 'simple' ) ) {
		return esc_html__( 'Add to Cart', 'rx-theme' );
	}

	if ( $product->is_type( 'variable' ) ) {
		return esc_html__( 'Select Options', 'rx-theme' );
	}

	if ( $product->is_type( 'grouped' ) ) {
		return esc_html__( 'View Products', 'rx-theme' );
	}

	if ( $product->is_type( 'external' ) ) {
		return esc_html__( 'Buy Product', 'rx-theme' );
	}

	return $text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'rx_woocommerce_product_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'rx_woocommerce_product_add_to_cart_text', 10, 2 );

/**
 * Add product reading/selling info.
 */
function rx_woocommerce_single_product_extra_info() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	echo '<div class="rx-product-extra-info">';

	if ( $product->get_review_count() > 0 ) {
		echo '<span class="rx-product-review-count">';
		echo sprintf(
			esc_html( _n( '%s customer review', '%s customer reviews', $product->get_review_count(), 'rx-theme' ) ),
			esc_html( number_format_i18n( $product->get_review_count() ) )
		);
		echo '</span>';
	}

	echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_product_extra_info', 11 );

/**
 * Disable WooCommerce sidebar on product pages if theme wants full width.
 */
function rx_woocommerce_disable_sidebar_on_product() {

	if ( is_product() ) {
		remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
	}
}
add_action( 'wp', 'rx_woocommerce_disable_sidebar_on_product' );

/**
 * Add WooCommerce sidebar wrapper.
 */
function rx_woocommerce_sidebar_wrapper_start() {

	if ( is_shop() || is_product_category() || is_product_tag() ) {
		echo '<aside class="rx-woo-sidebar">';
	}
}
add_action( 'woocommerce_sidebar', 'rx_woocommerce_sidebar_wrapper_start', 1 );

/**
 * Close WooCommerce sidebar wrapper.
 */
function rx_woocommerce_sidebar_wrapper_end() {

	if ( is_shop() || is_product_category() || is_product_tag() ) {
		echo '</aside>';
	}
}
add_action( 'woocommerce_sidebar', 'rx_woocommerce_sidebar_wrapper_end', 99 );

/**
 * Register WooCommerce sidebar.
 */
function rx_woocommerce_register_sidebar() {

	register_sidebar(
		array(
			'name'          => esc_html__( 'WooCommerce Sidebar', 'rx-theme' ),
			'id'            => 'rx-woocommerce-sidebar',
			'description'   => esc_html__( 'Widgets added here will appear on WooCommerce shop archive pages.', 'rx-theme' ),
			'before_widget' => '<section id="%1$s" class="widget rx-widget rx-woo-widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title rx-widget-title">',
			'after_title'   => '</h2>',
		)
	);
}
add_action( 'widgets_init', 'rx_woocommerce_register_sidebar' );

/**
 * Output RX WooCommerce sidebar instead of default.
 */
function rx_woocommerce_custom_sidebar() {

	if ( is_active_sidebar( 'rx-woocommerce-sidebar' ) ) {
		dynamic_sidebar( 'rx-woocommerce-sidebar' );
	}
}
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
add_action( 'woocommerce_sidebar', 'rx_woocommerce_custom_sidebar', 10 );

/**
 * Add product schema extra brand if product attribute exists.
 */
function rx_woocommerce_structured_data_product( $markup, $product ) {

	if ( ! $product instanceof WC_Product ) {
		return $markup;
	}

	$brand = $product->get_attribute( 'pa_brand' );

	if ( ! empty( $brand ) ) {
		$markup['brand'] = array(
			'@type' => 'Brand',
			'name'  => wp_strip_all_tags( $brand ),
		);
	}

	return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'rx_woocommerce_structured_data_product', 10, 2 );

/**
 * Add Open Graph product meta.
 */
function rx_woocommerce_product_open_graph_meta() {

	if ( ! is_product() ) {
		return;
	}

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$title       = get_the_title();
	$description = wp_strip_all_tags( get_the_excerpt() );
	$image_id    = $product->get_image_id();
	$image_url   = $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '';

	echo "\n<!-- RX WooCommerce Product Open Graph -->\n";
	echo '<meta property="og:type" content="product" />' . "\n";
	echo '<meta property="og:title" content="' . esc_attr( $title ) . '" />' . "\n";

	if ( $description ) {
		echo '<meta property="og:description" content="' . esc_attr( $description ) . '" />' . "\n";
	}

	if ( $image_url ) {
		echo '<meta property="og:image" content="' . esc_url( $image_url ) . '" />' . "\n";
	}

	echo '<meta property="product:price:amount" content="' . esc_attr( $product->get_price() ) . '" />' . "\n";
	echo '<meta property="product:price:currency" content="' . esc_attr( get_woocommerce_currency() ) . '" />' . "\n";
}
add_action( 'wp_head', 'rx_woocommerce_product_open_graph_meta', 5 );

/**
 * Add Twitter product meta.
 */
function rx_woocommerce_product_twitter_meta() {

	if ( ! is_product() ) {
		return;
	}

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$image_id  = $product->get_image_id();
	$image_url = $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '';

	echo "\n<!-- RX WooCommerce Product Twitter Meta -->\n";
	echo '<meta name="twitter:card" content="summary_large_image" />' . "\n";
	echo '<meta name="twitter:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n";
	echo '<meta name="twitter:description" content="' . esc_attr( wp_strip_all_tags( get_the_excerpt() ) ) . '" />' . "\n";

	if ( $image_url ) {
		echo '<meta name="twitter:image" content="' . esc_url( $image_url ) . '" />' . "\n";
	}
}
add_action( 'wp_head', 'rx_woocommerce_product_twitter_meta', 6 );

/**
 * Add product search form shortcode.
 *
 * Usage:
 * [rx_product_search]
 */
function rx_woocommerce_product_search_shortcode() {

	ob_start();
	?>
	<form role="search" method="get" class="rx-product-search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
		<label class="screen-reader-text" for="rx-product-search-field">
			<?php esc_html_e( 'Search products', 'rx-theme' ); ?>
		</label>
		<input
			type="search"
			id="rx-product-search-field"
			class="rx-product-search-field"
			placeholder="<?php esc_attr_e( 'Search products...', 'rx-theme' ); ?>"
			value="<?php echo esc_attr( get_search_query() ); ?>"
			name="s"
		/>
		<input type="hidden" name="post_type" value="product" />
		<button type="submit" class="rx-product-search-submit">
			<?php esc_html_e( 'Search', 'rx-theme' ); ?>
		</button>
	</form>
	<?php
	return ob_get_clean();
}
add_shortcode( 'rx_product_search', 'rx_woocommerce_product_search_shortcode' );

/**
 * Add featured products shortcode.
 *
 * Usage:
 * [rx_featured_products limit="4" columns="4"]
 */
function rx_woocommerce_featured_products_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'limit'   => 4,
			'columns' => 4,
		),
		$atts,
		'rx_featured_products'
	);

	return do_shortcode(
		sprintf(
			'[featured_products limit="%d" columns="%d"]',
			absint( $atts['limit'] ),
			absint( $atts['columns'] )
		)
	);
}
add_shortcode( 'rx_featured_products', 'rx_woocommerce_featured_products_shortcode' );

/**
 * Add sale products shortcode.
 *
 * Usage:
 * [rx_sale_products limit="4" columns="4"]
 */
function rx_woocommerce_sale_products_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'limit'   => 4,
			'columns' => 4,
		),
		$atts,
		'rx_sale_products'
	);

	return do_shortcode(
		sprintf(
			'[sale_products limit="%d" columns="%d"]',
			absint( $atts['limit'] ),
			absint( $atts['columns'] )
		)
	);
}
add_shortcode( 'rx_sale_products', 'rx_woocommerce_sale_products_shortcode' );

/**
 * Add latest products shortcode.
 *
 * Usage:
 * [rx_latest_products limit="8" columns="4"]
 */
function rx_woocommerce_latest_products_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'limit'   => 8,
			'columns' => 4,
		),
		$atts,
		'rx_latest_products'
	);

	return do_shortcode(
		sprintf(
			'[products limit="%d" columns="%d" orderby="date" order="DESC"]',
			absint( $atts['limit'] ),
			absint( $atts['columns'] )
		)
	);
}
add_shortcode( 'rx_latest_products', 'rx_woocommerce_latest_products_shortcode' );

/**
 * Add best selling products shortcode.
 *
 * Usage:
 * [rx_best_selling_products limit="8" columns="4"]
 */
function rx_woocommerce_best_selling_products_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'limit'   => 8,
			'columns' => 4,
		),
		$atts,
		'rx_best_selling_products'
	);

	return do_shortcode(
		sprintf(
			'[best_selling_products limit="%d" columns="%d"]',
			absint( $atts['limit'] ),
			absint( $atts['columns'] )
		)
	);
}
add_shortcode( 'rx_best_selling_products', 'rx_woocommerce_best_selling_products_shortcode' );

/**
 * AJAX quick view product.
 */
function rx_woocommerce_ajax_quick_view() {

	check_ajax_referer( 'rx_woocommerce_nonce', 'nonce' );

	$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;

	if ( ! $product_id ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Invalid product.', 'rx-theme' ),
			)
		);
	}

	$product = wc_get_product( $product_id );

	if ( ! $product ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Product not found.', 'rx-theme' ),
			)
		);
	}

	ob_start();
	?>
	<div class="rx-quick-view-modal-content">
		<div class="rx-quick-view-image">
			<?php echo wp_kses_post( $product->get_image( 'woocommerce_single' ) ); ?>
		</div>
		<div class="rx-quick-view-summary">
			<h2><?php echo esc_html( $product->get_name() ); ?></h2>
			<div class="rx-quick-view-price">
				<?php echo wp_kses_post( $product->get_price_html() ); ?>
			</div>
			<div class="rx-quick-view-excerpt">
				<?php echo wp_kses_post( wpautop( $product->get_short_description() ) ); ?>
			</div>
			<a class="button rx-view-product-button" href="<?php echo esc_url( get_permalink( $product_id ) ); ?>">
				<?php esc_html_e( 'View Product', 'rx-theme' ); ?>
			</a>
		</div>
	</div>
	<?php

	wp_send_json_success(
		array(
			'html' => ob_get_clean(),
		)
	);
}
add_action( 'wp_ajax_rx_quick_view', 'rx_woocommerce_ajax_quick_view' );
add_action( 'wp_ajax_nopriv_rx_quick_view', 'rx_woocommerce_ajax_quick_view' );

/**
 * Add custom notice wrapper classes.
 */
function rx_woocommerce_notice_classes( $classes ) {

	$classes[] = 'rx-woocommerce-notice';

	return $classes;
}
add_filter( 'woocommerce_notice_classes', 'rx_woocommerce_notice_classes' );

/**
 * Make product tabs section accessible.
 */
function rx_woocommerce_before_product_tabs() {
	echo '<section class="rx-product-tabs-section" aria-label="' . esc_attr__( 'Product information tabs', 'rx-theme' ) . '">';
}
add_action( 'woocommerce_after_single_product_summary', 'rx_woocommerce_before_product_tabs', 9 );

function rx_woocommerce_after_product_tabs() {
	echo '</section>';
}
add_action( 'woocommerce_after_single_product_summary', 'rx_woocommerce_after_product_tabs', 11 );

/**
 * Add empty cart message enhancement.
 */
function rx_woocommerce_cart_is_empty_message() {
	echo '<div class="rx-empty-cart-extra">';
	echo '<p>' . esc_html__( 'Your cart is waiting for something useful. Explore our shop and add your favorite products.', 'rx-theme' ) . '</p>';
	echo '<a class="button rx-shop-now-button" href="' . esc_url( wc_get_page_permalink( 'shop' ) ) . '">' . esc_html__( 'Shop Now', 'rx-theme' ) . '</a>';
	echo '</div>';
}
add_action( 'woocommerce_cart_is_empty', 'rx_woocommerce_cart_is_empty_message', 20 );

/**
 * Product category image fallback.
 */
function rx_woocommerce_subcategory_thumbnail_size( $size ) {
	return 'woocommerce_thumbnail';
}
add_filter( 'subcategory_archive_thumbnail_size', 'rx_woocommerce_subcategory_thumbnail_size' );

/**
 * Add category card wrapper.
 */
function rx_woocommerce_before_subcategory_title( $category ) {
	echo '<div class="rx-product-category-card-content">';
}
add_action( 'woocommerce_shop_loop_subcategory_title', 'rx_woocommerce_before_subcategory_title', 5 );

function rx_woocommerce_after_subcategory_title( $category ) {
	echo '</div>';
}
add_action( 'woocommerce_after_subcategory_title', 'rx_woocommerce_after_subcategory_title', 20 );

/**
 * Customize required field mark.
 */
function rx_woocommerce_required_field_html( $html ) {
	return '<abbr class="required rx-required" title="' . esc_attr__( 'required', 'rx-theme' ) . '">*</abbr>';
}
add_filter( 'woocommerce_required_field_html', 'rx_woocommerce_required_field_html' );

/**
 * Add payment security note.
 */
function rx_woocommerce_review_order_before_payment() {
	echo '<div class="rx-secure-payment-note">';
	echo esc_html__( 'Your payment information is processed securely. Please review your order before placing it.', 'rx-theme' );
	echo '</div>';
}
add_action( 'woocommerce_review_order_before_payment', 'rx_woocommerce_review_order_before_payment' );

/**
 * Add order received thank you custom box.
 */
function rx_woocommerce_thankyou_custom_box( $order_id ) {

	if ( ! $order_id ) {
		return;
	}

	$order = wc_get_order( $order_id );

	if ( ! $order ) {
		return;
	}

	echo '<div class="rx-thankyou-box">';
	echo '<h2>' . esc_html__( 'Thank you for your order!', 'rx-theme' ) . '</h2>';
	echo '<p>' . sprintf(
		esc_html__( 'Your order number is #%s. We have received your order and will process it soon.', 'rx-theme' ),
		esc_html( $order->get_order_number() )
	) . '</p>';
	echo '</div>';
}
add_action( 'woocommerce_thankyou', 'rx_woocommerce_thankyou_custom_box', 5 );

/**
 * Add order status badge class.
 */
function rx_woocommerce_order_status_class( $status ) {
	return 'rx-order-status rx-order-status-' . sanitize_html_class( $status );
}

/**
 * Hide coupon form on checkout if needed.
 *
 * Change false to true if you want to disable checkout coupon form.
 */
function rx_woocommerce_disable_checkout_coupon_form() {
	return false;
}

if ( rx_woocommerce_disable_checkout_coupon_form() ) {
	remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
}

/**
 * Add product meta after loop item for data attributes.
 */
function rx_woocommerce_loop_product_data_attributes() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	echo '<span class="rx-product-data" hidden ';
	echo 'data-product-id="' . esc_attr( $product->get_id() ) . '" ';
	echo 'data-product-type="' . esc_attr( $product->get_type() ) . '" ';
	echo 'data-product-price="' . esc_attr( $product->get_price() ) . '"';
	echo '></span>';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_loop_product_data_attributes', 99 );

/**
 * Improve WooCommerce page title.
 */
function rx_woocommerce_page_title( $title ) {

	if ( is_shop() ) {
		$shop_page_id = wc_get_page_id( 'shop' );

		if ( $shop_page_id > 0 ) {
			return get_the_title( $shop_page_id );
		}
	}

	return $title;
}
add_filter( 'woocommerce_page_title', 'rx_woocommerce_page_title' );

/**
 * Add before product loop container.
 */
function rx_woocommerce_product_loop_container_start() {
	echo '<div class="rx-products-loop-wrapper">';
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_product_loop_container_start', 40 );

/**
 * Add after product loop container.
 */
function rx_woocommerce_product_loop_container_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_shop_loop', 'rx_woocommerce_product_loop_container_end', 5 );

/**
 * Add custom class to product loop items.
 */
function rx_woocommerce_post_class( $classes, $class, $product_id ) {

	if ( 'product' === get_post_type( $product_id ) ) {
		$classes[] = 'rx-product-card';
	}

	return $classes;
}
add_filter( 'woocommerce_post_class', 'rx_woocommerce_post_class', 10, 3 );

/**
 * Product review avatar size.
 */
function rx_woocommerce_review_gravatar_size( $size ) {
	return 70;
}
add_filter( 'woocommerce_review_gravatar_size', 'rx_woocommerce_review_gravatar_size' );

/**
 * Change review verified owner text.
 */
function rx_woocommerce_review_display_verified_owner( $verified, $comment ) {
	return $verified;
}
add_filter( 'woocommerce_review_display_verified_owner', 'rx_woocommerce_review_display_verified_owner', 10, 2 );

/**
 * Add product share buttons.
 */
function rx_woocommerce_single_product_share_buttons() {

	if ( ! is_product() ) {
		return;
	}

	$url   = rawurlencode( get_permalink() );
	$title = rawurlencode( get_the_title() );

	echo '<div class="rx-product-share">';
	echo '<span class="rx-product-share-title">' . esc_html__( 'Share:', 'rx-theme' ) . '</span>';

	echo '<a href="' . esc_url( 'https://www.facebook.com/sharer/sharer.php?u=' . $url ) . '" target="_blank" rel="noopener noreferrer">Facebook</a>';
	echo '<a href="' . esc_url( 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title ) . '" target="_blank" rel="noopener noreferrer">X</a>';
	echo '<a href="' . esc_url( 'https://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $title ) . '" target="_blank" rel="noopener noreferrer">LinkedIn</a>';

	echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_product_share_buttons', 45 );

/**
 * Add product estimated delivery text.
 */
function rx_woocommerce_estimated_delivery_text() {

	if ( ! is_product() ) {
		return;
	}

	$from = date_i18n( get_option( 'date_format' ), strtotime( '+3 days' ) );
	$to   = date_i18n( get_option( 'date_format' ), strtotime( '+7 days' ) );

	echo '<div class="rx-estimated-delivery">';
	echo sprintf(
		esc_html__( 'Estimated delivery: %1$s - %2$s', 'rx-theme' ),
		esc_html( $from ),
		esc_html( $to )
	);
	echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_estimated_delivery_text', 36 );

/**
 * Add low stock warning on single product.
 */
function rx_woocommerce_single_low_stock_warning() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	if ( $product->managing_stock() && $product->is_in_stock() ) {
		$qty = $product->get_stock_quantity();

		if ( $qty > 0 && $qty <= 5 ) {
			echo '<div class="rx-low-stock-warning">';
			echo sprintf(
				esc_html__( 'Only %s left in stock.', 'rx-theme' ),
				esc_html( $qty )
			);
			echo '</div>';
		}
	}
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_low_stock_warning', 27 );

/**
 * Customize product availability text.
 */
function rx_woocommerce_get_availability_text( $availability, $product ) {

	if ( ! $product instanceof WC_Product ) {
		return $availability;
	}

	if ( ! $product->is_in_stock() ) {
		return esc_html__( 'Currently unavailable', 'rx-theme' );
	}

	if ( $product->managing_stock() && $product->get_stock_quantity() <= 5 ) {
		return esc_html__( 'Limited stock available', 'rx-theme' );
	}

	return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'rx_woocommerce_get_availability_text', 10, 2 );

/**
 * Add custom product class based on stock.
 */
function rx_woocommerce_product_stock_class( $classes, $class, $product_id ) {

	$product = wc_get_product( $product_id );

	if ( ! $product ) {
		return $classes;
	}

	if ( $product->is_in_stock() ) {
		$classes[] = 'rx-product-in-stock';
	} else {
		$classes[] = 'rx-product-out-of-stock';
	}

	if ( $product->is_on_sale() ) {
		$classes[] = 'rx-product-on-sale';
	}

	if ( $product->is_featured() ) {
		$classes[] = 'rx-product-featured';
	}

	return $classes;
}
add_filter( 'woocommerce_post_class', 'rx_woocommerce_product_stock_class', 20, 3 );

/**
 * Remove WooCommerce generator tag.
 */
remove_action( 'wp_head', 'wc_generator_tag' );

/**
 * Clean WooCommerce scripts on non-commerce pages.
 */
function rx_woocommerce_script_cleanup() {

	if ( is_admin() ) {
		return;
	}

	if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() || is_product() ) {
		return;
	}

	/**
	 * Do not remove scripts if the current page has WooCommerce shortcode.
	 */
	global $post;

	if ( $post instanceof WP_Post ) {
		$content = $post->post_content;

		if (
			has_shortcode( $content, 'woocommerce_cart' ) ||
			has_shortcode( $content, 'woocommerce_checkout' ) ||
			has_shortcode( $content, 'products' ) ||
			has_shortcode( $content, 'product_page' ) ||
			has_shortcode( $content, 'featured_products' ) ||
			has_shortcode( $content, 'sale_products' )
		) {
			return;
		}
	}

	wp_dequeue_style( 'woocommerce-general' );
	wp_dequeue_style( 'woocommerce-layout' );
	wp_dequeue_style( 'woocommerce-smallscreen' );
}
add_action( 'wp_enqueue_scripts', 'rx_woocommerce_script_cleanup', 100 );

/**
 * Add custom quantity buttons wrapper.
 */
function rx_woocommerce_before_quantity_input_field() {
	echo '<div class="rx-quantity-wrap">';
	echo '<button type="button" class="rx-qty-minus" aria-label="' . esc_attr__( 'Decrease quantity', 'rx-theme' ) . '">−</button>';
}
add_action( 'woocommerce_before_quantity_input_field', 'rx_woocommerce_before_quantity_input_field' );

function rx_woocommerce_after_quantity_input_field() {
	echo '<button type="button" class="rx-qty-plus" aria-label="' . esc_attr__( 'Increase quantity', 'rx-theme' ) . '">+</button>';
	echo '</div>';
}
add_action( 'woocommerce_after_quantity_input_field', 'rx_woocommerce_after_quantity_input_field' );

/**
 * Add inline JS for quantity buttons.
 */
function rx_woocommerce_quantity_buttons_script() {

	if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_product() ) {
		return;
	}
	?>
	<script>
		document.addEventListener('click', function(event) {
			if (!event.target.classList.contains('rx-qty-plus') && !event.target.classList.contains('rx-qty-minus')) {
				return;
			}

			const wrapper = event.target.closest('.rx-quantity-wrap');
			if (!wrapper) {
				return;
			}

			const input = wrapper.querySelector('input.qty');
			if (!input) {
				return;
			}

			const step = parseFloat(input.getAttribute('step')) || 1;
			const min = parseFloat(input.getAttribute('min')) || 0;
			const max = parseFloat(input.getAttribute('max')) || 999999;
			let value = parseFloat(input.value) || min;

			if (event.target.classList.contains('rx-qty-plus')) {
				value = Math.min(value + step, max);
			}

			if (event.target.classList.contains('rx-qty-minus')) {
				value = Math.max(value - step, min);
			}

			input.value = value;
			input.dispatchEvent(new Event('change', { bubbles: true }));
		});
	</script>
	<?php
}
add_action( 'wp_footer', 'rx_woocommerce_quantity_buttons_script', 30 );

/**
 * Add product loop hover image.
 */
function rx_woocommerce_loop_hover_image() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	$gallery_ids = $product->get_gallery_image_ids();

	if ( empty( $gallery_ids ) ) {
		return;
	}

	$hover_id = absint( $gallery_ids[0] );

	if ( ! $hover_id ) {
		return;
	}

	echo '<div class="rx-product-hover-image">';
	echo wp_get_attachment_image(
		$hover_id,
		'woocommerce_thumbnail',
		false,
		array(
			'loading'  => 'lazy',
			'decoding' => 'async',
			'alt'      => esc_attr( $product->get_name() ),
		)
	);
	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_loop_hover_image', 11 );

/**
 * Add custom product badges wrapper start.
 */
function rx_woocommerce_product_badges_wrap_start() {
	echo '<div class="rx-product-badges">';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_product_badges_wrap_start', 9 );

/**
 * Add custom product badges wrapper end.
 */
function rx_woocommerce_product_badges_wrap_end() {
	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_product_badges_wrap_end', 14 );

/**
 * Add featured badge.
 */
function rx_woocommerce_featured_badge() {

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return;
	}

	if ( $product->is_featured() ) {
		echo '<span class="rx-featured-badge">' . esc_html__( 'Featured', 'rx-theme' ) . '</span>';
	}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'rx_woocommerce_featured_badge', 12 );

/**
 * Product category count HTML.
 */
function rx_woocommerce_subcategory_count_html( $html, $category ) {

	if ( ! $category instanceof WP_Term ) {
		return $html;
	}

	return '<mark class="rx-category-count count">' . esc_html( $category->count ) . '</mark>';
}
add_filter( 'woocommerce_subcategory_count_html', 'rx_woocommerce_subcategory_count_html', 10, 2 );

/**
 * Add page header for WooCommerce endpoints.
 */
function rx_woocommerce_account_endpoint_title( $title ) {

	if ( is_wc_endpoint_url( 'orders' ) ) {
		return esc_html__( 'My Orders', 'rx-theme' );
	}

	if ( is_wc_endpoint_url( 'downloads' ) ) {
		return esc_html__( 'My Downloads', 'rx-theme' );
	}

	if ( is_wc_endpoint_url( 'edit-address' ) ) {
		return esc_html__( 'My Addresses', 'rx-theme' );
	}

	if ( is_wc_endpoint_url( 'edit-account' ) ) {
		return esc_html__( 'Account Details', 'rx-theme' );
	}

	return $title;
}
add_filter( 'woocommerce_endpoint_orders_title', 'rx_woocommerce_account_endpoint_title' );

/**
 * Add custom order button text.
 */
function rx_woocommerce_order_button_text() {
	return esc_html__( 'Place Order Securely', 'rx-theme' );
}
add_filter( 'woocommerce_order_button_text', 'rx_woocommerce_order_button_text' );

/**
 * Add login/register wrapper.
 */
function rx_woocommerce_before_customer_login_form() {
	echo '<div class="rx-customer-login-wrapper">';
}
add_action( 'woocommerce_before_customer_login_form', 'rx_woocommerce_before_customer_login_form' );

function rx_woocommerce_after_customer_login_form() {
	echo '</div>';
}
add_action( 'woocommerce_after_customer_login_form', 'rx_woocommerce_after_customer_login_form' );

/**
 * Add WooCommerce support check admin notice.
 */
function rx_woocommerce_admin_notice_theme_ready() {

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	if ( ! rx_is_woocommerce_active() ) {
		return;
	}

	$screen = get_current_screen();

	if ( ! $screen || 'dashboard' !== $screen->id ) {
		return;
	}

	if ( get_transient( 'rx_woocommerce_admin_notice_dismissed' ) ) {
		return;
	}

	echo '<div class="notice notice-success is-dismissible rx-woo-admin-notice">';
	echo '<p><strong>' . esc_html__( 'RX Theme WooCommerce:', 'rx-theme' ) . '</strong> ';
	echo esc_html__( 'WooCommerce support is active and ready.', 'rx-theme' );
	echo '</p>';
	echo '</div>';
}
add_action( 'admin_notices', 'rx_woocommerce_admin_notice_theme_ready' );

/**
 * Add custom WooCommerce image sizes.
 */
function rx_woocommerce_custom_image_sizes() {

	add_image_size( 'rx-product-card', 600, 600, true );
	add_image_size( 'rx-product-wide', 900, 600, true );
	add_image_size( 'rx-product-single-large', 1000, 1000, false );
}
add_action( 'after_setup_theme', 'rx_woocommerce_custom_image_sizes' );

/**
 * Add custom product gallery thumbnail columns.
 */
function rx_woocommerce_product_thumbnails_columns() {
	return 5;
}
add_filter( 'woocommerce_product_thumbnails_columns', 'rx_woocommerce_product_thumbnails_columns' );

/**
 * Account orders per page.
 */
function rx_woocommerce_my_account_my_orders_query( $args ) {

	$args['limit'] = 10;

	return $args;
}
add_filter( 'woocommerce_my_account_my_orders_query', 'rx_woocommerce_my_account_my_orders_query' );

/**
 * Add customer support box in account dashboard.
 */
function rx_woocommerce_account_support_box() {
	?>
	<div class="rx-account-support-box">
		<h3><?php esc_html_e( 'Need help?', 'rx-theme' ); ?></h3>
		<p><?php esc_html_e( 'If you have questions about your order, payment, delivery, or account, please contact store support.', 'rx-theme' ); ?></p>
		<a class="button rx-support-button" href="<?php echo esc_url( home_url( '/contact/' ) ); ?>">
			<?php esc_html_e( 'Contact Support', 'rx-theme' ); ?>
		</a>
	</div>
	<?php
}
add_action( 'woocommerce_account_dashboard', 'rx_woocommerce_account_support_box', 20 );

/**
 * Add product category shortcode.
 *
 * Usage:
 * [rx_product_categories number="8" columns="4"]
 */
function rx_woocommerce_product_categories_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'number'  => 8,
			'columns' => 4,
		),
		$atts,
		'rx_product_categories'
	);

	return do_shortcode(
		sprintf(
			'[product_categories number="%d" columns="%d"]',
			absint( $atts['number'] ),
			absint( $atts['columns'] )
		)
	);
}
add_shortcode( 'rx_product_categories', 'rx_woocommerce_product_categories_shortcode' );

/**
 * Add product count shortcode.
 *
 * Usage:
 * [rx_product_count]
 */
function rx_woocommerce_product_count_shortcode() {

	$count = wp_count_posts( 'product' );

	if ( ! $count || empty( $count->publish ) ) {
		return '0';
	}

	return esc_html( number_format_i18n( $count->publish ) );
}
add_shortcode( 'rx_product_count', 'rx_woocommerce_product_count_shortcode' );

/**
 * Add cart count shortcode.
 *
 * Usage:
 * [rx_cart_count]
 */
function rx_woocommerce_cart_count_shortcode() {

	if ( ! WC()->cart ) {
		return '0';
	}

	return esc_html( WC()->cart->get_cart_contents_count() );
}
add_shortcode( 'rx_cart_count', 'rx_woocommerce_cart_count_shortcode' );

/**
 * Add cart total shortcode.
 *
 * Usage:
 * [rx_cart_total]
 */
function rx_woocommerce_cart_total_shortcode() {

	if ( ! WC()->cart ) {
		return wp_kses_post( wc_price( 0 ) );
	}

	return wp_kses_post( WC()->cart->get_cart_total() );
}
add_shortcode( 'rx_cart_total', 'rx_woocommerce_cart_total_shortcode' );

/**
 * Add custom classes to checkout button.
 */
function rx_woocommerce_proceed_to_checkout_button_html() {

	$checkout_url = wc_get_checkout_url();

	echo '<a href="' . esc_url( $checkout_url ) . '" class="checkout-button button alt wc-forward rx-checkout-button">';
	echo esc_html__( 'Proceed to Secure Checkout', 'rx-theme' );
	echo '</a>';
}
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
add_action( 'woocommerce_proceed_to_checkout', 'rx_woocommerce_proceed_to_checkout_button_html', 20 );

/**
 * Modify mini cart buttons.
 */
function rx_woocommerce_widget_shopping_cart_button_view_cart() {
	echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward rx-mini-cart-view-cart">' . esc_html__( 'View Cart', 'rx-theme' ) . '</a>';
}

function rx_woocommerce_widget_shopping_cart_proceed_to_checkout() {
	echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="button checkout wc-forward rx-mini-cart-checkout">' . esc_html__( 'Checkout', 'rx-theme' ) . '</a>';
}

remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

add_action( 'woocommerce_widget_shopping_cart_buttons', 'rx_woocommerce_widget_shopping_cart_button_view_cart', 10 );
add_action( 'woocommerce_widget_shopping_cart_buttons', 'rx_woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

/**
 * Add privacy note under checkout form.
 */
function rx_woocommerce_checkout_privacy_note() {
	?>
	<div class="rx-checkout-privacy-note">
		<?php esc_html_e( 'Your personal data will be used to process your order, support your experience, and manage access to your account.', 'rx-theme' ); ?>
	</div>
	<?php
}
add_action( 'woocommerce_review_order_after_submit', 'rx_woocommerce_checkout_privacy_note', 20 );

/**
 * Add product archive top banner.
 */
function rx_woocommerce_shop_top_banner() {

	if ( ! is_shop() ) {
		return;
	}

	echo '<div class="rx-shop-top-banner">';
	echo '<h2>' . esc_html__( 'Shop Products', 'rx-theme' ) . '</h2>';
	echo '<p>' . esc_html__( 'Explore selected products and choose what fits your needs.', 'rx-theme' ) . '</p>';
	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_shop_top_banner', 0 );

/**
 * Add category archive custom intro.
 */
function rx_woocommerce_category_intro() {

	if ( ! is_product_category() ) {
		return;
	}

	$term = get_queried_object();

	if ( ! $term instanceof WP_Term ) {
		return;
	}

	echo '<div class="rx-category-intro">';
	echo '<h2>' . esc_html( $term->name ) . '</h2>';

	if ( ! empty( $term->description ) ) {
		echo '<p>' . wp_kses_post( wp_trim_words( $term->description, 35, '...' ) ) . '</p>';
	}

	echo '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'rx_woocommerce_category_intro', 2 );

/**
 * Add safe product excerpt fallback.
 */
function rx_woocommerce_single_short_description( $post_excerpt ) {

	if ( ! empty( $post_excerpt ) ) {
		return $post_excerpt;
	}

	global $product;

	if ( ! $product instanceof WC_Product ) {
		return $post_excerpt;
	}

	$content = get_post_field( 'post_content', $product->get_id() );

	if ( empty( $content ) ) {
		return $post_excerpt;
	}

	return wp_trim_words( wp_strip_all_tags( $content ), 35, '...' );
}
add_filter( 'woocommerce_short_description', 'rx_woocommerce_single_short_description' );

/**
 * Add product page content anchor links.
 */
function rx_woocommerce_single_product_anchor_links() {

	if ( ! is_product() ) {
		return;
	}

	echo '<nav class="rx-product-anchor-links" aria-label="' . esc_attr__( 'Product page links', 'rx-theme' ) . '">';
	echo '<a href="#tab-description">' . esc_html__( 'Description', 'rx-theme' ) . '</a>';
	echo '<a href="#reviews">' . esc_html__( 'Reviews', 'rx-theme' ) . '</a>';
	echo '<a href="#related-products">' . esc_html__( 'Related', 'rx-theme' ) . '</a>';
	echo '</nav>';
}
add_action( 'woocommerce_single_product_summary', 'rx_woocommerce_single_product_anchor_links', 6 );

/**
 * Add ID before related products section.
 */
function rx_woocommerce_related_products_section_anchor() {
	echo '<div id="related-products" class="rx-related-products-anchor"></div>';
}
add_action( 'woocommerce_after_single_product_summary', 'rx_woocommerce_related_products_section_anchor', 19 );

/**
 * Protect AJAX handlers from bad requests.
 */
function rx_woocommerce_verify_ajax_request() {

	if ( ! wp_doing_ajax() ) {
		return;
	}

	if ( isset( $_REQUEST['action'] ) && false !== strpos( sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ), 'rx_' ) ) {
		if ( empty( $_REQUEST['nonce'] ) ) {
			wp_send_json_error(
				array(
					'message' => esc_html__( 'Security check failed.', 'rx-theme' ),
				),
				403
			);
		}
	}
}
add_action( 'init', 'rx_woocommerce_verify_ajax_request', 1 );

/**
 * Add product loop rating wrapper.
 */
function rx_woocommerce_before_loop_rating() {
	echo '<div class="rx-product-rating-wrap">';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'rx_woocommerce_before_loop_rating', 4 );

function rx_woocommerce_after_loop_rating() {
	echo '</div>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'rx_woocommerce_after_loop_rating', 6 );

/**
 * Move rating before price.
 */
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 12 );

/**
 * Add product card footer.
 */
function rx_woocommerce_product_card_footer_start() {
	echo '<div class="rx-product-card-footer">';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_product_card_footer_start', 5 );

function rx_woocommerce_product_card_footer_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_shop_loop_item', 'rx_woocommerce_product_card_footer_end', 30 );

/**
 * Ensure checkout terms are visible-friendly.
 */
function rx_woocommerce_terms_and_conditions_checkbox_text( $text ) {
	return esc_html__( 'I have read and agree to the website terms and conditions.', 'rx-theme' );
}
add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', 'rx_woocommerce_terms_and_conditions_checkbox_text' );

/**
 * Add RX theme WooCommerce template debug comment.
 */
function rx_woocommerce_template_debug_comment() {

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {
		return;
	}

	echo "\n<!-- RX Theme WooCommerce integration active -->\n";
}
add_action( 'wp_footer', 'rx_woocommerce_template_debug_comment', 99 );

Important: this file is powerful, so after adding it, clear cache and test these pages first:

/shop/
/cart/
/checkout/
/my-account/
single product page
product category page

If any design breaks, the most likely cause will be duplicate wrappers from your woocommerce.php template or custom WooCommerce template overrides.

Leave a Reply

Your email address will not be published. Required fields are marked *