/** * Plugin Name: IndieMoto Cart Product Availability * Description: Adds availability information to cart items based on inventory and selected shipping method. * Version: 1.4 * Author: Brad Molen */ if (!defined('ABSPATH')) { exit; // Exit if accessed directly } class IndieMoto_Cart_Product_Availability { public function __construct() { // Hook to add inventory status to cart items add_filter('woocommerce_get_item_data', [$this, 'add_inventory_status_to_cart_items'], 10, 2); // Ensure shipping method is updated in the session add_action('woocommerce_checkout_update_order_review', [$this, 'update_shipping_method_in_session']); add_action('woocommerce_cart_updated', [$this, 'update_shipping_method_in_session']); } /** * Ensures that the chosen shipping method is updated in the session. */ public function update_shipping_method_in_session() { if (WC()->session && WC()->shipping) { $packages = WC()->shipping->get_packages(); if (!empty($packages)) { $chosen_methods = WC()->session->get('chosen_shipping_methods'); if (!$chosen_methods || !is_array($chosen_methods)) { WC()->session->set('chosen_shipping_methods', array_map(function ($package) { return key($package['rates']); }, $packages)); } } } } /** * Retrieves the chosen shipping method reliably. * * @return string|null The chosen shipping method or null if not found. */ private function get_chosen_shipping_method() { $chosen_methods = WC()->session->get('chosen_shipping_methods'); if (is_array($chosen_methods) && !empty($chosen_methods[0])) { return $chosen_methods[0]; } return null; } /** * Adds inventory availability information to cart items. * * @param array $item_data Existing cart item data. * @param array $cart_item Cart item details. * @return array Modified cart item data. */ public function add_inventory_status_to_cart_items($item_data, $cart_item) { $product_id = $cart_item['product_id']; $product = wc_get_product($product_id); // Determine drop-ship eligibility $is_eligible = get_post_meta($product_id, 'wps_drop_ship_eligible', true); // Get the chosen shipping method $chosen_shipping_method = $this->get_chosen_shipping_method(); // Determine inventory status $woo_inventory = $product->get_stock_quantity(); $sku = $product->get_sku(); $warehouse_inventory_output = do_shortcode('[warehouse_inventory sku="' . esc_attr($sku) . '"]'); // Parse warehouse inventory value using strip_tags and preg_replace $warehouse_inventory_output = strip_tags($warehouse_inventory_output); $warehouse_stock = (int) preg_replace('/[^0-9]/', '', $warehouse_inventory_output); // Add drop-ship eligibility warning if (!in_array(strtolower($is_eligible), ['1', 'true', 'yes'], true)) { $item_data[] = [ 'name' => __('Shipping Warning', 'woocommerce'), 'value' => '' . __('This item must be picked up in-store.', 'woocommerce') . '', ]; } // Inventory logic for Pickup if ($chosen_shipping_method && strpos($chosen_shipping_method, 'pickup') !== false) { if ($woo_inventory > 0) { $item_data[] = [ 'name' => __('Availability', 'woocommerce'), 'value' => __('Pickup Today', 'woocommerce'), ]; } elseif ($warehouse_stock > 0) { $item_data[] = [ 'name' => __('Availability', 'woocommerce'), 'value' => __('Pickup in 1-3 days', 'woocommerce'), ]; } else { $item_data[] = [ 'name' => __('Availability', 'woocommerce'), 'value' => '' . __('Backordered', 'woocommerce') . '', ]; } } // Inventory logic for Shipping elseif ($chosen_shipping_method && strpos($chosen_shipping_method, 'shipping') !== false) { if ($woo_inventory > 0 || $warehouse_stock > 0) { $item_data[] = [ 'name' => __('Availability', 'woocommerce'), 'value' => __('Shipping today', 'woocommerce'), ]; } else { $item_data[] = [ 'name' => __('Availability', 'woocommerce'), 'value' => '' . __('Backordered', 'woocommerce') . '', ]; } } return $item_data; } } // Instantiate the plugin new IndieMoto_Cart_Product_Availability(); {"id":43425,"date":"2024-12-26T23:45:58","date_gmt":"2024-12-27T05:45:58","guid":{"rendered":"https:\/\/indie-moto.com\/product\/motion-pro-armor-coat-clutch-lw-cable-67-0160\/"},"modified":"2025-01-06T14:57:32","modified_gmt":"2025-01-06T20:57:32","slug":"motion-pro-armor-coat-clutch-lw-cable-67-0160","status":"publish","type":"product","link":"https:\/\/indie-moto.com\/product\/motion-pro-armor-coat-clutch-lw-cable-67-0160\/","title":{"rendered":"MOTION PRO ARMOR COAT CLUTCH LW CABLE – 67-0160"},"content":{"rendered":"
None<\/p>\n
ARMOR COAT CLUTCH LW CABLE<\/p>\n","protected":false},"featured_media":43446,"template":"","meta":[],"product_cat":[70],"product_tag":[63],"class_list":{"0":"post-43425","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-cable-hydraulic-control-lines","7":"product_tag-hard-drive","8":"pa_cable-type-clutch","9":"pa_clutch-longitudinally-wound","10":"pa_finish-armor-coat","11":"pa_product-type-cable-hydraulic-control-lines","13":"first","14":"onbackorder","15":"taxable","16":"shipping-taxable","17":"purchasable","18":"product-type-simple"},"alg_wc_cog_cost":"38.18","_links":{"self":[{"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/product\/43425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/media\/43446"}],"wp:attachment":[{"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/media?parent=43425"}],"wp:term":[{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/product_cat?post=43425"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/indie-moto.com\/wp-json\/wp\/v2\/product_tag?post=43425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}