is_type( 'variable' ) ) { return $stock_status; } if ( ! $product ) { $product = wc_get_product( get_the_ID() ); } if ( ! $product ) { return $stock_status; } $is_variation = $product->is_type( 'variation' ); $product_id = $product->get_id(); $parent_id = $is_variation ? $product->get_parent_id() : $product_id; $product_name = $product->get_name(); $product_url = get_permalink( $parent_id ); $product_tags = wp_strip_all_tags( wc_get_product_tag_list( $parent_id, ', ' ) ); $category_ids = get_option( 'ninetytwo_notify_category_ids_label', array() ); $category_ids = is_array( $category_ids ) ? $category_ids : array(); if ( ! empty( $category_ids ) && ! has_term( $category_ids, 'product_cat', $parent_id ) ) { return $stock_status; } if ( $product->is_in_stock() ) { return $stock_status; } // Variations are always exact-match only; the similar-products // setting only broadens alerts for whole (non-variation) products. $similar_products_on = ! $is_variation && get_option( 'ninetytwo_notify_similar_products_label' ); $form_html = ninetytwo_notify_get_notifier_form_html( $product, $parent_id, $is_variation, $product_id, $product_name, $product_url, $product_tags, $similar_products_on ); return $stock_status . $form_html; } } add_filter( 'woocommerce_get_stock_html', 'ninetytwo_notify_after_add_to_cart_button', 100, 2 ); /** * Builds the markup for the subscription form. Broken out so the strings and * structure stay filterable without needing to touch the hook callback. * * @param WC_Product $product The product or variation being rendered. * @param int $parent_id Parent product ID (same as $product_id for simple products). * @param bool $is_variation Whether $product is a variation. * @param int $product_id The exact product/variation ID being subscribed to. * @param string $product_name Product name, for the hidden field and emails. * @param string $product_url Product permalink. * @param string $product_tags Comma-separated product tag names. * @param bool $similar_products_on Whether similar-product matching is enabled for this form. * @return string */ function ninetytwo_notify_get_notifier_form_html( $product, $parent_id, $is_variation, $product_id, $product_name, $product_url, $product_tags, $similar_products_on ) { $store_name = get_bloginfo( 'name' ); $heading = $similar_products_on ? __( 'Notify me when this or a similar product is available', '92-notify-for-woocommerce' ) : __( 'Notify me when this product is available', '92-notify-for-woocommerce' ); $description = $similar_products_on ? __( 'Enter your email and we\'ll notify you when this product is back in stock or a similar product becomes available.', '92-notify-for-woocommerce' ) : __( 'Enter your email and we\'ll notify you when this product is back in stock.', '92-notify-for-woocommerce' ); /* translators: %s: store name */ $consent_text = sprintf( __( 'I agree to receive product availability emails from %s.', '92-notify-for-woocommerce' ), $store_name ); $button_text = __( 'Notify Me', '92-notify-for-woocommerce' ); $heading = apply_filters( 'ninetytwo_notify_form_heading', $heading, $product ); $description = apply_filters( 'ninetytwo_notify_form_description', $description, $product ); $button_text = apply_filters( 'ninetytwo_notify_form_button_text', $button_text, $product ); $privacy_policy_url = function_exists( 'get_privacy_policy_url' ) ? get_privacy_policy_url() : ''; $form_id = 'ntn-form-' . $product_id; ob_start(); ?>