Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /** * Plugin Name: Custom Inventory Shipping * Description: Manage inventory ba..

Decoded Output download

<?php 
/** 
 * Plugin Name: Custom Inventory Shipping 
 * Description: Manage inventory based on shipping methods. 
 * Version: 1.0 
 * Author: Your Name 
 * Text Domain: custom-inventory-shipping 
 */ 
 
// Exit if accessed directly. 
if (!defined('ABSPATH')) { 
    exit; 
} 
 
// Add custom inventory fields to the product inventory tab 
add_action('woocommerce_product_options_inventory_product_data', 'add_custom_inventory_fields'); 
function add_custom_inventory_fields() { 
    woocommerce_wp_text_input(array( 
        'id'          => '_inventory_a', 
        'label'       => __('Inventory A', 'custom-inventory-shipping'), 
        'desc_tip'    => true, 
        'description' => __('Enter the custom inventory value for Inventory A.', 'custom-inventory-shipping'), 
        'type'        => 'number', 
    )); 
    woocommerce_wp_text_input(array( 
        'id'          => '_inventory_b', 
        'label'       => __('Inventory B', 'custom-inventory-shipping'), 
        'desc_tip'    => true, 
        'description' => __('Enter the custom inventory value for Inventory B.', 'custom-inventory-shipping'), 
        'type'        => 'number', 
    )); 
} 
 
// Save custom inventory fields 
add_action('woocommerce_process_product_meta', 'save_custom_inventory_fields'); 
function save_custom_inventory_fields($post_id) { 
    $inventory_a = isset($_POST['_inventory_a']) ? sanitize_text_field($_POST['_inventory_a']) : ''; 
    $inventory_b = isset($_POST['_inventory_b']) ? sanitize_text_field($_POST['_inventory_b']) : ''; 
    update_post_meta($post_id, '_inventory_a', $inventory_a); 
    update_post_meta($post_id, '_inventory_b', $inventory_b); 
} 
 
// Register admin menu 
add_action('admin_menu', 'custom_inventory_shipping_menu'); 
function custom_inventory_shipping_menu() { 
    add_menu_page( 
        'Custom Inventory Shipping Settings', 
        'Inventory Shipping', 
        'manage_options', 
        'custom-inventory-shipping', 
        'custom_inventory_shipping_settings_page', 
        'dashicons-admin-generic', 
        20 
    ); 
} 
 
