Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter cfmanjeet

    (@cfmanjeet)

    I have used the main code but whenever I try to navigate checkout page, the checkout page automatic converts it decimal to integer If there are any changes in hooks , please confirm so that i can achieve decimal functionality and if possible please share document related hook.:




    <?php

    // Enable decimal quantities (in frontend and backend)

    remove_filter('woocommerce_stock_amount', 'intval');

    add_filter('woocommerce_stock_amount', 'floatval');

    add_filter('woocommerce_quantity_input_args', 'customize_simple_product_quantity_input_args', 10, 2);

    function customize_simple_product_quantity_input_args($args, $product)

    {

    if ($product->is_type('simple')) { // Apply only to simple products

    $args['min_value'] = 1; // Minimum value

    $args['step'] = 0.5; // Step size

    $args['input_value'] = 1; // Default value

    }

    return $args;

    }

    add_action('woocommerce_before_quantity_input_field', 'add_weight_label_for_simple_product_before');

    function add_weight_label_for_simple_product_before()

    {

    if (is_product()) {

    global $product;

    if ($product->is_type('simple')) {

    echo '<span class="weight-label">Weight : </span>';

    }

    }

    }

    add_action('woocommerce_after_quantity_input_field', 'add_weight_label_for_simple_product');

    function add_weight_label_for_simple_product()

    {

    if (is_product()) {

    global $product;

    if ($product->is_type('simple')) {

    echo '<span class="weight-label"> Kg</span>';

    }

    }

    }

    add_filter('woocommerce_get_item_data', 'add_weight_to_cart_item_data_for_simple_product', 10, 2);

    function add_weight_to_cart_item_data_for_simple_product($item_data, $cart_item)

    {

    $product = $cart_item['data'];

    if ($product->is_type('simple')) {

    $weight = $cart_item['quantity']; // Quantity as weight

    $item_data[] = array(

    'name' => __('Weight', 'woocommerce'),

    'value' => $weight . ' Kg',

    );

    }

    return $item_data;

    }

    add_filter('woocommerce_checkout_create_order_line_item', 'save_weight_to_order_meta', 10, 2);

    function save_weight_to_order_meta($item, $cart_item_key)

    {

    $weight = WC()->cart->get_cart_item($cart_item_key)['quantity']; // Get quantity as weight

    $item->add_meta_data('Weight', $weight . ' Kg', true); // Save weight to order meta

    }

    add_filter('woocommerce_order_item_display_meta_key', 'update_order_item_display_meta_for_simple_product', 10, 2);

    function update_order_item_display_meta_for_simple_product($display_key, $meta)

    {

    if ($meta->key === 'Weight') {

    $display_key = __('Weight (Kg)', 'woocommerce');

    }

    return $display_key;

    }

    add_filter('woocommerce_email_order_items_args', 'customize_email_order_items_args_for_simple_product');

    function customize_email_order_items_args_for_simple_product($args)

    {

    $args['show_meta'] = true; // Show metadata like weight

    return $args;

    }

    add_action('woocommerce_admin_order_item_headers', 'add_admin_order_weight_column_for_simple_product');

    add_action('woocommerce_admin_order_item_values', 'display_admin_order_weight_column_for_simple_product', 10, 3);

    function add_admin_order_weight_column_for_simple_product()

    {

    echo '<th class="weight-column">' . __('Weight (Kg)', 'woocommerce') . '</th>';

    }

    function display_admin_order_weight_column_for_simple_product($product, $item, $item_id)

    {

    $order = $item->get_order();

    if ($product->is_type('simple')) {

    echo '<td>' . esc_html($item->get_quantity()) . ' Kg</td>';

    }

    }



Viewing 1 replies (of 1 total)