Home Forums WC Vendors Pro Support CUSTOMS FIELDS : PRODUCT

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #41398
    Marc Bovet
    Participant

    An example on how to add custom product fields to WooCommerce and WC Vendors.

    #41399
    Marc Bovet
    Participant

    <?php

    /*************************************************************************************************
    * WC WENDORS-PRO + WooCommerce
    *
    * Adding custom fields to the backend (WooCommerce admin) + frontentd (WC Vendors dashboard) + Woo Commerce product page
    *
    * All custom fields unique ID have to start with “wcv_custom_product_”

    * Poids = Weight : wcv_custom_product_poids
    * Largeur = Widht : wcv_custom_product_largeur
    * Hauteur = Height : wcv_custom_product_hauteur
    * Epaisseur = thickness : wcv_custom_product_epaisseur
    *
    *************************************************************************************************/

    /*************************************************************************************************
    * STEP 1
    * edit product-edit.php template
    * Copy the template /plugins/wc-vendors-pro/templates/dashboard/product-edit.php to /themes/your-theme/wc-vendors/dashboard/product-edit.php
    *
    * see code snippet below
    *************************************************************************************************/

    
    <!-- Product Edit Form -->
    <form method="post" action="" id="wcv-product-edit" class="wcv-form wcv-formvalidator"> 
    
    	<!-- Basic Product Details -->
    	<div class="wcv-product-basic wcv-product"> 
    		<!-- Product Title -->
    		<?php WCVendors_Pro_Product_Form::title( $object_id, $product_title ); ?>
    		<!-- Product Description -->
    		<?php WCVendors_Pro_Product_Form::description( $object_id, $product_description );  ?>
    		<!-- Product Short Description -->
    		<?php WCVendors_Pro_Product_Form::short_description( $object_id, $product_short_description );  ?>
    		<!-- Product Categories -->
    	    <?php WCVendors_Pro_Product_Form::categories( $object_id, true ); ?>
    	    <!-- Product Tags -->
    	    <?php WCVendors_Pro_Product_Form::tags( $object_id, true ); ?>
    
    		<!--CUSTOM FIELDS : POIDS  + HAUTEUR + LARGEUR + EPAISSEUR -->
    		<?php   //POIDS
    				WCVendors_Pro_Form_Helper::input( array(  
    				'type'      => 'text',
    				'post_id'   => $object_id, 
    				'id'     => 'wcv_custom_product_poids', 
    				'label'    => __( 'Poids', 'wcvendors-pro' ), 
    				'placeholder'   => __( 'Poids', 'wcvendors-pro' ), 
    				'desc_tip'    => 'true', 
    				'description'   => __( 'Poids de l\'oeuvre en kg', 'wcvendors-pro' ), 
    				) );
    
    				//HAUTEUR		
    				WCVendors_Pro_Form_Helper::input( array(  
    				'type'      => 'text',
    				 'post_id'   => $object_id, 
    				 'id'     => 'wcv_custom_product_hauteur', 
    				 'label'    => __( 'Hauteur', 'wcvendors-pro' ), 
    				 'placeholder'   => __( 'Hauteur de l\'oeuvre en cm', 'wcvendors-pro' ), 
    				 'desc_tip'    => 'true', 
    				 'description'   => __( 'Hauteur de l\'oeuvre en cm ', 'wcvendors-pro' ), 
    				 ) );
    			
    				//LARGEUR
    				WCVendors_Pro_Form_Helper::input( array(  
    				'type'      => 'text',
    				 'post_id'   => $object_id, 
    				 'id'     => 'wcv_custom_product_largeur', 
    				 'label'    => __( 'Largeur', 'wcvendors-pro' ), 
    				 'placeholder'   => __( 'Largeur de l\'oeuvre en cm', 'wcvendors-pro' ), 
    				 'desc_tip'    => 'true', 
    				 'description'   => __( 'Largeur de l\'oeuvre en cm ', 'wcvendors-pro' ), 
    				 ) );
    				 
    				//THICKNESS 
    				WCVendors_Pro_Form_Helper::input( array(  
    				'type'      => 'text',
    				 'post_id'   => $object_id, 
    				 'id'     => 'wcv_custom_product_epaisseur', 
    				 'label'    => __( 'Epaisseur', 'wcvendors-pro' ), 
    				 'placeholder'   => __( 'Epaisseur de l\'oeuvre en cm', 'wcvendors-pro' ), 
    				 'desc_tip'    => 'true', 
    				 'description'   => __( 'Epaisseur de l\'oeuvre en cm ', 'wcvendors-pro' ), 
    				) );
    		?>		
    		
    	</div>
    
    #41400
    Marc Bovet
    Participant

    *****************************************************
    * STEP 2
    * ADDING OUR CUSTOMS FIELDS TO THE BACKEND
    * Copy this code to your functions.php file
    *************************************************************************************************/

    
    function mysocialart_custom_fields_admin() {
    
     echo '<h4>MY CUSTOM FIELDS</h4>'; // CHANGE TEXT HERE.
    
     // FIELD Poids
     woocommerce_wp_text_input(
      array(
       'id' => 'wcv_custom_product_poids', 
       'data_type' => 'text', 
       'label' => __('Poids en kg', 'msk'),
       'placeholder' => __('00.00', 'msk'),
       'description' => __('La description peut apparaître dans une infobulle si "desc_tip" est sur "true".', 'msk'),
       'desc_tip' => true // Si "true", la description s'affichera en infobulle
      )
     );
    
     // FIELD Hauteur
     woocommerce_wp_text_input(
      array(
       'id' => 'wcv_custom_product_hauteur', 
       'data_type' => 'text', 
       'label' => __('Hauteur en cm', 'msk'),
       'placeholder' => __('00.00', 'msk'),
       'description' => __('La description peut apparaître dans une infobulle si "desc_tip" est sur "true".', 'msk'),
       'desc_tip' => true // Si "true", la description s'affichera en infobulle
      )
     );
     
      // FIELD Largeur
     woocommerce_wp_text_input(
      array(
       'id' => 'wcv_custom_product_largeur', 
       'data_type' => 'text', 
       'label' => __('Largeur en cm', 'msk'),
       'placeholder' => __('00.00', 'msk'),
       'description' => __('La description peut apparaître dans une infobulle si "desc_tip" est sur "true".', 'msk'),
       'desc_tip' => true // Si "true", la description s'affichera en infobulle
      )
     );
     
      // FIELD epaisseur
     woocommerce_wp_text_input(
      array(
       'id' => 'wcv_custom_product_epaisseur', 
       'data_type' => 'text', 
       'label' => __('epaisseur en cm', 'msk'),
       'placeholder' => __('00.00', 'msk'),
       'description' => __('La description peut apparaître dans une infobulle si "desc_tip" est sur "true".', 'msk'),
       'desc_tip' => true // Si "true", la description s'affichera en infobulle
      )
     );
     
    }
    // CUSTOM FIELDS WILL BE ADDED TO THE GENERAL TAB OF THE PRODUCT
    add_action('woocommerce_product_options_general_product_data', 'mysocialart_custom_fields_admin');
    
    #41401
    Marc Bovet
    Participant

    /*************************************************************************************************
    * STEP 3
    * SAVE OUR CUSTOMS FIELDS WHEN ADD OR EDIT A PRODUCT TO THE BACKEND
    * ADD THIS CODE TO YOUR FUNCTIONS.PHP FILE
    *************************************************************************************************/

    
    function save_mysocialart_custom_fields_admin($product_id, $post, $update) {
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    
     if ($post->post_type == 'product') {
    	 
    	// save poids
      if (isset($_POST['wcv_custom_product_poids'])) {
       $wcv_custom_product_poids = $_POST['wcv_custom_product_poids'];
       update_post_meta($product_id, 'wcv_custom_product_poids', $wcv_custom_product_poids);
      }
    	// save hauteur
      if (isset($_POST['wcv_custom_product_hauteur'])) {
       $wcv_custom_product_hauteur = $_POST['wcv_custom_product_hauteur'];
       update_post_meta($product_id, 'wcv_custom_product_hauteur', $wcv_custom_product_hauteur);
      }
      
      //save largeur
      if (isset($_POST['wcv_custom_product_largeur'])) {
       $wcv_custom_product_largeur = $_POST['wcv_custom_product_largeur'];
       update_post_meta($product_id, 'wcv_custom_product_largeur', $wcv_custom_product_largeur);
      }
      
      //save épaisseur
      if (isset($_POST['wcv_custom_product_epaisseur'])) {
       $wcv_custom_product_epaisseur = $_POST['wcv_custom_product_epaisseur'];
       update_post_meta($product_id, 'wcv_custom_product_epaisseur', $wcv_custom_product_epaisseur);
      }  
      
     }
    }
    add_action('save_post', 'save_mysocialart_custom_fields_admin', 10, 3);
    
    #41402
    Marc Bovet
    Participant

    /*************************************************************************************************
    * STEP 4
    * CREATE A NEW TAB TO DISPLAY OUR CUSTOM FIELDS
    * ADD THIS CODE TO YOUR FUNCTIONS.PHP FILE
    *************************************************************************************************/

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
     
     // Adds the new tab
     
     $tabs['test_tab'] = array(
      'title'  => __( 'Information complémentaire', 'woocommerce' ), // THE NAME OF YOUR TAB.
      'priority'  => 50,
      'callback'  => 'wcv_mysocialart_custom_fields_product_page' // => SEE STEP 5
     );
    
     return $tabs;
    
    }
    

    /*************************************************************************************************
    * STEP 5
    * DISPLAY OUR CUSTOM FIELDS ON THE PRODUCT PAGE IN OUR CUSTOM TAB
    * ADD THIS CODE TO YOUR FUNCTIONS.PHP FILE
    *************************************************************************************************/

    function wcv_mysocialart_custom_fields_product_page() {
    	
        $poids = get_post_meta( get_the_ID(), 'wcv_custom_product_poids', true ); 
        echo 'Poids: ' . $poids . ' kg<br>';
    	
        $hauteur= get_post_meta( get_the_ID(), 'wcv_custom_product_hauteur', true ); 
        echo 'Hauteur: ' . $hauteur. ' cm<br>';
    	
    	$largeur= get_post_meta( get_the_ID(), 'wcv_custom_product_largeur', true ); 
        echo 'Largeur: ' . $largeur. ' cm<br>';
    	
    	$epaisseur = get_post_meta( get_the_ID(), 'wcv_custom_product_epaisseur', true ); 
        echo 'épaisseur: ' . $epaisseur . ' kg<br>';
     }
    

    /*************************************************************************************************
    * STEP 6
    * CHANGE THE TEXT WHEN A PRODUCT IS OUT OF Stock
    * ADD THIS CODE TO YOUR FUNCTIONS.PHP FILE
    *************************************************************************************************/

    add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
    function wcs_custom_get_availability( $availability, $_product ) {
        
        // Change In Stock Text
        if ( $_product->is_in_stock() ) {
            $availability['availability'] = __('Oeuvre disponible!', 'woocommerce');
        }
        // Change Out of Stock Text
        if ( ! $_product->is_in_stock() ) {
            $availability['availability'] = __('Oeuvre vendue', 'woocommerce');
        }
        return $availability;
    }
    
    #41406
    SKYNET
    Participant

    Thank you Marc to help me with customization on my website.
    I could not put the size and weight to add a product in the dashboard, now i can 🙂

    Link of the video customization:
    http://recordit.co/DKFd5N5e5B

    #41433
    Anna
    Member

    @skynet @marc.bovet
    Thank you for sharing this work- this will likely be helpful to other users of Wc Vendors Pro. 🙂

    #43151
    flowe
    Participant

    Okey, tried it, but I have a few problems.
    My new custom field (Budget) shows up on the WC Vendors Pro Add Product Form.
    When editing an existig product, I also see the values I entered.
    However, I do not see it in the Woocommerce Admin Backend (Products) nor in the shop pages.
    Any Idea what is wrong with my code?

    I suspect the error is in the functions.php file somewhere (Child theme)

    ———————-
    // Custom Product field

    function mysocialart_custom_fields_admin() {

    echo ‘<h4>BUDGET</h4>’;

    // FIELD Budget
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘wcv_custom_product_budget’,
    ‘data_type’ => ‘text’,
    ‘label’ => __(‘Budget €:’, ‘msk’),
    ‘placeholder’ => __(‘00.00’, ‘msk’),
    ‘description’ => __(‘Enter your Project Budget “desc_tip” when bla “true”.’, ‘msk’),
    ‘desc_tip’ => true // If “true”, the description will open in an infobubble
    )
    );

    }

    // CUSTOM FIELDS WILL BE ADDED TO THE GENERAL TAB OF THE PRODUCT
    add_action(‘woocommerce_product_options_general_product_data’, ‘mysocialart_custom_fields_admin’);

    function save_mysocialart_custom_fields_admin($product_id, $post, $update) {
    if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) return;

    if ($post->post_type == ‘product’) {

    // save budget
    if (isset($_POST[‘wcv_custom_product_budget’])) {
    $wcv_custom_product_budget = $_POST[‘wcv_custom_product_budget’];
    update_post_meta($product_id, ‘wcv_custom_product_budget’, $wcv_custom_product_budget);
    }
    }
    }
    add_action(‘save_post’, ‘save_mysocialart_custom_fields_admin’, 10, 3);

    add_filter( ‘woocommerce_product_tabs’, ‘woo_new_product_tab’ );
    function woo_new_product_tab( $tabs ) {

    // Adds the new tab

    $tabs[‘test_tab’] = array(
    ‘title’ => __( ‘Budget’, ‘woocommerce’ ), // THE NAME OF YOUR TAB.
    ‘priority’ => 50,
    ‘callback’ => ‘wcv_mysocialart_custom_fields_product_page’ // => SEE STEP 5
    );

    return $tabs;

    }

    function wcv_mysocialart_custom_fields_product_page() {

    $budget = get_post_meta( get_the_ID(), ‘wcv_custom_product_budget’, true );
    echo ‘Budget: ‘ . $budget . ‘ €<br>’;

    }

    add_filter( ‘woocommerce_get_availability’, ‘wcs_custom_get_availability’, 1, 2);
    function wcs_custom_get_availability( $availability, $_product ) {

    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
    $availability[‘availability’] = __(‘Oeuvre disponible!’, ‘woocommerce’);
    }
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
    $availability[‘availability’] = __(‘Oeuvre vendue’, ‘woocommerce’);
    }
    return $availability;
    }

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.