/** * Enqueue script and styles for child theme */ function woodmart_child_enqueue_styles() { wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'woodmart-style' ), woodmart_get_theme_info( 'Version' ) ); } add_action( 'wp_enqueue_scripts', 'woodmart_child_enqueue_styles', 10010 ); load_child_theme_textdomain( 'woodmart', get_stylesheet_directory() . '/lang' ); load_child_theme_textdomain( 'woocommerce', get_stylesheet_directory() . '/lang' ); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/js/wjs.js', array( 'jquery' ), '', true ); add_action( 'acf/init', 'set_acf_settings' ); function set_acf_settings() { acf_update_setting( 'enable_shortcode', true ); } // Display alternative product function function display_alternative_product() { global $product; // Ensure we are working with a product if (!is_object($product) || !method_exists($product, 'is_in_stock')) { return; } // Check if the current product is out of stock if (!$product->is_in_stock()) { // Get the Alternative Product using ACF Relationship field $alternative_product = get_field('alternative_product', $product->get_id()); // Check if the field returns a valid product if (is_object($alternative_product)) { // If ACF returns a post object, get its ID $alternative_product_id = $alternative_product->ID; } elseif (is_numeric($alternative_product)) { // If ACF returns an ID directly $alternative_product_id = $alternative_product; } else { echo '
No alternative product set for this item.
'; return; } // Load the alternative WooCommerce product $alternative_product_obj = wc_get_product($alternative_product_id); if ($alternative_product_obj) { // Display the alternative product echo '' . $alternative_product_obj->get_price_html() . '
'; // Product Price echo 'Add to Cart'; echo 'Alternative product not found.
'; } } } add_action('woocommerce_single_product_summary', 'display_alternative_product', 35); // Add AJAX handlers add_action('wp_ajax_add_to_cart', 'handle_ajax_add_to_cart'); add_action('wp_ajax_nopriv_add_to_cart', 'handle_ajax_add_to_cart'); function handle_ajax_add_to_cart() { $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0; if ($product_id > 0) { $added = WC()->cart->add_to_cart($product_id); if ($added) { wp_send_json_success([ 'message' => 'Product added to cart', 'cart_count' => WC()->cart->get_cart_contents_count() ]); } } wp_send_json_error(['message' => 'Failed to add product to cart']); } add_action('wp_ajax_add_all_to_cart', 'handle_ajax_add_all_to_cart'); add_action('wp_ajax_nopriv_add_all_to_cart', 'handle_ajax_add_all_to_cart'); function handle_ajax_add_all_to_cart() { $product_ids = isset($_POST['product_ids']) ? (array)$_POST['product_ids'] : []; $success = true; foreach ($product_ids as $product_id) { $product = wc_get_product($product_id); if ($product && $product->is_in_stock()) { $added = WC()->cart->add_to_cart($product_id); if (!$added) { $success = false; } } } if ($success) { wp_send_json_success([ 'message' => 'All products added to cart', 'cart_count' => WC()->cart->get_cart_contents_count() ]); } else { wp_send_json_error(['message' => 'Some products could not be added to cart']); } } // Add AJAX URL to WordPress function add_ajax_url() { wp_localize_script('jquery', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'))); } add_action('wp_enqueue_scripts', 'add_ajax_url'); // Custom developer code for adding all to cart function enqueue_add_all_to_cart_script() { wp_enqueue_script( 'add-all-to-cart', get_stylesheet_directory_uri() . '/js/add-all-to-cart.js', ['jquery'], '1.0', true ); wp_localize_script('add-all-to-cart', 'ajax_obj', [ 'ajax_url' => admin_url('admin-ajax.php'), 'cart_url' => wc_get_cart_url(), // Get the cart page URL dynamically ]); } add_action('wp_enqueue_scripts', 'enqueue_add_all_to_cart_script');