function custom_inventory_shipping_settings_page() { 
    // Check if the user has the required capability 
    if (!current_user_can('manage_options')) { 
        return; 
    } 
 
    // Process form submission 
    if (isset($_POST['custom_inventory_shipping_settings_submit'])) { 
        // Verify nonce 
        check_admin_referer('custom_inventory_shipping_settings'); 
 
        // Sanitize and update the inventory instances options 
        $inventory_a_instances = isset($_POST['inventory_a_instances']) ? array_map('absint', $_POST['inventory_a_instances']) : []; 
        $inventory_b_instances = isset($_POST['inventory_b_instances']) ? array_map('absint', $_POST['inventory_b_instances']) : []; 
        update_option('custom_inventory_a_instances', $inventory_a_instances); 
        update_option('custom_inventory_b_instances', $inventory_b_instances); 
 
        // Sanitize and update the test input option 
        $test_input = isset($_POST['test_input']) ? sanitize_text_field($_POST['test_input']) : ''; 
        update_option('test_input', $test_input); 
 
        // Display success message 
        add_settings_error('custom_inventory_shipping_settings', 'settings_saved', 'Settings saved.', 'success'); 
    } 
 
    // Retrieve saved options from the database 
    $inventory_a_instances = get_option('custom_inventory_a_instances', []); 
    $inventory_b_instances = get_option('custom_inventory_b_instances', []); 
    $test_input = get_option('test_input', ''); 
 
    // Render the settings page 
    ?> 
    <div class="wrap"> 
        <h1>Custom Inventory Shipping Settings</h1> 
        <?php settings_errors('custom_inventory_shipping_settings'); ?> 
        <form method="post" action=""> 
            <?php wp_nonce_field('custom_inventory_shipping_settings'); ?> 
            <table class="form-table"> 
                <tr valign="top"> 
                    <th scope="row">Inventory A Shipping Methods</th> 
                    <td> 
                        <select multiple="multiple" name="inventory_a_instances[]" style="width: 100%;"> 
                            <?php echo get_shipping_method_instance_options($inventory_a_instances); ?> 
                        </select> 
                        <p class="description">Select the shipping methods that should reduce Inventory A.</p> 
                    </td> 
                </tr> 
                <tr valign="top"> 
                    <th scope="row">Inventory B Shipping Methods</th> 
                    <td> 
                        <select multiple="multiple" name="inventory_b_instances[]" style="width: 100%;"> 
                            <?php echo get_shipping_method_instance_options($inventory_b_instances); ?> 
                        </select> 
                        <p class="description">Select the shipping methods that should reduce Inventory B.</p> 
                    </td> 
                </tr> 
                <tr valign="top"> 
                    <th scope="row">Test Input</th> 
                    <td> 
                        <input type="text" name="test_input" value="<?php echo esc_attr($test_input); ?>"> 
                        <p class="description">Enter some text to test if it gets saved.</p> 
                    </td> 
                </tr> 
            </table> 
            <?php submit_button('Save Settings', 'primary', 'custom_inventory_shipping_settings_submit'); ?> 
        </form> 
    </div> 
    <?php 
} 
 
 
// Helper function to get shipping method instance options 
function get_shipping_method_instance_options($selected_instances) { 
    $options = ''; 
 
    // Fetch all shipping zones and their methods 
    $zones = WC_Shipping_Zones::get_zones(); 
    foreach ($zones as $zone) { 
        $zone_shipping_methods = $zone['shipping_methods']; 
        foreach ($zone_shipping_methods as $zone_shipping_method) { 
            $zone_instance_id = $zone_shipping_method->instance_id; 
            $zone_instance_title = $zone_shipping_method->title; 
            $selected = in_array($zone_instance_id, $selected_instances) ? 'selected="selected"' : ''; 
            $options .= sprintf('<option value="%s" %s>%s - %s</option>', $zone_instance_id, $selected, $zone['zone_name'], $zone_instance_title); 
        } 
    } 
 
    return $options; 
} 
 
// Adjust shipping methods based on inventory 
add_filter('woocommerce_package_rates', 'adjust_shipping_methods_based_on_inventory', 10, 2); 
function adjust_shipping_methods_based_on_inventory($rates, $package) { 
    $inventory_a_instances = get_option('custom_inventory_a_instances', []); 
    $inventory_b_instances = get_option('custom_inventory_b_instances', []); 
 
    foreach (WC()->cart->get_cart() as $cart_item) { 
        $product_id = $cart_item['product_id']; 
        $quantity = $cart_item['quantity']; 
 
        $inventory_a = get_post_meta($product_id, '_inventory_a', true); 
        $inventory_b = get_post_meta($product_id, '_inventory_b', true); 
 
        foreach ($rates as $rate_key => $rate) { 
            $instance_id = $rate->get_instance_id(); 
            if (in_array($instance_id, $inventory_a_instances) && ($inventory_a === '' || $inventory_a < $quantity)) { 
                unset($rates[$rate_key]); 
                wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error'); 
            } elseif (in_array($instance_id, $inventory_b_instances) && ($inventory_b === '' || $inventory_b < $quantity)) { 
                unset($rates[$rate_key]); 
                wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error'); 
            } 
        } 
    } 
    return $rates; 
} 
 
