Plugin Directory

Changeset 3801527


Ignore:
Timestamp:
11/14/2026 08:15:10 AM (4 days ago)
Author:
rakeshdhiman
Message:

add default product init setting for invoice

Location:
rakeshdhiman-invoice-manager-woocommerce
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-invoice-manager.php

    r3801470 r3801527  
    481481                        )
    482482                );
     483
     484                woocommerce_wp_text_input(
     485                        array(
     486                                'id'          => '_wcdi_qty_unit',
     487                                'label'       => __( 'Quantity unit', 'rakeshdhiman-invoice-manager-woocommerce' ),
     488                                'description' => __( 'Optional. Example: Kg, Box, Hour. Leave empty to use the default from Invoice Settings.', 'rakeshdhiman-invoice-manager-woocommerce' ),
     489                                'desc_tip'    => true,
     490                        )
     491                );
     492
    483493                echo '</div>';
    484494        }
    … …  
    499509                        update_post_meta( $product_id, '_wcdi_hsn_code', sanitize_text_field( wp_unslash( $_POST['_wcdi_hsn_code'] ) ) );
    500510                }
     511       
     512                if ( isset( $_POST['_wcdi_qty_unit'] ) ) {
     513                        update_post_meta( $product_id, '_wcdi_qty_unit', sanitize_text_field( wp_unslash( $_POST['_wcdi_qty_unit'] ) ) );
     514                }
     515
    501516        }
    502517
    … …  
    676691         * @return string
    677692         */
    678         public static function format_quantity_static( $qty, $with_unit = true ) {
    679                 return self::format_quantity( $qty, $with_unit );
     693        public static function format_quantity_static( $qty, $with_unit = true, $unit = '' ) {
     694                return self::format_quantity( $qty, $with_unit, $unit );
    680695        }
    681696
    … …  
    891906                                'sku'      => $product ? $product->get_sku() : '',
    892907                                'hsn'      => self::get_hsn_code( $product ),
    893                                 'quantity' => self::format_quantity( $qty ),
     908                                'quantity' => self::format_quantity( $qty, true, self::get_quantity_unit( $product ) ),
    894909
    895910                                /*
    … …  
    15381553        }
    15391554
    1540 protected static function format_quantity( $qty, $with_unit = true ) {
    1541         $formatted = fmod( $qty, 1 ) === 0.0 ? number_format_i18n( $qty, 0 ) : wc_format_decimal( $qty, 2 );
    1542 
    1543         if ( $with_unit ) {
    1544                 /* translators: %s: formatted quantity number */
    1545                 return sprintf( __( '%s Pcs', 'rakeshdhiman-invoice-manager-woocommerce' ), $formatted );
    1546         }
    1547 
    1548         return $formatted;
     1555public static function get_quantity_unit( $product ) {
     1556        $settings = class_exists( 'WCDI_Settings' ) ? WCDI_Settings::get_settings() : array();
     1557        $default  = trim( (string) ( $settings['quantity_unit'] ?? 'Pcs' ) );
     1558
     1559        if ( '' === $default ) {
     1560                $default = 'Pcs';
     1561        }
     1562
     1563        if ( ! $product instanceof WC_Product ) {
     1564                return $default;
     1565        }
     1566
     1567        $unit = trim( (string) $product->get_meta( '_wcdi_qty_unit', true ) );
     1568
     1569        if ( '' === $unit && $product->is_type( 'variation' ) ) {
     1570                $parent = wc_get_product( $product->get_parent_id() );
     1571                if ( $parent instanceof WC_Product ) {
     1572                        $unit = trim( (string) $parent->get_meta( '_wcdi_qty_unit', true ) );
     1573                }
     1574        }
     1575
     1576        return '' !== $unit ? $unit : $default;
     1577}
     1578
     1579protected static function format_quantity( $qty, $with_unit = true, $unit = '' ) {
     1580        $formatted = fmod( (float) $qty, 1 ) === 0.0 ? number_format_i18n( $qty, 0 ) : wc_format_decimal( $qty, 2 );
     1581
     1582        if ( ! $with_unit ) {
     1583                return $formatted;
     1584        }
     1585
     1586        $unit = trim( (string) $unit );
     1587
     1588        if ( '' === $unit ) {
     1589                return $formatted;
     1590        }
     1591
     1592        return sprintf(
     1593                /* translators: 1: formatted quantity number, 2: unit label such as Pcs or Kg. */
     1594                __( '%1$s %2$s', 'rakeshdhiman-invoice-manager-woocommerce' ),
     1595                $formatted,
     1596                $unit
     1597        );
    15491598}
    15501599
  • rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-packing-slip.php

    r3713235 r3801527  
    295295                                'name'     => $item->get_name(),
    296296                                'sku'      => $sku,
    297                                 'quantity' => WCDI_Invoice_Manager::format_quantity_static( $qty ),
     297                                'quantity' => WCDI_Invoice_Manager::format_quantity_static(
     298                                $qty,
     299                                true,
     300                                WCDI_Invoice_Manager::get_quantity_unit( $product )
     301                        ),
    298302                        );
    299303                }
  • rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-settings.php

    r3801470 r3801527  
    111111                        'show_hsn_column'           => 'yes',
    112112                        'hsn_label'                 => 'HSN',
     113                        'quantity_unit'             => 'Pcs',
    113114                        'logo_url'                  => '',
    114115                        'logo_id'                   => 0,
    … …  
    197198                $output['hsn_label']           = sanitize_text_field( $input['hsn_label'] ?? $current['hsn_label'] );
    198199                $output['hsn_label']           = '' !== $output['hsn_label'] ? $output['hsn_label'] : 'HSN';
     200                $output['quantity_unit'] = sanitize_text_field( $input['quantity_unit'] ?? $current['quantity_unit'] ?? 'Pcs' );
     201        $output['quantity_unit'] = '' !== trim( $output['quantity_unit'] ) ? $output['quantity_unit'] : 'Pcs';
    199202                $output['logo_url']            = esc_url_raw( $input['logo_url'] ?? $current['logo_url'] );
    200203                $output['logo_id']             = absint( $input['logo_id'] ?? $current['logo_id'] ?? 0 );
    … …  
    676679                                                                                </td>
    677680                                                                        </tr>
     681                                                                        <tr>
     682                                                                        <th scope="row"><label for="wcdi_quantity_unit"><?php esc_html_e( 'Quantity unit', 'rakeshdhiman-invoice-manager-woocommerce' ); ?></label></th>
     683                                                                        <td>
     684                                                                                <input name="<?php echo esc_attr( self::OPTION_KEY ); ?>[quantity_unit]" id="wcdi_quantity_unit" type="text" class="regular-text wcdi-preview-input" value="<?php echo esc_attr( $settings['quantity_unit'] ?? 'Pcs' ); ?>">
     685                                                                                <p class="description"><?php esc_html_e( 'Default unit on the invoice, for example Pcs, Kg, or Box. A product can override this.', 'rakeshdhiman-invoice-manager-woocommerce' ); ?></p>
     686                                                                        </td>
     687                                                                </tr>
    678688                                                                </tbody>
    679689                                                        </table>
  • rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-invoice-manager.php

    r3801470 r3801527  
    481481                        )
    482482                );
     483
     484                woocommerce_wp_text_input(
     485                        array(
     486                                'id'          => '_wcdi_qty_unit',
     487                                'label'       => __( 'Quantity unit', 'rakeshdhiman-invoice-manager-woocommerce' ),
     488                                'description' => __( 'Optional. Example: Kg, Box, Hour. Leave empty to use the default from Invoice Settings.', 'rakeshdhiman-invoice-manager-woocommerce' ),
     489                                'desc_tip'    => true,
     490                        )
     491                );
     492
    483493                echo '</div>';
    484494        }
    … …  
    499509                        update_post_meta( $product_id, '_wcdi_hsn_code', sanitize_text_field( wp_unslash( $_POST['_wcdi_hsn_code'] ) ) );
    500510                }
     511       
     512                if ( isset( $_POST['_wcdi_qty_unit'] ) ) {
     513                        update_post_meta( $product_id, '_wcdi_qty_unit', sanitize_text_field( wp_unslash( $_POST['_wcdi_qty_unit'] ) ) );
     514                }
     515
    501516        }
    502517
    … …  
    676691         * @return string
    677692         */
    678         public static function format_quantity_static( $qty, $with_unit = true ) {
    679                 return self::format_quantity( $qty, $with_unit );
     693        public static function format_quantity_static( $qty, $with_unit = true, $unit = '' ) {
     694                return self::format_quantity( $qty, $with_unit, $unit );
    680695        }
    681696
    … …  
    891906                                'sku'      => $product ? $product->get_sku() : '',
    892907                                'hsn'      => self::get_hsn_code( $product ),
    893                                 'quantity' => self::format_quantity( $qty ),
     908                                'quantity' => self::format_quantity( $qty, true, self::get_quantity_unit( $product ) ),
    894909
    895910                                /*
    … …  
    15381553        }
    15391554
    1540 protected static function format_quantity( $qty, $with_unit = true ) {
    1541         $formatted = fmod( $qty, 1 ) === 0.0 ? number_format_i18n( $qty, 0 ) : wc_format_decimal( $qty, 2 );
    1542 
    1543         if ( $with_unit ) {
    1544                 /* translators: %s: formatted quantity number */
    1545                 return sprintf( __( '%s Pcs', 'rakeshdhiman-invoice-manager-woocommerce' ), $formatted );
    1546         }
    1547 
    1548         return $formatted;
     1555public static function get_quantity_unit( $product ) {
     1556        $settings = class_exists( 'WCDI_Settings' ) ? WCDI_Settings::get_settings() : array();
     1557        $default  = trim( (string) ( $settings['quantity_unit'] ?? 'Pcs' ) );
     1558
     1559        if ( '' === $default ) {
     1560                $default = 'Pcs';
     1561        }
     1562
     1563        if ( ! $product instanceof WC_Product ) {
     1564                return $default;
     1565        }
     1566
     1567        $unit = trim( (string) $product->get_meta( '_wcdi_qty_unit', true ) );
     1568
     1569        if ( '' === $unit && $product->is_type( 'variation' ) ) {
     1570                $parent = wc_get_product( $product->get_parent_id() );
     1571                if ( $parent instanceof WC_Product ) {
     1572                        $unit = trim( (string) $parent->get_meta( '_wcdi_qty_unit', true ) );
     1573                }
     1574        }
     1575
     1576        return '' !== $unit ? $unit : $default;
     1577}
     1578
     1579protected static function format_quantity( $qty, $with_unit = true, $unit = '' ) {
     1580        $formatted = fmod( (float) $qty, 1 ) === 0.0 ? number_format_i18n( $qty, 0 ) : wc_format_decimal( $qty, 2 );
     1581
     1582        if ( ! $with_unit ) {
     1583                return $formatted;
     1584        }
     1585
     1586        $unit = trim( (string) $unit );
     1587
     1588        if ( '' === $unit ) {
     1589                return $formatted;
     1590        }
     1591
     1592        return sprintf(
     1593                /* translators: 1: formatted quantity number, 2: unit label such as Pcs or Kg. */
     1594                __( '%1$s %2$s', 'rakeshdhiman-invoice-manager-woocommerce' ),
     1595                $formatted,
     1596                $unit
     1597        );
    15491598}
    15501599
  • rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-packing-slip.php

    r3713235 r3801527  
    295295                                'name'     => $item->get_name(),
    296296                                'sku'      => $sku,
    297                                 'quantity' => WCDI_Invoice_Manager::format_quantity_static( $qty ),
     297                                'quantity' => WCDI_Invoice_Manager::format_quantity_static(
     298                                $qty,
     299                                true,
     300                                WCDI_Invoice_Manager::get_quantity_unit( $product )
     301                        ),
    298302                        );
    299303                }
  • rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-settings.php

    r3801470 r3801527  
    111111                        'show_hsn_column'           => 'yes',
    112112                        'hsn_label'                 => 'HSN',
     113                        'quantity_unit'             => 'Pcs',
    113114                        'logo_url'                  => '',
    114115                        'logo_id'                   => 0,
    … …  
    197198                $output['hsn_label']           = sanitize_text_field( $input['hsn_label'] ?? $current['hsn_label'] );
    198199                $output['hsn_label']           = '' !== $output['hsn_label'] ? $output['hsn_label'] : 'HSN';
     200                $output['quantity_unit'] = sanitize_text_field( $input['quantity_unit'] ?? $current['quantity_unit'] ?? 'Pcs' );
     201        $output['quantity_unit'] = '' !== trim( $output['quantity_unit'] ) ? $output['quantity_unit'] : 'Pcs';
    199202                $output['logo_url']            = esc_url_raw( $input['logo_url'] ?? $current['logo_url'] );
    200203                $output['logo_id']             = absint( $input['logo_id'] ?? $current['logo_id'] ?? 0 );
    … …  
    676679                                                                                </td>
    677680                                                                        </tr>
     681                                                                        <tr>
     682                                                                        <th scope="row"><label for="wcdi_quantity_unit"><?php esc_html_e( 'Quantity unit', 'rakeshdhiman-invoice-manager-woocommerce' ); ?></label></th>
     683                                                                        <td>
     684                                                                                <input name="<?php echo esc_attr( self::OPTION_KEY ); ?>[quantity_unit]" id="wcdi_quantity_unit" type="text" class="regular-text wcdi-preview-input" value="<?php echo esc_attr( $settings['quantity_unit'] ?? 'Pcs' ); ?>">
     685                                                                                <p class="description"><?php esc_html_e( 'Default unit on the invoice, for example Pcs, Kg, or Box. A product can override this.', 'rakeshdhiman-invoice-manager-woocommerce' ); ?></p>
     686                                                                        </td>
     687                                                                </tr>
    678688                                                                </tbody>
    679689                                                        </table>
Note: See TracChangeset for help on using the changeset viewer.