VariationSwatches
1 week ago
Wishlist
1 week ago
AdminMessage.php
1 week ago
AjaxSearch.php
1 week ago
Backorder.php
1 week ago
CatalogMode.php
1 year ago
Compare.php
1 week ago
DefaultTemplates.php
2 years ago
FlashSalesCountdown.php
1 year ago
MenuCart.php
4 months ago
MobilePanel.php
1 week ago
MultiStep.php
1 year ago
Notifications.php
1 week ago
QuickView.php
1 week ago
RecentlyViewedProducts.php
1 year ago
RenameLabel.php
4 months ago
Shopify.php
1 year ago
SingleAjaxAddToCart.php
1 week ago
SizeChart.php
1 week ago
StickyAddToCart.php
1 week ago
Backorder.php
231 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Backorder. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\Modules; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | class Backorder { |
| 13 | /** |
| 14 | * Init. |
| 15 | * |
| 16 | * @since 1.2.0 |
| 17 | */ |
| 18 | public static function init() { |
| 19 | |
| 20 | if ( ! sp_get_module_settings( 'backorder', 'status' ) ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | add_action( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'set_order_limit_single' ), 10, 3 ); |
| 25 | add_action( 'woocommerce_update_cart_validation', array( __CLASS__, 'set_order_limit_cart' ), 10, 4 ); |
| 26 | add_filter( 'woocommerce_get_availability_text', array( __CLASS__, 'availability_message' ), 10, 2 ); |
| 27 | add_action( 'woocommerce_product_options_stock_status', array( __CLASS__, 'add_fields' ) ); |
| 28 | add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'save_meta_box' ), 10, 1 ); |
| 29 | add_filter( 'sp_backorder_limit', array( __CLASS__, 'get_product_backorder_limit' ), 10, 3 ); |
| 30 | add_filter( 'sp_backorder_date', array( __CLASS__, 'get_product_backorder_date' ), 10, 2 ); |
| 31 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * enqueue scripts. |
| 36 | * |
| 37 | * @since 1.1.0 |
| 38 | */ |
| 39 | public static function enqueue_scripts() { |
| 40 | wp_enqueue_script( 'sp-backorder-admin' ); |
| 41 | wp_enqueue_style( 'sp-backorder-admin' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set add to cart limit. |
| 46 | * |
| 47 | * @since 1.1.0 |
| 48 | */ |
| 49 | public static function set_order_limit_single( $passed, $product_id, $quantity ) { |
| 50 | |
| 51 | if ( ! self::is_onbackorder( $product_id ) ) { |
| 52 | return $passed; |
| 53 | } |
| 54 | |
| 55 | $cart_items_count = ! empty( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0; |
| 56 | $total_count = $cart_items_count + $quantity; |
| 57 | $limit = apply_filters( 'sp_backorder_limit', sp_get_module_settings( 'backorder', 'backorder_limit' ), $product_id, false ); |
| 58 | |
| 59 | if ( $cart_items_count >= $limit || $total_count > $limit ) { |
| 60 | |
| 61 | $passed = false; |
| 62 | |
| 63 | /* translators: %d: available limit number */ |
| 64 | wc_add_notice( sprintf( __( 'The maximum backorder limit has reached. (%d available)', 'shop-press' ), $limit ), 'error' ); |
| 65 | } |
| 66 | |
| 67 | return $passed; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set update cart limit. |
| 72 | * |
| 73 | * @since 1.1.0 |
| 74 | */ |
| 75 | public static function set_order_limit_cart( $passed, $cart_item_key, $values, $quantity ) { |
| 76 | |
| 77 | if ( ! self::is_onbackorder( $values['product_id'] ) ) { |
| 78 | return $passed; |
| 79 | } |
| 80 | |
| 81 | $cart_items_count = ! empty( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0; |
| 82 | $original_quantity = $values['quantity']; |
| 83 | $total_count = $cart_items_count - $original_quantity + $quantity; |
| 84 | $limit = apply_filters( 'sp_backorder_limit', sp_get_module_settings( 'backorder', 'backorder_limit' ), $values['product_id'], $cart_item_key ); |
| 85 | |
| 86 | if ( $cart_items_count > $limit || $total_count > $limit ) { |
| 87 | |
| 88 | $passed = false; |
| 89 | |
| 90 | /* translators: %d: available limit number */ |
| 91 | wc_add_notice( sprintf( __( 'The maximum backorder limit has reached. (%d available)', 'shop-press' ), $limit ), 'error' ); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | return $passed; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check if the product is on backorder. |
| 100 | * |
| 101 | * @since 1.1.0 |
| 102 | * |
| 103 | * @param int $product_id |
| 104 | * |
| 105 | * @return bool |
| 106 | */ |
| 107 | public static function is_onbackorder( $product_id ) { |
| 108 | $product = wc_get_product( $product_id ); |
| 109 | |
| 110 | if ( method_exists( $product, 'get_stock_status' ) && $product->get_stock_status() === 'onbackorder' ) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add filter to backorder limit for a specific product. |
| 119 | * |
| 120 | * @since 1.1.0 |
| 121 | */ |
| 122 | public static function get_product_backorder_limit( $limit, $product_id, $cart_item_key ) { |
| 123 | |
| 124 | if ( $product_id ) { |
| 125 | |
| 126 | $meta = get_post_meta( $product_id, 'sp_backorder_limit', true ); |
| 127 | $limit = ( isset( $meta ) && ! empty( $meta ) ) ? $meta : $limit; |
| 128 | } |
| 129 | |
| 130 | if ( $cart_item_key ) { |
| 131 | |
| 132 | $cart_item = WC()->cart->get_cart_item( $cart_item_key ); |
| 133 | $meta = get_post_meta( $cart_item['product_id'], 'sp_backorder_limit', true ); |
| 134 | $limit = ( isset( $meta ) && ! empty( $meta ) ) ? $meta : $limit; |
| 135 | } |
| 136 | |
| 137 | return $limit; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add filter to backorder date for a specific product. |
| 142 | * |
| 143 | * @since 1.1.0 |
| 144 | */ |
| 145 | public static function get_product_backorder_date( $date, $product_id ) { |
| 146 | $meta = get_post_meta( $product_id, 'sp_backorder_date', true ); |
| 147 | |
| 148 | if ( isset( $meta ) && ! empty( $meta ) ) { |
| 149 | return $meta; |
| 150 | } |
| 151 | |
| 152 | return $date; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Display availability message. |
| 157 | * |
| 158 | * @since 1.1.0 |
| 159 | */ |
| 160 | public static function availability_message( $availability, $product ) { |
| 161 | $date = apply_filters( 'sp_backorder_date', sp_get_module_settings( 'backorder', 'backorder_date' ), $product->get_id() ); |
| 162 | $date = gmdate( 'F j Y', strtotime( $date ) ); |
| 163 | |
| 164 | $message = str_replace( '{date}', $date, sp_get_module_settings( 'backorder', 'backorder_msg' ) ); |
| 165 | |
| 166 | if ( $product->is_on_backorder() || $product->backorders_allowed() ) { |
| 167 | return $message; |
| 168 | } |
| 169 | |
| 170 | return $availability; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Add custom fields to product inventory. |
| 175 | * |
| 176 | * @since 1.1.0 |
| 177 | */ |
| 178 | public static function add_fields() { |
| 179 | $product_id = get_the_id(); |
| 180 | $product = wc_get_product( $product_id ); |
| 181 | $backorder_limit = get_post_meta( $product_id, 'sp_backorder_limit', true ); |
| 182 | $backorder_date = get_post_meta( $product_id, 'sp_backorder_date', true ); |
| 183 | $manage_stock = get_post_meta( $product_id, '_manage_stock', true ); |
| 184 | $stock_status = get_post_meta( $product_id, '_stock_status', true ); |
| 185 | $limit = apply_filters( 'sp_backorder_limit', sp_get_module_settings( 'backorder', 'backorder_limit' ), $product_id, false ); |
| 186 | |
| 187 | $display_backorder = ( self::is_onbackorder( $product_id ) && $product->is_type( 'simple' ) ) || $manage_stock === 'yes'; |
| 188 | |
| 189 | ?> |
| 190 | <div class="sp-backorder-fields <?php echo ( $display_backorder && 'onbackorder' === $stock_status ) ? esc_attr( 'sp-backorder-fields-show' ) : ''; ?>"> |
| 191 | <?php |
| 192 | |
| 193 | $field = array( |
| 194 | 'id' => 'sp_backorder_limit', |
| 195 | 'value' => $backorder_limit, |
| 196 | /* translators: %d: default limit number */ |
| 197 | 'placeholder' => sprintf( __( 'Default Limit (%d)', 'shop-press' ), $limit ), |
| 198 | 'label' => __( 'Backorder Limit', 'shop-press' ), |
| 199 | ); |
| 200 | |
| 201 | woocommerce_wp_text_input( $field ); |
| 202 | |
| 203 | ?> |
| 204 | <p class="form-field sp_backorder_date_field "> |
| 205 | <label for="sp_backorder_date"><?php esc_html_e( 'Backorder Date', 'shop-press' ); ?></label> |
| 206 | <input type="date" name="sp_backorder_date" id="sp_backorder_date" value="<?php echo esc_attr( $backorder_date ); ?>"> |
| 207 | </p> |
| 208 | </div> |
| 209 | <?php |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Save custom fields. |
| 214 | * |
| 215 | * @since 1.1.0 |
| 216 | */ |
| 217 | public static function save_meta_box( $post_id ) { |
| 218 | |
| 219 | if ( ! isset( $_REQUEST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['woocommerce_meta_nonce'] ) ), 'woocommerce_save_data' ) ) { |
| 220 | return; |
| 221 | } |
| 222 | if ( isset( $_REQUEST['sp_backorder_limit'] ) ) { |
| 223 | update_post_meta( $post_id, 'sp_backorder_limit', sanitize_text_field( wp_unslash( $_REQUEST['sp_backorder_limit'] ) ) ); |
| 224 | } |
| 225 | |
| 226 | if ( isset( $_REQUEST['sp_backorder_date'] ) ) { |
| 227 | update_post_meta( $post_id, 'sp_backorder_date', sanitize_text_field( wp_unslash( $_REQUEST['sp_backorder_date'] ) ) ); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 |