// Validate inventory before checkout 
add_action('woocommerce_checkout_process', 'validate_inventory_before_checkout'); 
function validate_inventory_before_checkout() { 
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); 
    $chosen_shipping_instance_id = isset($chosen_shipping_methods[0]) ? explode(':', $chosen_shipping_methods[0])[1] : ''; 
 
    $inventory_a_instances = get_option('custom_inventory_a_instances', []); 
    $inventory_b_instances = get_option('custom_inventory_b_instances', []); 
 
    foreach (WC()->cart->get_cart() as $cart_item) { 
        $product_id = $cart_item['product_id']; 
        $quantity = $cart_item['quantity']; 
 
        $inventory_a = get_post_meta($product_id, '_inventory_a', true); 
        $inventory_b = get_post_meta($product_id, '_inventory_b', true); 
 
        if (in_array($chosen_shipping_instance_id, $inventory_a_instances) && ($inventory_a === '' || $inventory_a < $quantity)) { 
            wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error'); 
        } elseif (in_array($chosen_shipping_instance_id, $inventory_b_instances) && ($inventory_b === '' || $inventory_b < $quantity)) { 
            wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error'); 
        } 
    } 
} 
 
// Adjust inventory after order is placed 
add_action('woocommerce_thankyou', 'adjust_inventory_based_on_zone_shipping_method'); 
function adjust_inventory_based_on_zone_shipping_method($order_id) { 
    if (!$order_id) return; 
 
    $order = wc_get_order($order_id); 
    $shipping_methods = $order->get_shipping_methods(); 
    $shipping_instance_id = ''; 
 
    foreach ($shipping_methods as $shipping_method_item) { 
        $shipping_instance_id = $shipping_method_item->get_instance_id(); 
        break; 
    } 
 
    $inventory_a_instances = get_option('custom_inventory_a_instances', []); 
    $inventory_b_instances = get_option('custom_inventory_b_instances', []); 
 
    foreach ($order->get_items() as $item) { 
        $product_id = $item->get_product_id(); 
        $quantity = $item->get_quantity(); 
 
        if (in_array($shipping_instance_id, $inventory_a_instances)) { 
            $inventory_a = get_post_meta($product_id, '_inventory_a', true); 
            if ($inventory_a !== '') { 
                $new_inventory_a = max(0, $inventory_a - $quantity); 
                update_post_meta($product_id, '_inventory_a', $new_inventory_a); 
            } 
        } elseif (in_array($shipping_instance_id, $inventory_b_instances)) { 
            $inventory_b = get_post_meta($product_id, '_inventory_b', true); 
            if ($inventory_b !== '') { 
                $new_inventory_b = max(0, $inventory_b - $quantity); 
                update_post_meta($product_id, '_inventory_b', $new_inventory_b); 
            } 
        } 
    } 
} ?>

Did this file decode correctly?

Original Code

