$first_label ); foreach ( EBGI_GSTIN_Validator::GST_SLABS as $slab ) { /* translators: %s: GST rate */ $options[ $slab ] = sprintf( __( 'GST %s%%', '8-bit-gst-invoice-for-woocommerce' ), $slab ); } return $options; } /** * Fields on the General tab (simple & parent of variable products). */ public static function render_simple_fields() { echo '
'; } /** * Validate + save simple product fields after a nonce and capability check. * * @param WC_Product $product Product. */ public static function save_simple_fields( $product ) { $nonce = isset( $_POST['woocommerce_meta_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_meta_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'woocommerce_save_data' ) ) { return; } if ( ! self::can_edit( $product->get_id() ) ) { return; } $hsn = isset( $_POST[ self::META_HSN ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::META_HSN ] ) ) : null; $slab = isset( $_POST[ self::META_SLAB ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::META_SLAB ] ) ) : null; self::apply_to_product( $product, $hsn, $slab, true ); } /** * Can the current user edit this product? * * @param int $product_id Product (or parent product) ID; 0 for a product that has no ID yet. * @return bool */ private static function can_edit( $product_id ) { return $product_id ? current_user_can( 'edit_product', $product_id ) : current_user_can( 'edit_products' ); } /** * Variation fields. * * @param int $loop Index. * @param array $variation_data Data. * @param WP_Post $variation Variation post. */ public static function render_variation_fields( $loop, $variation_data, $variation ) { $product = wc_get_product( $variation->ID ); if ( ! $product ) { return; } woocommerce_wp_text_input( array( 'id' => self::META_HSN . '_' . $loop, 'name' => self::FIELD_VARIATION_HSN . '[' . $loop . ']', 'value' => $product->get_meta( self::META_HSN ), 'label' => __( 'HSN / SAC code', '8-bit-gst-invoice-for-woocommerce' ), 'placeholder' => __( 'Same as parent', '8-bit-gst-invoice-for-woocommerce' ), 'wrapper_class' => 'form-row form-row-first', ) ); woocommerce_wp_select( array( 'id' => self::META_SLAB . '_' . $loop, 'name' => self::FIELD_VARIATION_SLAB . '[' . $loop . ']', 'value' => $product->get_meta( self::META_SLAB ), 'label' => __( 'GST rate', '8-bit-gst-invoice-for-woocommerce' ), 'options' => self::slab_options( __( 'Same as parent', '8-bit-gst-invoice-for-woocommerce' ) ), 'wrapper_class' => 'form-row form-row-last', ) ); } /** * Save variation fields after a nonce and capability check (WooCommerce saves the variation right after this hook). * * WooCommerce saves variations through its AJAX handler (security / save-variations) or the * product form (woocommerce_meta_nonce / woocommerce_save_data). The request type picks the nonce, never the input. * * @param WC_Product_Variation $variation Variation. * @param int $i Index. */ public static function save_variation_fields( $variation, $i ) { $nonce_field = wp_doing_ajax() ? 'security' : 'woocommerce_meta_nonce'; $nonce_action = wp_doing_ajax() ? 'save-variations' : 'woocommerce_save_data'; $nonce = isset( $_POST[ $nonce_field ] ) ? sanitize_text_field( wp_unslash( $_POST[ $nonce_field ] ) ) : ''; if ( ! wp_verify_nonce( $nonce, $nonce_action ) ) { return; } if ( ! self::can_edit( $variation->get_parent_id() ) ) { return; } $i = absint( $i ); $hsn = isset( $_POST[ self::FIELD_VARIATION_HSN ][ $i ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::FIELD_VARIATION_HSN ][ $i ] ) ) : null; $slab = isset( $_POST[ self::FIELD_VARIATION_SLAB ][ $i ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::FIELD_VARIATION_SLAB ][ $i ] ) ) : null; self::apply_to_product( $variation, $hsn, $slab, false ); } /** * Set HSN / slab on a product object (does not save). Null leaves a field untouched. * * @param WC_Product $product Product. * @param string|null $hsn HSN ('' clears). * @param string|null $slab Slab ('' clears). * @param bool $show_error Show admin error for an invalid HSN. * @return bool False when the HSN was invalid and ignored. */ public static function apply_to_product( $product, $hsn, $slab, $show_error = false ) { $ok = true; if ( null !== $hsn ) { $hsn = preg_replace( '/\s+/', '', (string) $hsn ); if ( '' === $hsn ) { $product->delete_meta_data( self::META_HSN ); } elseif ( EBGI_GSTIN_Validator::is_valid_hsn( $hsn ) ) { $product->update_meta_data( self::META_HSN, $hsn ); } else { $ok = false; if ( $show_error && class_exists( 'WC_Admin_Meta_Boxes' ) ) { /* translators: %s: entered HSN code */ WC_Admin_Meta_Boxes::add_error( sprintf( __( 'HSN / SAC code "%s" was not saved: it must be 4, 6 or 8 digits.', '8-bit-gst-invoice-for-woocommerce' ), esc_html( $hsn ) ) ); } } } if ( null !== $slab ) { if ( '' === $slab ) { $product->delete_meta_data( self::META_SLAB ); } elseif ( EBGI_GSTIN_Validator::is_valid_slab( $slab ) ) { $key = EBGI_GSTIN_Validator::slab_key( $slab ); $product->update_meta_data( self::META_SLAB, $key ); if ( 'yes' === EBGI_Settings::get( 'sync_tax_class' ) && EBGI_Tax_Rates::class_exists_for_slab( $key ) ) { $product->set_tax_class( EBGI_Tax_Rates::class_slug( $key ) ); } } } return $ok; } /** * Effective HSN for a product (variation falls back to its parent). * * @param WC_Product|false $product Product. * @return string */ public static function get_hsn( $product ) { if ( ! $product ) { return ''; } $hsn = (string) $product->get_meta( self::META_HSN ); if ( '' === $hsn && $product->get_parent_id() ) { $parent = wc_get_product( $product->get_parent_id() ); $hsn = $parent ? (string) $parent->get_meta( self::META_HSN ) : ''; } return $hsn; } /** * Effective GST slab for a product ('' = not set, use store default). * * @param WC_Product|false $product Product. * @return string */ public static function get_slab( $product ) { if ( ! $product ) { return ''; } $slab = (string) $product->get_meta( self::META_SLAB ); if ( '' === $slab && $product->get_parent_id() ) { $parent = wc_get_product( $product->get_parent_id() ); $slab = $parent ? (string) $parent->get_meta( self::META_SLAB ) : ''; } return $slab; } /** * Freeze HSN + slab onto the order line item (fires for Classic and Block / Store API checkout). * * @param WC_Order_Item_Product $item Item. * @param string $cart_item_key Cart key. * @param array $values Cart item. * @param WC_Order $order Order. */ public static function snapshot_order_item( $item, $cart_item_key, $values, $order ) { $product = isset( $values['data'] ) ? $values['data'] : $item->get_product(); $hsn = self::get_hsn( $product ); $slab = self::get_slab( $product ); if ( '' !== $hsn ) { $item->update_meta_data( self::ITEM_HSN, $hsn ); } if ( '' !== $slab ) { $item->update_meta_data( self::ITEM_SLAB, $slab ); } } /** * HSN / slab stored on an order item, falling back to the product's current values. * * @param WC_Order_Item_Product $item Item. * @return array array( 'hsn' => string, 'slab' => string ). */ public static function get_item_tax_meta( $item ) { $hsn = (string) $item->get_meta( self::ITEM_HSN ); $slab = (string) $item->get_meta( self::ITEM_SLAB ); if ( '' === $hsn || '' === $slab ) { $product = $item->get_product(); if ( '' === $hsn ) { $hsn = self::get_hsn( $product ); } if ( '' === $slab ) { $slab = self::get_slab( $product ); } } return array( 'hsn' => $hsn, 'slab' => $slab, ); } /** * Show HSN under each line item on the admin order screen. * * @param int $item_id Item ID. * @param WC_Order_Item $item Item. * @param WC_Product $product Product. */ public static function show_item_hsn( $item_id, $item, $product ) { if ( ! $item instanceof WC_Order_Item_Product ) { return; } $meta = self::get_item_tax_meta( $item ); if ( '' === $meta['hsn'] && '' === $meta['slab'] ) { return; } echo '