default.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | use ShopEngine\Utils\Helper; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | $post_type = get_post_type(); |
| 8 | $product = \ShopEngine\Widgets\Products::instance()->get_product($post_type); |
| 9 | |
| 10 | $icon = ''; |
| 11 | $stock_status = $product->get_stock_status(); |
| 12 | $availability = $product->get_availability(); |
| 13 | |
| 14 | if($stock_status == 'instock') : |
| 15 | |
| 16 | $icon = isset($settings['shopengine_pstock_in_stock_icon']) ? $settings['shopengine_pstock_in_stock_icon'] : ''; |
| 17 | |
| 18 | elseif($stock_status == 'outofstock') : |
| 19 | |
| 20 | $icon = isset($settings['shopengine_pstock_out_of_stock_icon']) ? $settings['shopengine_pstock_out_of_stock_icon'] : ''; |
| 21 | |
| 22 | elseif($stock_status == 'onbackorder') : |
| 23 | |
| 24 | $icon = isset($settings['shopengine_pstock_available_on_backorder_icon']) ? $settings['shopengine_pstock_available_on_backorder_icon'] : ''; |
| 25 | |
| 26 | endif; |
| 27 | |
| 28 | $default_icon_html = ''; |
| 29 | if(!empty($icon)) { |
| 30 | ob_start(); |
| 31 | \Elementor\Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']); |
| 32 | $default_icon_html = ob_get_clean(); |
| 33 | } |
| 34 | $default_stock_data = [ |
| 35 | 'stock_status' => $stock_status, |
| 36 | 'availability' => $availability['availability'] ?? '', |
| 37 | 'class' => $availability['class'] ?? '', |
| 38 | 'icon' => $default_icon_html, |
| 39 | ]; |
| 40 | |
| 41 | // Prepare variation data for variable products |
| 42 | $variation_data = []; |
| 43 | if ($product->is_type('variable')) { |
| 44 | $variations = $product->get_available_variations(); |
| 45 | foreach ($variations as $variation) { |
| 46 | $variation_product = wc_get_product($variation['variation_id']); |
| 47 | $variation_stock_status = $variation_product->get_stock_status(); |
| 48 | $variation_icon = ''; |
| 49 | |
| 50 | // Map variation stock status to icon |
| 51 | if ($variation_stock_status == 'instock') { |
| 52 | $variation_icon = isset($settings['shopengine_pstock_in_stock_icon']) ? $settings['shopengine_pstock_in_stock_icon'] : ''; |
| 53 | } elseif ($variation_stock_status == 'outofstock') { |
| 54 | $variation_icon = isset($settings['shopengine_pstock_out_of_stock_icon']) ? $settings['shopengine_pstock_out_of_stock_icon'] : ''; |
| 55 | } elseif ($variation_stock_status == 'onbackorder') { |
| 56 | $variation_icon = isset($settings['shopengine_pstock_available_on_backorder_icon']) ? $settings['shopengine_pstock_available_on_backorder_icon'] : ''; |
| 57 | } |
| 58 | |
| 59 | // Pre-render variation icon HTML |
| 60 | $variation_icon_html = ''; |
| 61 | if (!empty($variation_icon)) { |
| 62 | ob_start(); |
| 63 | \Elementor\Icons_Manager::render_icon($variation_icon, ['aria-hidden' => 'true']); |
| 64 | $variation_icon_html = ob_get_clean(); |
| 65 | } |
| 66 | |
| 67 | $variation_data[$variation['variation_id']] = [ |
| 68 | 'stock_status' => $variation_stock_status, |
| 69 | 'availability' => $variation_product->get_availability()['availability'] ?? '', |
| 70 | 'class' => $variation_product->get_availability()['class'] ?? '', |
| 71 | 'icon' => $variation_icon_html, |
| 72 | ]; |
| 73 | } |
| 74 | } |
| 75 | ?> |
| 76 | |
| 77 | <div class="shopengine-product-stock" data-variations='<?php echo wp_json_encode($variation_data); ?>' data-default-stock='<?php echo wp_json_encode($default_stock_data); ?>'> |
| 78 | |
| 79 | <?php if($post_type == \ShopEngine\Core\Template_Cpt::TYPE) : |
| 80 | |
| 81 | $icons = [ |
| 82 | 'in_stock_icon' => isset($settings['shopengine_pstock_in_stock_icon']) ? $this->_get_icon($settings['shopengine_pstock_in_stock_icon']) : '', |
| 83 | 'out_of_stock_icon' => isset($settings['shopengine_pstock_out_of_stock_icon']) ? $this->_get_icon($settings['shopengine_pstock_out_of_stock_icon']) : '', |
| 84 | 'available_on_backorder_icon' => isset($settings['shopengine_pstock_available_on_backorder_icon']) ? $this->_get_icon($settings['shopengine_pstock_available_on_backorder_icon']) : '', |
| 85 | ]; |
| 86 | |
| 87 | $stock_type = $settings['shopengine_pstock_stock_type']; |
| 88 | $stock_type = in_array($stock_type, array_keys($this->stock_types())) ? $stock_type : 'in_stock'; // Validate Stock Type. |
| 89 | |
| 90 | $stock_class = str_replace('_', '-', $stock_type); |
| 91 | $stock_text = isset($settings[$stock_type . '_text']) ? $settings[$stock_type . '_text'] : Self::stock_types()[$stock_type]; |
| 92 | $stock_icon = isset($icons[$stock_type . '_icon']) ? $icons[$stock_type . '_icon'] : ''; |
| 93 | |
| 94 | shopengine_content_render('<p class="stock ' . $stock_class . '" data-stock-status="' . esc_attr($stock_type) . '">' . $stock_icon . ' ' . $stock_text . '</p>'); |
| 95 | |
| 96 | else : ?> |
| 97 | <p class="stock <?php echo esc_attr(isset($availability['class']) ? $availability['class'] : ''); ?>" data-stock-status="<?php echo esc_attr($stock_status); ?>"> |
| 98 | |
| 99 | <?php |
| 100 | if(!empty($icon)) : |
| 101 | \Elementor\Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']); |
| 102 | endif; |
| 103 | |
| 104 | if ( $product->is_on_backorder() ) { |
| 105 | $stock_html = !empty($availability['availability']) ? $availability['availability'] : esc_html__('On backorder', 'shopengine'); |
| 106 | } elseif ( $product->is_in_stock() ) { |
| 107 | $stock_html = !empty($availability['availability']) ? $availability['availability'] : esc_html__('In Stock', 'shopengine'); |
| 108 | } else { |
| 109 | $stock_html = !empty($availability['availability']) ? $availability['availability'] : esc_html__('Out of stock', 'shopengine'); |
| 110 | } |
| 111 | |
| 112 | $availability_html = $availability['availability'] ?? ''; |
| 113 | echo wp_kses(apply_filters( 'woocommerce_stock_html', $stock_html, $availability_html, $product ), Helper::get_kses_array()); |
| 114 | |
| 115 | ?> |
| 116 | |
| 117 | </p> |
| 118 | |
| 119 | <?php endif; ?> |
| 120 | |
| 121 | </div> |
| 122 |