<?php
/**
 * Plugin Name: Custom Inventory Shipping
 * Description: Manage inventory based on shipping methods.
 * Version: 1.0
 * Author: Your Name
 * Text Domain: custom-inventory-shipping
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

// Add custom inventory fields to the product inventory tab
add_action('woocommerce_product_options_inventory_product_data', 'add_custom_inventory_fields');
function add_custom_inventory_fields() {
    woocommerce_wp_text_input(array(
        'id'          => '_inventory_a',
        'label'       => __('Inventory A', 'custom-inventory-shipping'),
        'desc_tip'    => true,
        'description' => __('Enter the custom inventory value for Inventory A.', 'custom-inventory-shipping'),
        'type'        => 'number',
    ));
    woocommerce_wp_text_input(array(
        'id'          => '_inventory_b',
        'label'       => __('Inventory B', 'custom-inventory-shipping'),
        'desc_tip'    => true,
        'description' => __('Enter the custom inventory value for Inventory B.', 'custom-inventory-shipping'),
        'type'        => 'number',
    ));
}

// Save custom inventory fields
add_action('woocommerce_process_product_meta', 'save_custom_inventory_fields');
function save_custom_inventory_fields($post_id) {
    $inventory_a = isset($_POST['_inventory_a']) ? sanitize_text_field($_POST['_inventory_a']) : '';
    $inventory_b = isset($_POST['_inventory_b']) ? sanitize_text_field($_POST['_inventory_b']) : '';
    update_post_meta($post_id, '_inventory_a', $inventory_a);
    update_post_meta($post_id, '_inventory_b', $inventory_b);
}

// Register admin menu
add_action('admin_menu', 'custom_inventory_shipping_menu');
function custom_inventory_shipping_menu() {
    add_menu_page(
        'Custom Inventory Shipping Settings',
        'Inventory Shipping',
        'manage_options',
        'custom-inventory-shipping',
        'custom_inventory_shipping_settings_page',
        'dashicons-admin-generic',
        20
    );
}

function custom_inventory_shipping_settings_page() {
    // Check if the user has the required capability
    if (!current_user_can('manage_options')) {
        return;
    }

    // Process form submission
    if (isset($_POST['custom_inventory_shipping_settings_submit'])) {
        // Verify nonce
        check_admin_referer('custom_inventory_shipping_settings');

        // Sanitize and update the inventory instances options
        $inventory_a_instances = isset($_POST['inventory_a_instances']) ? array_map('absint', $_POST['inventory_a_instances']) : [];
        $inventory_b_instances = isset($_POST['inventory_b_instances']) ? array_map('absint', $_POST['inventory_b_instances']) : [];
        update_option('custom_inventory_a_instances', $inventory_a_instances);
        update_option('custom_inventory_b_instances', $inventory_b_instances);

        // Sanitize and update the test input option
        $test_input = isset($_POST['test_input']) ? sanitize_text_field($_POST['test_input']) : '';
        update_option('test_input', $test_input);

        // Display success message
        add_settings_error('custom_inventory_shipping_settings', 'settings_saved', 'Settings saved.', 'success');
    }

    // Retrieve saved options from the database
    $inventory_a_instances = get_option('custom_inventory_a_instances', []);
    $inventory_b_instances = get_option('custom_inventory_b_instances', []);
    $test_input = get_option('test_input', '');

    // Render the settings page
    ?>
    <div class="wrap">
        <h1>Custom Inventory Shipping Settings</h1>
        <?php settings_errors('custom_inventory_shipping_settings'); ?>
        <form method="post" action="">
            <?php wp_nonce_field('custom_inventory_shipping_settings'); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Inventory A Shipping Methods</th>
                    <td>
                        <select multiple="multiple" name="inventory_a_instances[]" style="width: 100%;">
                            <?php echo get_shipping_method_instance_options($inventory_a_instances); ?>
                        </select>
                        <p class="description">Select the shipping methods that should reduce Inventory A.</p>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Inventory B Shipping Methods</th>
                    <td>
                        <select multiple="multiple" name="inventory_b_instances[]" style="width: 100%;">
                            <?php echo get_shipping_method_instance_options($inventory_b_instances); ?>
                        </select>
                        <p class="description">Select the shipping methods that should reduce Inventory B.</p>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Test Input</th>
                    <td>
                        <input type="text" name="test_input" value="<?php echo esc_attr($test_input); ?>">
                        <p class="description">Enter some text to test if it gets saved.</p>
                    </td>
                </tr>
            </table>
            <?php submit_button('Save Settings', 'primary', 'custom_inventory_shipping_settings_submit'); ?>
        </form>
    </div>
    <?php
}


// Helper function to get shipping method instance options
function get_shipping_method_instance_options($selected_instances) {
    $options = '';

    // Fetch all shipping zones and their methods
    $zones = WC_Shipping_Zones::get_zones();
    foreach ($zones as $zone) {
        $zone_shipping_methods = $zone['shipping_methods'];
        foreach ($zone_shipping_methods as $zone_shipping_method) {
            $zone_instance_id = $zone_shipping_method->instance_id;
            $zone_instance_title = $zone_shipping_method->title;
            $selected = in_array($zone_instance_id, $selected_instances) ? 'selected="selected"' : '';
            $options .= sprintf('<option value="%s" %s>%s - %s</option>', $zone_instance_id, $selected, $zone['zone_name'], $zone_instance_title);
        }
    }

    return $options;
}

// Adjust shipping methods based on inventory
add_filter('woocommerce_package_rates', 'adjust_shipping_methods_based_on_inventory', 10, 2);
function adjust_shipping_methods_based_on_inventory($rates, $package) {
    $inventory_a_instances = get_option('custom_inventory_a_instances', []);
    $inventory_b_instances = get_option('custom_inventory_b_instances', []);

    foreach (WC()->cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $quantity = $cart_item['quantity'];

        $inventory_a = get_post_meta($product_id, '_inventory_a', true);
        $inventory_b = get_post_meta($product_id, '_inventory_b', true);

        foreach ($rates as $rate_key => $rate) {
            $instance_id = $rate->get_instance_id();
            if (in_array($instance_id, $inventory_a_instances) && ($inventory_a === '' || $inventory_a < $quantity)) {
                unset($rates[$rate_key]);
                wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error');
            } elseif (in_array($instance_id, $inventory_b_instances) && ($inventory_b === '' || $inventory_b < $quantity)) {
                unset($rates[$rate_key]);
                wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error');
            }
        }
    }
    return $rates;
}

// Validate inventory before checkout
add_action('woocommerce_checkout_process', 'validate_inventory_before_checkout');
function validate_inventory_before_checkout() {
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
    $chosen_shipping_instance_id = isset($chosen_shipping_methods[0]) ? explode(':', $chosen_shipping_methods[0])[1] : '';

    $inventory_a_instances = get_option('custom_inventory_a_instances', []);
    $inventory_b_instances = get_option('custom_inventory_b_instances', []);

    foreach (WC()->cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $quantity = $cart_item['quantity'];

        $inventory_a = get_post_meta($product_id, '_inventory_a', true);
        $inventory_b = get_post_meta($product_id, '_inventory_b', true);

        if (in_array($chosen_shipping_instance_id, $inventory_a_instances) && ($inventory_a === '' || $inventory_a < $quantity)) {
            wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error');
        } elseif (in_array($chosen_shipping_instance_id, $inventory_b_instances) && ($inventory_b === '' || $inventory_b < $quantity)) {
            wc_add_notice(__('Product is out of stock for the selected shipping method.', 'custom-inventory-shipping'), 'error');
        }
    }
}

// Adjust inventory after order is placed
add_action('woocommerce_thankyou', 'adjust_inventory_based_on_zone_shipping_method');
function adjust_inventory_based_on_zone_shipping_method($order_id) {
    if (!$order_id) return;

    $order = wc_get_order($order_id);
    $shipping_methods = $order->get_shipping_methods();
    $shipping_instance_id = '';

    foreach ($shipping_methods as $shipping_method_item) {
        $shipping_instance_id = $shipping_method_item->get_instance_id();
        break;
    }

    $inventory_a_instances = get_option('custom_inventory_a_instances', []);
    $inventory_b_instances = get_option('custom_inventory_b_instances', []);

    foreach ($order->get_items() as $item) {
        $product_id = $item->get_product_id();
        $quantity = $item->get_quantity();

        if (in_array($shipping_instance_id, $inventory_a_instances)) {
            $inventory_a = get_post_meta($product_id, '_inventory_a', true);
            if ($inventory_a !== '') {
                $new_inventory_a = max(0, $inventory_a - $quantity);
                update_post_meta($product_id, '_inventory_a', $new_inventory_a);
            }
        } elseif (in_array($shipping_instance_id, $inventory_b_instances)) {
            $inventory_b = get_post_meta($product_id, '_inventory_b', true);
            if ($inventory_b !== '') {
                $new_inventory_b = max(0, $inventory_b - $quantity);
                update_post_meta($product_id, '_inventory_b', $new_inventory_b);
            }
        }
    }
}

Function Calls

None

Variables

None

Stats

MD5 b64f50f0cfc77d8c3c5ba8e66a7655d7
Eval Count 0
Decode Time 65 ms