Support » Theme: OceanWP » Product custom fields won’t show on cart/checkout/orders and emails.

  • Hi.

    I tried adding a custom field (text field) for products and i have made this field “requierd”. The issue is that in oceanwp it won’t show the custom field on cart/checkout etc… another issue is that the user can leave this field empty and continue purchasing the product.

    If i enter to the theme customizer options and press “yes” on the “Removing custom WooCommerce features” it works well.

    Also works well if i use any other theme.

    Any clues on how to fix this issue?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Can you share your site link and tell me the steps to replicate the issue so that I can check it on my end?

    Thread Starter asafmoraz

    (@asafmoraz)

    Hi, sorry but my site is still under construction and it is stored locally on my computer using mamp.

    The thing is that no matter which way you try to embed a custom field (plugin or code), you won’t see the option in the shopping cart and payment page.

    For example:

    <?php
    /**
    * Plugin Name: Custom Fields for WooCommerce
    * Description: Add custom fields to WooCommerce products
    * Version: 1.0.0
    * Author: Gareth Harris
    * Author URI: https://pluginrepublic.com/
    * Text Domain: cfwc
    * WC requires at least: 3.4.0
    * WC tested up to: 3.4.2
    */

    // Exit if accessed directly
    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    /**
    * Display the custom text field
    * @since 1.0.0
    */
    function cfwc_create_custom_field() {
    $args = array(
    ‘id’ => ‘custom_text_field_title’,
    ‘label’ => __( ‘Custom Text Field Title’, ‘cfwc’ ),
    ‘class’ => ‘cfwc-custom-field’,
    ‘desc_tip’ => true,
    ‘description’ => __( ‘Enter the title of your custom text field.’, ‘ctwc’ ),
    );
    woocommerce_wp_text_input( $args );
    }
    add_action( ‘woocommerce_product_options_general_product_data’, ‘cfwc_create_custom_field’ );

    /**
    * Save the custom field
    * @since 1.0.0
    */
    function cfwc_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST[‘custom_text_field_title’] ) ? $_POST[‘custom_text_field_title’] : ”;
    $product->update_meta_data( ‘custom_text_field_title’, sanitize_text_field( $title ) );
    $product->save();
    }
    add_action( ‘woocommerce_process_product_meta’, ‘cfwc_save_custom_field’ );

    /**
    * Display custom field on the front end
    * @since 1.0.0
    */
    function cfwc_display_custom_field() {
    global $post;
    // Check for the custom field value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( ‘custom_text_field_title’ );
    if( $title ) {
    // Only display our field if we’ve got a value for the field title
    printf(
    ‘<div class=”cfwc-custom-field-wrapper”><label for=”cfwc-title-field”>%s</label><input type=”text” id=”cfwc-title-field” name=”cfwc-title-field” value=””></div>’,
    esc_html( $title )
    );
    }
    }
    add_action( ‘woocommerce_before_add_to_cart_button’, ‘cfwc_display_custom_field’ );

    /**
    * Validate the text field
    * @since 1.0.0
    * @param Array $passed Validation status.
    * @param Integer $product_id Product ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_validate_custom_field( $passed, $product_id, $quantity ) {
    if( empty( $_POST[‘cfwc-title-field’] ) ) {
    // Fails validation
    $passed = false;
    wc_add_notice( __( ‘Please enter a value into the text field’, ‘cfwc’ ), ‘error’ );
    }
    return $passed;
    }
    add_filter( ‘woocommerce_add_to_cart_validation’, ‘cfwc_validate_custom_field’, 10, 3 );

    /**
    * Add the text field as item data to the cart object
    * @since 1.0.0
    * @param Array $cart_item_data Cart item meta data.
    * @param Integer $product_id Product ID.
    * @param Integer $variation_id Variation ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    if( ! empty( $_POST[‘cfwc-title-field’] ) ) {
    // Add the item data
    $cart_item_data[‘title_field’] = $_POST[‘cfwc-title-field’];
    $product = wc_get_product( $product_id ); // Expanded function
    $price = $product->get_price(); // Expanded function
    $cart_item_data[‘total_price’] = $price + 100; // Expanded function
    }
    return $cart_item_data;
    }
    add_filter( ‘woocommerce_add_cart_item_data’, ‘cfwc_add_custom_field_item_data’, 10, 4 );

    /**
    * Update the price in the cart
    * @since 1.0.0
    */
    function cfwc_before_calculate_totals( $cart_obj ) {
    if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) {
    return;
    }
    // Iterate through each cart item
    foreach( $cart_obj->get_cart() as $key=>$value ) {
    if( isset( $value[‘total_price’] ) ) {
    $price = $value[‘total_price’];
    $value[‘data’]->set_price( ( $price ) );
    }
    }
    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘cfwc_before_calculate_totals’, 10, 1 );

    /**
    * Display the custom field value in the cart
    * @since 1.0.0
    */
    function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item[‘title_field’] ) ) {
    $name .= sprintf(
    ‘<p>%s</p>’,
    esc_html( $cart_item[‘title_field’] )
    );
    }
    return $name;
    }
    add_filter( ‘woocommerce_cart_item_name’, ‘cfwc_cart_item_name’, 10, 3 );

    /**
    * Add custom field to order object
    */
    function cfwc_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
    if( isset( $values[‘title_field’] ) ) {
    $item->add_meta_data( __( ‘Custom Field’, ‘cfwc’ ), $values[‘title_field’], true );
    }
    }
    }
    add_action( ‘woocommerce_checkout_create_order_line_item’, ‘cfwc_add_custom_data_to_order’, 10, 4 );
    <?php
    /**
    * Plugin Name: Custom Fields for WooCommerce
    * Description: Add custom fields to WooCommerce products
    * Version: 1.0.0
    * Author: Gareth Harris
    * Author URI: https://pluginrepublic.com/
    * Text Domain: cfwc
    * WC requires at least: 3.4.0
    * WC tested up to: 3.4.2
    */

    // Exit if accessed directly
    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    /**
    * Display the custom text field
    * @since 1.0.0
    */
    function cfwc_create_custom_field() {
    $args = array(
    ‘id’ => ‘custom_text_field_title’,
    ‘label’ => __( ‘Custom Text Field Title’, ‘cfwc’ ),
    ‘class’ => ‘cfwc-custom-field’,
    ‘desc_tip’ => true,
    ‘description’ => __( ‘Enter the title of your custom text field.’, ‘ctwc’ ),
    );
    woocommerce_wp_text_input( $args );
    }
    add_action( ‘woocommerce_product_options_general_product_data’, ‘cfwc_create_custom_field’ );

    /**
    * Save the custom field
    * @since 1.0.0
    */
    function cfwc_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST[‘custom_text_field_title’] ) ? $_POST[‘custom_text_field_title’] : ”;
    $product->update_meta_data( ‘custom_text_field_title’, sanitize_text_field( $title ) );
    $product->save();
    }
    add_action( ‘woocommerce_process_product_meta’, ‘cfwc_save_custom_field’ );

    /**
    * Display custom field on the front end
    * @since 1.0.0
    */
    function cfwc_display_custom_field() {
    global $post;
    // Check for the custom field value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( ‘custom_text_field_title’ );
    if( $title ) {
    // Only display our field if we’ve got a value for the field title
    printf(
    ‘<div class=”cfwc-custom-field-wrapper”><label for=”cfwc-title-field”>%s</label><input type=”text” id=”cfwc-title-field” name=”cfwc-title-field” value=””></div>’,
    esc_html( $title )
    );
    }
    }
    add_action( ‘woocommerce_before_add_to_cart_button’, ‘cfwc_display_custom_field’ );

    /**
    * Validate the text field
    * @since 1.0.0
    * @param Array $passed Validation status.
    * @param Integer $product_id Product ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_validate_custom_field( $passed, $product_id, $quantity ) {
    if( empty( $_POST[‘cfwc-title-field’] ) ) {
    // Fails validation
    $passed = false;
    wc_add_notice( __( ‘Please enter a value into the text field’, ‘cfwc’ ), ‘error’ );
    }
    return $passed;
    }
    add_filter( ‘woocommerce_add_to_cart_validation’, ‘cfwc_validate_custom_field’, 10, 3 );

    /**
    * Add the text field as item data to the cart object
    * @since 1.0.0
    * @param Array $cart_item_data Cart item meta data.
    * @param Integer $product_id Product ID.
    * @param Integer $variation_id Variation ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    if( ! empty( $_POST[‘cfwc-title-field’] ) ) {
    // Add the item data
    $cart_item_data[‘title_field’] = $_POST[‘cfwc-title-field’];
    $product = wc_get_product( $product_id ); // Expanded function
    $price = $product->get_price(); // Expanded function
    $cart_item_data[‘total_price’] = $price + 100; // Expanded function
    }
    return $cart_item_data;
    }
    add_filter( ‘woocommerce_add_cart_item_data’, ‘cfwc_add_custom_field_item_data’, 10, 4 );

    /**
    * Update the price in the cart
    * @since 1.0.0
    */
    function cfwc_before_calculate_totals( $cart_obj ) {
    if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) {
    return;
    }
    // Iterate through each cart item
    foreach( $cart_obj->get_cart() as $key=>$value ) {
    if( isset( $value[‘total_price’] ) ) {
    $price = $value[‘total_price’];
    $value[‘data’]->set_price( ( $price ) );
    }
    }
    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘cfwc_before_calculate_totals’, 10, 1 );

    /**
    * Display the custom field value in the cart
    * @since 1.0.0
    */
    function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item[‘title_field’] ) ) {
    $name .= sprintf(
    ‘<p>%s</p>’,
    esc_html( $cart_item[‘title_field’] )
    );
    }
    return $name;
    }
    add_filter( ‘woocommerce_cart_item_name’, ‘cfwc_cart_item_name’, 10, 3 );

    /**
    * Add custom field to order object
    */
    function cfwc_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
    if( isset( $values[‘title_field’] ) ) {
    $item->add_meta_data( __( ‘Custom Field’, ‘cfwc’ ), $values[‘title_field’], true );
    }
    }
    }
    add_action( ‘woocommerce_checkout_create_order_line_item’, ‘cfwc_add_custom_data_to_order’, 10, 4 );

    <?php
    /**
    * Plugin Name: Custom Fields for WooCommerce
    * Description: Add custom fields to WooCommerce products
    * Version: 1.0.0
    * Author: Gareth Harris
    * Author URI: https://pluginrepublic.com/
    * Text Domain: cfwc
    * WC requires at least: 3.4.0
    * WC tested up to: 3.4.2
    */

    // Exit if accessed directly
    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    /**
    * Display the custom text field
    * @since 1.0.0
    */
    function cfwc_create_custom_field() {
    $args = array(
    ‘id’ => ‘custom_text_field_title’,
    ‘label’ => __( ‘Custom Text Field Title’, ‘cfwc’ ),
    ‘class’ => ‘cfwc-custom-field’,
    ‘desc_tip’ => true,
    ‘description’ => __( ‘Enter the title of your custom text field.’, ‘ctwc’ ),
    );
    woocommerce_wp_text_input( $args );
    }
    add_action( ‘woocommerce_product_options_general_product_data’, ‘cfwc_create_custom_field’ );

    /**
    * Save the custom field
    * @since 1.0.0
    */
    function cfwc_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST[‘custom_text_field_title’] ) ? $_POST[‘custom_text_field_title’] : ”;
    $product->update_meta_data( ‘custom_text_field_title’, sanitize_text_field( $title ) );
    $product->save();
    }
    add_action( ‘woocommerce_process_product_meta’, ‘cfwc_save_custom_field’ );

    /**
    * Display custom field on the front end
    * @since 1.0.0
    */
    function cfwc_display_custom_field() {
    global $post;
    // Check for the custom field value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( ‘custom_text_field_title’ );
    if( $title ) {
    // Only display our field if we’ve got a value for the field title
    printf(
    ‘<div class=”cfwc-custom-field-wrapper”><label for=”cfwc-title-field”>%s</label><input type=”text” id=”cfwc-title-field” name=”cfwc-title-field” value=””></div>’,
    esc_html( $title )
    );
    }
    }
    add_action( ‘woocommerce_before_add_to_cart_button’, ‘cfwc_display_custom_field’ );

    /**
    * Validate the text field
    * @since 1.0.0
    * @param Array $passed Validation status.
    * @param Integer $product_id Product ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_validate_custom_field( $passed, $product_id, $quantity ) {
    if( empty( $_POST[‘cfwc-title-field’] ) ) {
    // Fails validation
    $passed = false;
    wc_add_notice( __( ‘Please enter a value into the text field’, ‘cfwc’ ), ‘error’ );
    }
    return $passed;
    }
    add_filter( ‘woocommerce_add_to_cart_validation’, ‘cfwc_validate_custom_field’, 10, 3 );

    /**
    * Add the text field as item data to the cart object
    * @since 1.0.0
    * @param Array $cart_item_data Cart item meta data.
    * @param Integer $product_id Product ID.
    * @param Integer $variation_id Variation ID.
    * @param Boolean $quantity Quantity
    */
    function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    if( ! empty( $_POST[‘cfwc-title-field’] ) ) {
    // Add the item data
    $cart_item_data[‘title_field’] = $_POST[‘cfwc-title-field’];
    $product = wc_get_product( $product_id ); // Expanded function
    $price = $product->get_price(); // Expanded function
    $cart_item_data[‘total_price’] = $price + 100; // Expanded function
    }
    return $cart_item_data;
    }
    add_filter( ‘woocommerce_add_cart_item_data’, ‘cfwc_add_custom_field_item_data’, 10, 4 );

    /**
    * Update the price in the cart
    * @since 1.0.0
    */
    function cfwc_before_calculate_totals( $cart_obj ) {
    if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) {
    return;
    }
    // Iterate through each cart item
    foreach( $cart_obj->get_cart() as $key=>$value ) {
    if( isset( $value[‘total_price’] ) ) {
    $price = $value[‘total_price’];
    $value[‘data’]->set_price( ( $price ) );
    }
    }
    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘cfwc_before_calculate_totals’, 10, 1 );

    /**
    * Display the custom field value in the cart
    * @since 1.0.0
    */
    function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item[‘title_field’] ) ) {
    $name .= sprintf(
    ‘<p>%s</p>’,
    esc_html( $cart_item[‘title_field’] )
    );
    }
    return $name;
    }
    add_filter( ‘woocommerce_cart_item_name’, ‘cfwc_cart_item_name’, 10, 3 );

    /**
    * Add custom field to order object
    */
    function cfwc_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
    if( isset( $values[‘title_field’] ) ) {
    $item->add_meta_data( __( ‘Custom Field’, ‘cfwc’ ), $values[‘title_field’], true );
    }
    }
    }
    add_action( ‘woocommerce_checkout_create_order_line_item’, ‘cfwc_add_custom_data_to_order’, 10, 4 );

    Thread Starter asafmoraz

    (@asafmoraz)

    Or this one:

    /**
    * @snippet Add input field to products – WooCommerce
    * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
    * @author Rodolfo Melogli
    * @compatible WooCommerce 3.5.7
    * @donate $9 https://businessbloomer.com/bloomer-armada/
    */

    // —————————————–
    // 1. Show custom input field above Add to Cart

    add_action( ‘woocommerce_before_add_to_cart_button’, ‘bbloomer_product_add_on’, 9 );

    function bbloomer_product_add_on() {
    $value = isset( $_POST[‘_custom_text_add_on’] ) ? sanitize_text_field( $_POST[‘_custom_text_add_on’] ) : ”;
    echo ‘<div><label>שם התינוק <abbr class=”required” title=”required”>*</abbr></label><p><input name=”_custom_text_add_on” value=”‘ . $value . ‘”></p></div>’;
    }

    // —————————————–
    // 2. Throw error if custom input field empty

    add_filter( ‘woocommerce_add_to_cart_validation’, ‘bbloomer_product_add_on_validation’, 10, 3 );

    function bbloomer_product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST[‘_custom_text_add_on’] ) && sanitize_text_field( $_POST[‘_custom_text_add_on’] ) == ” ) {
    wc_add_notice( ‘שם התינוק is a required field’, ‘יש לרשום את שם התינוק’ );
    $passed = false;
    }
    return $passed;
    }

    // —————————————–
    // 3. Save custom input field value into cart item data

    add_filter( ‘woocommerce_add_cart_item_data’, ‘bbloomer_product_add_on_cart_item_data’, 10, 2 );

    function bbloomer_product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST[‘_custom_text_add_on’] ) ) {
    $cart_item[‘custom_text_add_on’] = sanitize_text_field( $_POST[‘_custom_text_add_on’] );
    }
    return $cart_item;
    }

    // —————————————–
    // 4. Display custom input field value @ Cart

    add_filter( ‘woocommerce_get_item_data’, ‘bbloomer_product_add_on_display_cart’, 10, 2 );

    function bbloomer_product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item[‘custom_text_add_on’] ) ){
    $data[] = array(
    ‘name’ => ‘שם התינוק’,
    ‘value’ => sanitize_text_field( $cart_item[‘custom_text_add_on’] )
    );
    }
    return $data;
    }

    // —————————————–
    // 5. Save custom input field value into order item meta

    add_action( ‘woocommerce_add_order_item_meta’, ‘bbloomer_product_add_on_order_item_meta’, 10, 2 );

    function bbloomer_product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values[‘custom_text_add_on’] ) ) {
    wc_add_order_item_meta( $item_id, ‘שם התינוק Add-On’, $values[‘custom_text_add_on’], true );
    }
    }

    // —————————————–
    // 6. Display custom input field value into order table

    add_filter( ‘woocommerce_order_item_product’, ‘bbloomer_product_add_on_display_order’, 10, 2 );

    function bbloomer_product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item[‘custom_text_add_on’] ) ){
    $cart_item_meta[‘custom_text_add_on’] = $order_item[‘custom_text_add_on’];
    }
    return $cart_item;
    }

    // —————————————–
    // 7. Display custom input field value into order emails

    add_filter( ‘woocommerce_email_order_meta_fields’, ‘bbloomer_product_add_on_display_emails’ );

    function bbloomer_product_add_on_display_emails( $fields ) {
    $fields[‘custom_text_add_on’] = ‘שם התינוק’;
    return $fields;
    }

    • This reply was modified 2 years, 10 months ago by asafmoraz.

    Hello,

    Thank you for the detailed explanation. I’ll check the issue and get back to you.

    Thread Starter asafmoraz

    (@asafmoraz)

    Thank you 🙂

    Thread Starter asafmoraz

    (@asafmoraz)

    Hi,

    Just letting you know that i solved this issue by disabling “ajax add to cart”. I still has this problem in “quickview” so i disabled quickview for now because as much as i understand, there is no way to disable ajax add to cart in quickview.

    If you will come up with a solution to keep ajax add to cart i will be very happy 🙂

    Thanks,
    Asaf.

    Hello,

    I replied to your ticket on OceanWP support. Please check that.

    Thread Starter asafmoraz

    (@asafmoraz)

    Thank you very much 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Product custom fields won’t show on cart/checkout/orders and emails.’ is closed to new replies.