Custom Fields - by ctrimde - on WordPress WooCommerce support

This topic has 4 replies, 3 voices, and was last updated 10 years, 1 months ago ago by relentlessmike

  • Avatar: ctrimde
    ctrimde
    Participant
    May 29, 2015 at 21:30

    Hello Eva,

    I want to attach some custom fields to Woocommerce products like the UPC code you need for amazon. this is what i put in the function.php file:

    <?php
    define(‘ETHEME_DOMAIN’, ‘legenda’);
    require_once( get_template_directory() . ‘/framework/init.php’ );

    add_action( ‘woocommerce_product_options_pricing’, ‘wc_rrp_product_field’ );
    function wc_rrp_product_field() {
    woocommerce_wp_text_input( array( ‘id’ => ‘UPC’, ‘class’ => ‘wc_input_price short’, ‘label’ => __( UPC, ‘woocommerce’ ) .’)’ ) );
    }

    add_action( ‘save_post’, ‘wc_rrp_save_product’ );
    function wc_rrp_save_product( $product_id ) {
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
    return;
    if ( isset( $_POST[‘UPC’] ) ) {
    if ( is_numeric( $_POST[‘UPC’] ) )
    update_post_meta( $product_id, ‘UPC’, $_POST[‘UPC’] );
    } else delete_post_meta( $product_id, ‘UPC’ );
    }

    Now this show up nicely, however when i try to import products from WP all import- the custom field UPC does not show up…

    Do you have a code that other plugins would recognise as “a custom field” for products?

    Many thanks for your reply!

    3 Answers
    Avatar: Eva
    Eva Kemp
    Participant
    May 30, 2015 at 08:31

    Hello,

    Are you trying to add custom tab for a product?

    Regards,
    Eva Kemp.

    Avatar: ctrimde
    ctrimde
    Participant
    May 30, 2015 at 13:11

    Hello,

    no- just a Custom field which in the front end is actually NOT visible- but we need it in the backend since it is an important field for the Delivery Slip (barcode).

    Thanks;-)

    Avatar: relentlessmike
    relentlessmike
    Participant
    May 30, 2015 at 20:06

    Hello there-

    I have been working on the same thing for a few days now. I have used a very similar line of code that is giving me a field to enter the UPC when entering a new product, but I cannot get it to reflect properly on the front end. Even when I use the same line of code as above I still get a field that reflects the UPC as a price “$123,456,789.00″.

    I too need it to reflect in the custom fields for csv export.

    Here is what I have been playing with so far. It worked great for adding my MSRP.

    add_action( ‘woocommerce_product_options_pricing’, ‘wc_upc_product_field’ );
    function wc_upc_product_field() {
    woocommerce_wp_text_input( array( ‘id’ => ‘UPC’, ‘class’ => ‘wc_input_price short’, ‘label’ => __( ‘UPC’, ‘woocommerce’ ) . ” ) );
    }
    add_action( ‘save_post’, ‘wc_upc_save_product’ );
    function wc_upc_save_product( $product_id ) {
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
    return;
    if ( isset( $_POST[‘UPC’] ) ) {
    if ( is_numeric( $_POST[‘UPC’] ) )
    update_post_meta( $product_id, ‘UPC’, $_POST[‘UPC’] );
    } else delete_post_meta( $product_id, ‘UPC’ );
    }
    add_action( ‘woocommerce_single_product_summary’, ‘wc_upc_show’, 5 );
    function wc_upc_show() {
    global $product;
    // Do not show this on variable products
    if ( $product->product_type <> ‘variable’ ) {
    $upc = get_post_meta( $product->id, ‘UPC’, true );
    echo ‘<div class=”woocommerce_upc”>’;
    _e( ‘UPC: ‘, ‘woocommerce’ );
    echo ‘<span class=”woocommerce-upc-price”>’ . woocommerce_price( $upc ) . ‘</span>’;
    echo ‘</div>’;
    }
    }

  • Viewing 4 results - 1 through 4 (of 4 total)

The issue related to '‘Custom Fields’' has been successfully resolved, and the topic is now closed for further responses

Helpful Topics

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.