Home Forums WoodMart support forum Out of Stock Product label

Out of Stock Product label

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #592026

    Mike
    Participant

    Hi i have created a custom plugin that you can see in private content for custom stock status but i have a small issue with the Out of Stock Product label.

    When i set products to stock status: instore
    then the out of stock status label is appear on the product, screenshot: https://prnt.sc/_zyI9TPHr93S

    As the: instore status is acting like the instock status, the out of stock label it should not displayed.

    How can i solve this or better if can be done to display the Instore status label instead of out of stock?

    #592029

    Mike
    Participant

    i forgot to add the plugin code, please check

    
    <?php
    /**
     * Plugin Name: Custom Stock Delivery Status
     * Version: 1.0.4
     * Author: Mike 
     * Text Domain: custom-stock-delivery-status
     */
    
    namespace GoldenBath\CustomStockDeliveryStatus;
    
    if (!defined('ABSPATH')) {
        exit; // Exit if accessed directly
    }
    
    class CustomStockStatusHandler
    {
        private static $instance = null;
        private $custom_stock_statuses = array();
    
        private function __construct()
        {
            add_action('init', array($this, 'initializeCustomStockStatuses'));
            add_filter('woocommerce_product_stock_status_options', array($this, 'filterProductStockStatusOptions'));
            add_filter('woocommerce_get_availability_text', array($this, 'filterAvailabilityText'), 10, 2);
            add_filter('woocommerce_is_purchasable', array($this, 'validatePurchasable'), 10, 2);
            add_filter('woocommerce_get_availability_class', array($this, 'getStatusAvailabilityClass'), 10, 2);
            add_action('woocommerce_process_product_meta', array($this, 'addCustomStockStatusMeta'));
            add_action('wp_enqueue_scripts', array($this, 'enqueue_custom_styles'));
        }
    
        public static function getInstance()
        {
            if (self::$instance === null) {
                self::$instance = new self();
            }
            return self::$instance;
        }
    
        public function initializeCustomStockStatuses()
        {
            $this->custom_stock_statuses = array(
                'instock' => array(
                    'label' => __('Σε απόθεμα', 'custom-stock-delivery-status'),
                    'message' => __('1 έως 3 ημέρες', 'custom-stock-delivery-status'),
                    'tooltip' => __('Αυτό το προϊόν είναι σε απόθεμα στον προμηθευτή και διαθέσιμο για άμεση παραγγελία', 'custom-stock-delivery-status')
                ),
                'outofstock' => array(
                    'label' => __('Εξαντλημένο', 'custom-stock-delivery-status'),
                    'message' => __('Εξαντλημένο', 'custom-stock-delivery-status'),
                    'tooltip' => __('Αυτό το προϊόν έχει εξαντληθεί προς το παρόν', 'custom-stock-delivery-status')
                ),
                'onbackorder' => array(
                    'label' => __('Προπαραγγελία', 'custom-stock-delivery-status'),
                    'message' => __('Κατόπιν παραγγελίας', 'custom-stock-delivery-status'),
                    'tooltip' => __('Αυτό το προϊόν είναι κατόπιν παραγγελίας και θα αποσταλεί μόλις είναι διαθέσιμο', 'custom-stock-delivery-status')
                ),
                'instore' => array(
                    'label' => __('Ετοιμοπαράδοτο', 'custom-stock-delivery-status'),
                    'message' => __('Ετοιμοπαράδοτο', 'custom-stock-delivery-status'),
                    'tooltip' => __('Αυτό το προϊόν είναι ετοιμοπαράδοτο και άμεσα διαθέσιμο στο κατάστημά μας', 'custom-stock-delivery-status')
                ),
                'discontinued' => array(
                    'label' => __('Καταργήθηκε', 'custom-stock-delivery-status'),
                    'message' => __('Καταργήθηκε', 'custom-stock-delivery-status'),
                    'tooltip' => __('Αυτό το προϊόν έχει καταργηθεί απο τον κατασκευαστή και δεν είναι πλέον διαθέσιμο', 'custom-stock-delivery-status')
                )
            );
        }
    
        public function filterProductStockStatusOptions($status)
        {
            foreach ($this->custom_stock_statuses as $key => $value) {
                $status[$key] = $value['label'];
            }
            return $status;
        }
    
        public function filterAvailabilityText($availability, $product)
        {
            $stock_status = $product->get_stock_status();
            if (array_key_exists($stock_status, $this->custom_stock_statuses)) {
                $message = $this->custom_stock_statuses[$stock_status]['message'];
                $tooltip = $this->custom_stock_statuses[$stock_status]['tooltip'];
                return '<span class="availability-label">' . __('Διαθεσιμότητα:', 'custom-stock-delivery-status') . '</span> <span class="availability-status">' . esc_html($message) . '</span><span class="stock-icon" data-title="' . esc_attr($tooltip) . '"></span>';
            }
            return $availability;
        }
    
        public function validatePurchasable($purchasable, $product)
        {
            if ('discontinued' === $product->get_stock_status()) {
                $purchasable = false;
            }
            return $purchasable;
        }
    
        public function getStatusAvailabilityClass($availability_class, $product)
        {
            if (array_key_exists($product->get_stock_status(), $this->custom_stock_statuses)) {
                $availability_class = $product->get_stock_status();
            }
            return $availability_class;
        }
    
        public function enqueue_custom_styles()
        {
            wp_enqueue_style('custom-stock-status-styles', plugins_url('/css/custom-stock-status.css', __FILE__));
        }
    }
    
    CustomStockStatusHandler::getInstance();
    
    • This reply was modified 4 months, 2 weeks ago by Mike.
    #592175

    Hung Pham
    Keymaster

    Hi Mike,

    Thanks for reaching to us.

    Unfortunately, customization is out of our basic support.

    Thanks for understanding our limitations. Let me know if you have any questions.

    Kind Regards

    #592183

    Mike
    Participant

    Hi,
    i not asking you to edit my code or adjust it. I asking you why the theme is recognize different stock status than instock, onbackorder, outofstock like out of stock.

    This should not happen as there is no option to disable the out of stock label in theme.

    may i know the piece of code that shows the out of stock label in which file it is?

    #592215

    Mike
    Participant

    i have found the code part that is responsible for displaying the out of stock label, but i don’t understand why you have add a function that: if ( ! $product->is_in_stock
    then its display: $output[] = ‘<span class=”out-of-stock product-label”>’ . esc_html__( ‘Sold out’, ‘woodmart’ ) . ‘</span>’;

    if i understand this is check that when a product is out of stock its display the out of stock label

    here is the full part on the template-tags.php file

    
    if ( ! $product->is_in_stock() && 'thumbnail' === woodmart_get_opt( 'stock_status_position', 'thumbnail' ) ) {
    	$output[] = '<span class="out-of-stock product-label">' . esc_html__( 'Sold out', 'woodmart' ) . '</span>';
    }
    

    i was able to solve this by editing that part to this:

    
    if ( ! $product->is_in_stock() && 'thumbnail' === woodmart_get_opt( 'stock_status_position', 'thumbnail' ) ) {
        $show_sold_out = true;
    
        if ( $product->get_type() === 'variable' ) {
            $available_variations = $product->get_available_variations();
            foreach ( $available_variations as $variation ) {
                $variation_product = wc_get_product( $variation['variation_id'] );
                if ( $variation_product->get_stock_status() === 'instore' ) {
                    $show_sold_out = false;
                    break;
                }
            }
        } elseif ( $product->get_stock_status() === 'instore' ) {
            $show_sold_out = false;
        }
    
        if ( $show_sold_out ) {
            $output[] = '<span class="out-of-stock product-label">' . esc_html__( 'Sold out', 'woodmart' ) . '</span>';
        }
    }
    

    But the problem is that if the theme get updated the code will be removed

    • This reply was modified 4 months, 2 weeks ago by Mike.
    #592413

    Hung Pham
    Keymaster

    Hi Mike,

    First of all, I’m really sorry have taken long time to reply you due to the weekend.

    Glad to hear your issue has been resolved. Keep us in mind for future questions and concerns, we’re always here to help!

    Regards,

    #592451

    Mike
    Participant

    Hi, as i told you on previous reply, i was able to solve it but its a temporary fix as if i update the theme then it will be reverted back to original state, so there are 2 option.
    Solution 1: You as the theme author modificate the code so it will not add the out of stock label if there is a custom stock status

    Solution 2: give me an alternative code that i can override that part of theme code with an filter action hook

    #592760

    Hung Pham
    Keymaster

    Hi Mike,

    I’ve passed this topic to one of our developers to take a look.

    Regards,

    #594200

    Mike
    Participant

    Hi again.
    Will any of the devs will answer to this thread?

    #594241

    Hung Pham
    Keymaster

    Hi Mike,

    I’ve passed this topic to one of our developers, and I will let you know once get an answer in the Monday.

    Regards,

    #594764

    Hung Pham
    Keymaster

    Hi Mike,

    Thanks for your patience.

    Here are replies from our developer: Unfortunately, WoodMart theme does not support custom stock status, but we can provide code that will remove the out of stock label. Also, if you need to add some custom labels, you can also do so using this code.

    add_filter('woodmart_product_label_output', function ( $labels ) {
    	foreach ( $labels as $key => $label ) {
    		if ( strpos( $label, 'out-of-stock product-label') !== false ) {
    			unset( $labels[$key] );
    		}
    	}
    
    	// Your custom code here.
    
    	return $labels;
    });

    Regards,

    #594847

    Mike
    Participant

    Hi, thanks for the function!.

    Btw i was able to solve the problem with the code part of my plugin below

    
    add_filter('woodmart_product_label_output', array($this, 'modify_woodmart_labels'), 10, 1);
        public function modify_woodmart_labels($output)
        {
            global $product;
            $stock_status = $product->get_stock_status();
            $has_instore = false;
            $all_outofstock = true;
            $all_discontinued = true;
    
            // Handle simple products
            if ($stock_status === 'instore') {
                $this->remove_out_of_stock_label($output);
                $output[] = '<span class="instore product-label">' . esc_html__('In Store', 'custom-stock-delivery-status') . '</span>';
                return $output;
            }
            if ($stock_status === 'discontinued') {
                $this->remove_out_of_stock_label($output);
                $output[] = '<span class="discontinued product-label">' . esc_html__('Discontinued', 'custom-stock-delivery-status') . '</span>';
                return $output;
            }
    
            // Handle variable products
            if ($product->is_type('variable')) {
                $variations = $product->get_available_variations();
                foreach ($variations as $variation) {
                    $variation_obj = wc_get_product($variation['variation_id']);
                    $variation_status = $variation_obj->get_stock_status();
                    if ($variation_status === 'instore') {
                        $has_instore = true;
                        $all_outofstock = false;
                        $all_discontinued = false;
                        break; // If any variation is instore, we prioritize it
                    }
                    if ($variation_status !== 'outofstock') {
                        $all_outofstock = false;
                    }
                    if ($variation_status !== 'discontinued') {
                        $all_discontinued = false;
                    }
                }
                if ($has_instore) {
                    $this->remove_out_of_stock_label($output);
                    $output[] = '<span class="instore product-label">' . esc_html__('In Store', 'custom-stock-delivery-status') . '</span>';
                } elseif ($all_discontinued) {
                    $this->remove_out_of_stock_label($output);
                    $output[] = '<span class="discontinued product-label">' . esc_html__('Discontinued', 'custom-stock-delivery-status') . '</span>';
                } elseif ($all_outofstock) {
                    $this->remove_out_of_stock_label($output);
                    $output[] = '<span class="out-of-stock product-label">' . esc_html__('Sold out', 'woodmart') . '</span>';
                }
            } elseif ($stock_status === 'outofstock') {
                // Handle simple products that are out of stock
                $this->remove_out_of_stock_label($output);
                $output[] = '<span class="out-of-stock product-label">' . esc_html__('Sold out', 'woodmart') . '</span>';
            }
            return $output;
        }
    
        private function remove_out_of_stock_label(&$output)
        {
            foreach ($output as $key => $label) {
                if (strpos($label, 'out-of-stock') !== false) {
                    unset($output[$key]);
                }
            }
        }
    
    #595190

    Hung Pham
    Keymaster

    Hi Mike,

    Glad to hear your issue has been resolved and shared the solutions. Keep us in mind for future questions and concerns, we’re always here to help!

    Regards,

Viewing 13 posts - 1 through 13 (of 13 total)