Woocommerce code -add extra Input feild for woocoomerce price tab [ each product ]






// INput feild for woocoomerce price tab


add_action( 'woocommerce_product_options_pricing', 'alsawar_add_service_to_products' );       

function alsawar_add_service_to_products() {          
woocommerce_wp_text_input( array(
'id' => 'service',
'class' => 'short wc_input_price',
'label' => __( 'Service Charges', 'woocommerce' ),
'description' => 'Service Chargs ',
'placeholder' => 'Value'
        )
    );     
}

// Saving input feild


add_action( 'save_post', 'alsawar_save_service' );

function alsawar_save_service( $product_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['service'] ) ) {
        if ( is_numeric( $_POST['service'] ) )
            update_post_meta( $product_id, 'service', $_POST['service'] );
    } else delete_post_meta( $product_id, 'service' );
}



// Visible to front end Product page  
add_action( 'woocommerce_single_product_summary', 'alsawar_display_service', 9 );

function alsawar_display_service() {
    global $product;
    
    if ( $product->product_type <> 'variable' && $service_value = get_post_meta( $product->id, 'service', true ) ) {

        echo '<div class="woocommerce_rrp">';
        _e( 'Service Charges: ', 'woocommerce' );
        echo '<span>' . ( $service_value ) . '</span>';
        echo '</div>';
        }
   
}

Comments