• Resolved adrian4fun

    (@adrian4fun)


    Hi helpers!
    Thanks for open this topic and I hope I can search a solution here.
    So, I have a clothes store and customers can enter an email to notify them about the product available, but only on product page.
    It’s possible to have place under the product on the home page, where will be the option “notify me about availability”?

    The the alert code on product what I found is:

    <?php
    
    class WOO_Product_Stock_Alert_Frontend {
    
        private $dc_plugin_settings;
    
        public function __construct() {
            // Get plugin settings
            $this->dc_plugin_settings = get_dc_plugin_settings();
            //enqueue scripts
            add_action('wp_enqueue_scripts', array(&$this, 'frontend_scripts'));
            //enqueue styles
            add_action('wp_enqueue_scripts', array(&$this, 'frontend_styles'));
    
            if (isset($this->dc_plugin_settings)) {
                if (isset($this->dc_plugin_settings['is_enable']) && $this->dc_plugin_settings['is_enable'] == 'Enable') {
                    // Hover style
                    add_action('wp_head', array($this, 'frontend_style'));
                    //HTML for getting customer email
                    add_action('woocommerce_single_product_summary', array($this, 'get_alert_form'), 30);
                }
            }
        }
    
        function frontend_scripts() {
            global $WOO_Product_Stock_Alert;
            $frontend_script_path = $WOO_Product_Stock_Alert->plugin_url . 'assets/frontend/js/';
    
            if (function_exists('is_product')) {
                if (is_product()) {
                    // Enqueue your frontend javascript from here
                    wp_enqueue_script('stock_alert_frontend_js', $frontend_script_path . 'frontend.js', array('jquery'), $WOO_Product_Stock_Alert->version, true);
                }
            }
        }
    
        function frontend_styles() {
            global $WOO_Product_Stock_Alert;
            $frontend_style_path = $WOO_Product_Stock_Alert->plugin_url . 'assets/frontend/css/';
    
            if (function_exists('is_product')) {
                if (is_product()) {
                    // Enqueue your frontend stylesheet from here
                    wp_enqueue_style('stock_alert_frontend_css', $frontend_style_path . 'frontend.css', array(), $WOO_Product_Stock_Alert->version);
                }
            }
        }
    
        function frontend_style() {
            $dc_settings = array();
            $button_background_color_onhover = $button_text_color_onhover = '';
    
            $dc_settings = $this->dc_plugin_settings;
            if (isset($dc_settings) && !empty($dc_settings)) {
                $button_background_color_onhover = !empty($dc_settings['button_background_color_onhover']) ? $dc_settings['button_background_color_onhover'] : '';
                $button_text_color_onhover = !empty($dc_settings['button_text_color_onhover']) ? $dc_settings['button_text_color_onhover'] : '';
                $button_border_color_onhover = !empty($dc_settings['button_border_color_onhover']) ? $dc_settings['button_border_color_onhover'] : '';
            }
    
            echo '<style>
    			button.alert_button_hover:hover, button.unsubscribe_button:hover {
    				background: ' . $button_background_color_onhover . ' !important;
    				color: ' . $button_text_color_onhover . ' !important;
    				border-color: ' . $button_border_color_onhover . ' !important;
    			}
    		</style>';
        }
    
        function get_alert_form() {
            global $WOO_Product_Stock_Alert, $product;
    
            if (empty($product))
                return;
            //var_dump(get_post_meta( $product->get_id(), 'no_of_subscribers', true ));
            $stock_interest = $alert_text_html = $button_html = '';
            $dc_settings = array();
            $alert_text = $button_text = $button_background_color = $button_border_color = $button_text_color = $unsubscribe_button_text = '';
            $alert_success = $alert_email_exist = $valid_email = $alert_unsubscribe_message = '';
    
            $dc_settings = $this->dc_plugin_settings;
    
            if (isset($dc_settings) && !empty($dc_settings)) {
                $alert_text = isset($dc_settings['alert_text']) ? $dc_settings['alert_text'] : __('Get an alert when the product is in stock:', 'woocommerce-product-stock-alert');
                $alert_text_color = isset($dc_settings['alert_text_color']) ? $dc_settings['alert_text_color'] : '';
                $button_text = isset($dc_settings['button_text']) ? $dc_settings['button_text'] : __('Get an alert', 'woocommerce-product-stock-alert');
                $unsubscribe_button_text = isset($dc_settings['unsubscribe_button_text']) ? $dc_settings['unsubscribe_button_text'] : __('Unsubscribe', 'woocommerce-product-stock-alert');
                $button_background_color = isset($dc_settings['button_background_color']) ? $dc_settings['button_background_color'] : '';
                $button_border_color = isset($dc_settings['button_border_color']) ? $dc_settings['button_border_color'] : '';
                $button_text_color = isset($dc_settings['button_text_color']) ? $dc_settings['button_text_color'] : '';
                $button_background_color_onhover = isset($dc_settings['button_background_color_onhover']) ? $dc_settings['button_background_color_onhover'] : '';
                $button_text_color_onhover = isset($dc_settings['button_text_color_onhover']) ? $dc_settings['button_text_color_onhover'] : '';
                $button_border_color_onhover = isset($dc_settings['button_border_color_onhover']) ? $dc_settings['button_border_color_onhover'] : '';
                $alert_success = isset($dc_settings['alert_success']) ? $dc_settings['alert_success'] : '';
                $alert_email_exist = isset($dc_settings['alert_email_exist']) ? $dc_settings['alert_email_exist'] : '';
                $valid_email = isset($dc_settings['valid_email']) ? $dc_settings['valid_email'] : '';
                $alert_unsubscribe_message = isset($dc_settings['alert_unsubscribe_message']) ? $dc_settings['alert_unsubscribe_message'] : '';
                $shown_interest_text = isset($dc_settings['shown_interest_text']) ? $dc_settings['shown_interest_text'] : __('Already %no_of_subscribed% persons shown interest.', 'woocommerce-product-stock-alert');
            }
            if (empty($alert_text)) {
                $alert_text = __('Get an alert when the product is in stock:', 'woocommerce-product-stock-alert');
            }
            if (empty($button_text)) {
                $button_text = __('Get an alert', 'woocommerce-product-stock-alert');
            }
            if (empty($alert_success)) {
                $alert_success = __('Thank you for your interest in <b>%product_title%</b>, you will receive an email alert when it becomes available.', 'woocommerce-product-stock-alert');
            }
            if (empty($alert_email_exist)) {
                $alert_email_exist = __('<b>%customer_email%</b> is already registered with <b>%product_title%</b>.', 'woocommerce-product-stock-alert');
            }
            if (empty($valid_email)) {
                $valid_email = __('Please enter a valid email id and try again.', 'woocommerce-product-stock-alert');
            }
            if (empty($alert_unsubscribe_message)) {
                $alert_unsubscribe_message = __('<b>%customer_email%</b> is successfully unregistered.', 'woocommerce-product-stock-alert');
            }
    
            $shown_interest_section = '';
            if (isset($this->dc_plugin_settings['is_enable_no_interest']) && $this->dc_plugin_settings['is_enable_no_interest'] == 'Enable' && get_no_subscribed_persons($product->get_id()) != 0) {
                if ($shown_interest_text) {
                    $shown_interest_text = str_replace("%no_of_subscribed%", get_no_subscribed_persons($product->get_id()), $shown_interest_text);
                    $shown_interest_section = '<p>' . $shown_interest_text . '</p>';
                }
            }
    
            if (!empty($alert_text)) {
                $alert_text_html = '<h6 style="color:' . $alert_text_color . '" class="subscribe_for_interest_text">' . $alert_text . '</h6>';
            } else {
                $alert_text_html = '<h6 class="subscribe_for_interest_text">' . $alert_text . '</h6>';
            }
    
            if (!empty($button_background_color) && !empty($button_border_color) && !empty($button_text_color) && !empty($button_background_color_onhover) && !empty($button_text_color_onhover) && !empty($button_border_color_onhover)) {
                $button_html = '<button style="background: ' . $button_background_color . '; color: ' . $button_text_color . '; border-color: ' . $button_border_color . '" class="stock_alert_button alert_button_hover" name="alert_button">' . $button_text . '</button>';
                $unsubscribe_button_html = '<button class="unsubscribe_button" style="background: ' . $button_background_color . '; color: ' . $button_text_color . '; border-color: ' . $button_border_color . '">' . $unsubscribe_button_text . '</button>';
            } else {
                $button_html = '<button class="stock_alert_button" name="alert_button">' . $button_text . '</button>';
                $unsubscribe_button_html = '<button class="unsubscribe_button">' . $unsubscribe_button_text . '</button>';
            }
    
            wp_localize_script(
                    'stock_alert_frontend_js', 'form_submission_text', array('alert_text_html' => $alert_text_html,
                'button_html' => $button_html,
                'alert_success' => $alert_success,
                'alert_email_exist' => $alert_email_exist,
                'valid_email' => $valid_email,
                'unsubscribe_button' => $unsubscribe_button_html,
                'alert_unsubscribe_message' => $alert_unsubscribe_message
            ));
    
            $user_email = '';
            if (is_user_logged_in()) {
                $current_user = wp_get_current_user();
                $user_email = $current_user->data->user_email;
            }
            $placeholder = __('Enter your email', 'woocommerce-product-stock-alert');
            $placeholder = apply_filters('wc_product_stock_alert_box_email_placeholder', $placeholder);
            $stock_interest .= '<div class="alert_container">
    								' . $alert_text_html . '
    								<input type="text" class="stock_alert_email" name="alert_email" value="' . $user_email . '" placeholder="' . $placeholder . '" />
    								' . $button_html . '
    								<input type="hidden" class="current_product_id" value="' . $product->get_id() . '" />
    								<input type="hidden" class="current_product_name" value="' . $product->get_title() . '" />
    								' . $shown_interest_section . '
    							</div>';
    
            if ($product->is_type('simple')) {
                if ($this->display_stock_alert_form($product)) {
                    do_action('woocommerce_product_stock_alert_form_before');
                    echo $stock_interest;
                    do_action('woocommerce_product_stock_alert_form_after');
                }
            } else if ($product->is_type('variable')) {
                $child_ids = array();
                $flag = 0;
                if ($product->has_child()) {
                    $child_ids = $product->get_children();
                    if (isset($child_ids) && !empty($child_ids)) {
                        if (isset($child_ids['visible'])) {
                            foreach ($child_ids['visible'] as $child_id) {
                                $child_obj = new WC_Product_Variation($child_id);
    
                                if ($this->display_stock_alert_form($child_obj)) {
                                    $flag = 1;
                                }
                            }
                        } elseif (isset($child_ids['all'])) {
                            foreach ($child_ids['all'] as $child_id) {
                                $child_obj = new WC_Product_Variation($child_id);
    
                                if ($this->display_stock_alert_form($child_obj)) {
                                    $flag = 1;
                                }
                            }
                        } else {
                            foreach ($child_ids as $child_id) {
                                $child_obj = new WC_Product_Variation($child_id);
                                if ($this->display_stock_alert_form($child_obj)) {
                                    $flag = 1;
                                }
                            }
                        }
                    }
                }
    
                if ($flag == 1) {
                    do_action('woocommerce_product_stock_alert_form_before');
                    echo $stock_interest;
                    do_action('woocommerce_product_stock_alert_form_after');
                }
            } elseif ($product->is_type('subscription')) {
                if ($this->display_stock_alert_form($product)) {
                    do_action('woocommerce_product_stock_alert_form_before');
                    echo $stock_interest;
                    do_action('woocommerce_product_stock_alert_form_after');
                }
            } else {
                if ($this->display_stock_alert_form($product)) {
                    do_action('woocommerce_product_stock_alert_form_before');
                    echo $stock_interest;
                    do_action('woocommerce_product_stock_alert_form_after');
                }
            }
        }
    
        function display_stock_alert_form($product) {
            $display_stock_alert_form = false;
            $dc_settings = $this->dc_plugin_settings;
    
            if ($product) { 
    //            print_r(array(
    //                'managing_stock' => $product->managing_stock(), 
    //                'get_stock_quantity' => $product->get_stock_quantity(),
    //                'get_manage_stock' => $product->get_manage_stock(), 
    //                'get_stock_status' => $product->get_stock_status(),
    //                'woocommerce_notify_no_stock_amount' => get_option('woocommerce_notify_no_stock_amount'), 
    //                'backorders_allowed' => $product->backorders_allowed()
    //                )
    //            );
    //            print_r($product->get_availability());
                $managing_stock = $product->managing_stock();
                $stock_quantity = $product->get_stock_quantity();
                $manage_stock = $product->get_manage_stock();
                $stock_status = $product->get_stock_status();
                $is_in_stock = $product->is_in_stock();
                $is_on_backorder = $product->is_on_backorder( 1 );
                
                if ( ! $is_in_stock ) {
                        $display_stock_alert_form = true;
                } elseif ( $managing_stock && $is_on_backorder && isset($dc_settings['is_enable_backorders']) && $dc_settings['is_enable_backorders'] == 'Enable' ) {
                        $display_stock_alert_form = true;
                } elseif ( $managing_stock ) {
                    if(get_option('woocommerce_notify_no_stock_amount')){
                        if($stock_quantity <= (int) get_option('woocommerce_notify_no_stock_amount') && isset($dc_settings['is_enable_backorders']) && $dc_settings['is_enable_backorders'] == 'Enable' ){
                            $display_stock_alert_form = true;
                        }
                    }
                }
                
                
                
    //            if (isset($stock_quantity) && $manage_stock) {
    //                if ($managing_stock && $stock_quantity <= (int) get_option('woocommerce_notify_no_stock_amount')) {
    //                    if ($product->backorders_allowed() && isset($dc_settings['is_enable_backorders']) && $dc_settings['is_enable_backorders'] == 'Enable') {
    //                        if ($stock_status == 'outofstock' || $stock_quantity <= 0) {
    //                            $display_stock_alert_form = true;
    //                        }
    //                    } else {
    //                        $display_stock_alert_form = true;
    //                    }
    //                } elseif ($stock_quantity <= 0) {
    //                    if ($product->backorders_allowed() && isset($dc_settings['is_enable_backorders']) && $dc_settings['is_enable_backorders'] == 'Enable') {
    //                        if ($stock_status == 'outofstock' || $stock_quantity <= 0) {
    //                            $display_stock_alert_form = true;
    //                        }
    //                    } else {
    //                        $display_stock_alert_form = true;
    //                    }
    //                }
    //            }
            }
    
            return $display_stock_alert_form;
        }
    
    }

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Hi, in order to add the Out of Stock field in the archive, please use [display_stock_alert_form] shortcode.

    Thread Starter adrian4fun

    (@adrian4fun)

    Hi, Thanks for response.
    Can You tell me what file or template should I look for?

    @adrian4fun, you don’t need to override any template file. All you have to use WooCommerce hook that will define where you want to display the form and then call the shortcode.

    Thread Starter adrian4fun

    (@adrian4fun)

    Oh ok, I hope I understand that. So, I used a Woo Visual Hook Guide plugin for see where is that hook.
    It shows: Hook Name: woocommerce_after_shop_loop_item

    /wp-content/plugins/woocommerce/includes/wc-template-functions.php
    Line No: 1111

    After that I should put a shortcode for alert? One problem is that, I dont know what name of shortcode I must use…

    Hi @adrian4fun, Please add the code in the function.php of the current active theme :

    add_action( 'woocommerce_after_shop_loop_item', 'add_stock_alert_form_shop_page' );
    function add_stock_alert_form_shop_page() {
    echo do_shortcode ('[display_stock_alert_form]');
    }
    Thread Starter adrian4fun

    (@adrian4fun)

    Thanks @dualcube for a reply. I put this code in function.php in child theme. Unfortunately, this code broken my shop. ??

    Kindly add the code at the end of your functions.php file before ?> tag.

    Thread Starter adrian4fun

    (@adrian4fun)

    @dualcube thanks for your patience ??
    I did it at functions.php in child theme.
    All code looks:

    <?php
    	
    function gt3_child_scripts() {
    	wp_enqueue_style( 'gt3-parent-style', get_template_directory_uri(). '/style.css' );
    }
    add_action( 'wp_enqueue_scripts', 'gt3_child_scripts' );
    
    /**
     * Your code here.
     *
     */
    

    I put code before
    /**
    * Your code here.
    *
    */

    Before / After is just fine.

    Thread Starter adrian4fun

    (@adrian4fun)

    @dualcube Yes, but after that I get 505 error in my shop.

    That means, there might be some blank space after the end of your PHP file after “?>” tag.

    Please check and delete the same.

    Thread Starter adrian4fun

    (@adrian4fun)

    @dualcube You Was right ??
    In fact, it works great, but in this shop I only need a permanent inscription under the product if it is not available, not only when I am hover on a product.

    In your solution also is problem, when you looking for higher produkt on list.

    Hi @adrian4fun, can you please share some screenshot explaining what you have now and what you are looking for.

    Thread Starter adrian4fun

    (@adrian4fun)

    Ok, so now your earlier solution looks like on this screen:

    View post on imgur.com

    When I hover on image, I have information about out of stock alert.
    But I need information before I hover on the image. Only description under the product if it is not available, like here:

    View post on imgur.com

    (red button means “Alert me when available”)

    Hi @adrian4fun, can you please get in touch with us over our forum, so that we can help you out further : https://wc-marketplace.com/support-forum/topic/hide-some-fields-on-the-add-product-page-2/#post-35038

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Notification of product availability on the main page’ is closed to new replies.