Changeset 3801527
- Timestamp:
- 11/14/2026 08:15:10 AM (4 days ago)
- Location:
- rakeshdhiman-invoice-manager-woocommerce
- Files:
-
- 6 edited
-
tags/2.1.2/includes/class-wcdi-invoice-manager.php (modified) (5 diffs)
-
tags/2.1.2/includes/class-wcdi-packing-slip.php (modified) (1 diff)
-
tags/2.1.2/includes/class-wcdi-settings.php (modified) (3 diffs)
-
trunk/includes/class-wcdi-invoice-manager.php (modified) (5 diffs)
-
trunk/includes/class-wcdi-packing-slip.php (modified) (1 diff)
-
trunk/includes/class-wcdi-settings.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-invoice-manager.php
r3801470 r3801527 481 481 ) 482 482 ); 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 483 493 echo '</div>'; 484 494 } … … 499 509 update_post_meta( $product_id, '_wcdi_hsn_code', sanitize_text_field( wp_unslash( $_POST['_wcdi_hsn_code'] ) ) ); 500 510 } 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 501 516 } 502 517 … … 676 691 * @return string 677 692 */ 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 ); 680 695 } 681 696 … … 891 906 'sku' => $product ? $product->get_sku() : '', 892 907 'hsn' => self::get_hsn_code( $product ), 893 'quantity' => self::format_quantity( $qty ),908 'quantity' => self::format_quantity( $qty, true, self::get_quantity_unit( $product ) ), 894 909 895 910 /* … … 1538 1553 } 1539 1554 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; 1555 public 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 1579 protected 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 ); 1549 1598 } 1550 1599 -
rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-packing-slip.php
r3713235 r3801527 295 295 'name' => $item->get_name(), 296 296 '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 ), 298 302 ); 299 303 } -
rakeshdhiman-invoice-manager-woocommerce/tags/2.1.2/includes/class-wcdi-settings.php
r3801470 r3801527 111 111 'show_hsn_column' => 'yes', 112 112 'hsn_label' => 'HSN', 113 'quantity_unit' => 'Pcs', 113 114 'logo_url' => '', 114 115 'logo_id' => 0, … … 197 198 $output['hsn_label'] = sanitize_text_field( $input['hsn_label'] ?? $current['hsn_label'] ); 198 199 $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'; 199 202 $output['logo_url'] = esc_url_raw( $input['logo_url'] ?? $current['logo_url'] ); 200 203 $output['logo_id'] = absint( $input['logo_id'] ?? $current['logo_id'] ?? 0 ); … … 676 679 </td> 677 680 </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> 678 688 </tbody> 679 689 </table> -
rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-invoice-manager.php
r3801470 r3801527 481 481 ) 482 482 ); 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 483 493 echo '</div>'; 484 494 } … … 499 509 update_post_meta( $product_id, '_wcdi_hsn_code', sanitize_text_field( wp_unslash( $_POST['_wcdi_hsn_code'] ) ) ); 500 510 } 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 501 516 } 502 517 … … 676 691 * @return string 677 692 */ 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 ); 680 695 } 681 696 … … 891 906 'sku' => $product ? $product->get_sku() : '', 892 907 'hsn' => self::get_hsn_code( $product ), 893 'quantity' => self::format_quantity( $qty ),908 'quantity' => self::format_quantity( $qty, true, self::get_quantity_unit( $product ) ), 894 909 895 910 /* … … 1538 1553 } 1539 1554 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; 1555 public 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 1579 protected 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 ); 1549 1598 } 1550 1599 -
rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-packing-slip.php
r3713235 r3801527 295 295 'name' => $item->get_name(), 296 296 '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 ), 298 302 ); 299 303 } -
rakeshdhiman-invoice-manager-woocommerce/trunk/includes/class-wcdi-settings.php
r3801470 r3801527 111 111 'show_hsn_column' => 'yes', 112 112 'hsn_label' => 'HSN', 113 'quantity_unit' => 'Pcs', 113 114 'logo_url' => '', 114 115 'logo_id' => 0, … … 197 198 $output['hsn_label'] = sanitize_text_field( $input['hsn_label'] ?? $current['hsn_label'] ); 198 199 $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'; 199 202 $output['logo_url'] = esc_url_raw( $input['logo_url'] ?? $current['logo_url'] ); 200 203 $output['logo_id'] = absint( $input['logo_id'] ?? $current['logo_id'] ?? 0 ); … … 676 679 </td> 677 680 </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> 678 688 </tbody> 679 689 </table>
Note:
See TracChangeset
for help on using the changeset viewer.