Top 5 Programming Questions and Answers Regarding WooCommerce Product Options Part - 1

Product options are very important for e-stores owners who sell products with different attributes such as color, size etc. But it has seemed that WordPress programmers are getting issues while creating product options and they ask for the solution on different platforms. FMEAddons has decided to provides answers of top five question in this post. Provided answers are taken from the community and do not represent us. Now have a look on 5 top programming questions regarding WooCommerce product options with their best possible answers.

Important Note:

These are the answers provided by the community and best rated by the community as well. 


Question No. 1

A user sells shoes with various options such as size, width color etc and he wants the value to be changed based on customer selected option. If the customer selects small then values should be 1,2,3 if medium then values should be 4,5 and for large it should be 6. He wants to achieve this from WordPress backend.


You have to create products attribute, go to product menu and click attribute. Here you have added an attribute named size and click cog icon. Then add numeric terms to call slugs for size 1, 2 etc. After that, it displays product attribute in a drop down menu.

Question No. 2

A user wants to enable review on products pages in WooCommerce. He has added new product on his website and now he wants to enable review in bulk, for existing products and new products as well. Plus, if create a new product, the review should be auto-enabled on that product.


This can be done in two steps given below.

Step 1

Apply this code in function.php and navigate the products on the front end. Once done then remove or comment this code.

// Updating all products that have a 'comment_status' => 'closed' to 'open'

function updating_existing_products_once(){
    $args = array(
        // WC product post type
        'post_type'   => 'product',
        // all posts
        'numberposts' => -1,
        'comment_status' => 'closed',
        'post_status' => 'publish',

    $shop_products = get_posts( $args );
    foreach( $shop_products as $item){
        $product = new WC_Product($item->ID);
        wp_update_post( array(
            'ID'    => $item->ID,
            'comment_status' => 'open',
        ) );
// After usage comment this line below

Step 2:

Now apply function, as it will update new added items have 'comment_status' => 'closed' to 'open'.

add_action('transition_post_status', 'creating_a_new_product', 10, 3);
function creating_a_new_product($new_status, $old_status, $post) {
    if( $old_status != 'publish' && $new_status == 'publish' && !empty($post->ID)  && in_array( $post->post_type, array( 'product') ) ) {
        if ($post->comment_status != 'open' ){
            $product = new WC_Product($post->ID);
            wp_update_post( array(
                'ID'    => $post->ID,
                'comment_status' => 'open',
            ) );

This code goes to function.php file for the child theme you have activated works in any plugin file or any extension file. 

Question No. 3

A user experiencing z-index issue and unable to find any solution. When anybody zooms the pictures on his eCommerce website, the zoom option goes under the options drop down menu. He tried to change both zoom window and z-index of drop downs but issues remain same. Moreover, the slider on a home page is not in the center with the actual slider. He tried a lot but failed to move it.


Use this CSS to fixed the issue

.sidebar-left .sidebar, .sidebar-right .content {
   position: relative; 
   z-index: 0

.sidebar-right .sidebar, .sidebar-left .content {
   position: relative; 

When you change z-index of the element then it should be positioned also and it should be should be fixed positioning, absolute or relative in order to apply z-index.

Question No. 4

A user wants to add the phone number field on inquiry form available on product pages. As the inquiry form contains the name, email, and subject.


There are two ways of adding a phone number field on inquiry form. The first one is to copy the below mentioned code to your platform, and the other is with a quick installation of a plugin.

To do it with a code, please follow the following procedure.

Follow the steps but first take backup.

Go to wp-content > plugins > product-enquiry-for-woocommerce > data >contact.php and replace the content of contact.php file with the code given below.

    /* SimpleModal Contact Form
     * Copyright (c) 2012 Eric Martin -
     * Licensed under the MIT license:
     * Revision: $Id: contact-dist.php 269 2011-12-17 23:24:14Z emartin24 $

    global $to;
        $form_data = $_POST['wdm_form_dataset'];

        $to_adm = $_POST['wdm_admin_email'];

        $site_name = $_POST['wdm_site_name'];

    // User settings
    if (!empty($form_data['user_email']))
        $to = $form_data['user_email'];
    else if(isset($to_adm))
        $to = $to_adm;

    if (!empty($form_data['default_sub']))
        $subject = $form_data['default_sub'];
    else if(isset($site_name))
        $subject = "Enquiry for a product from ".$site_name;

     //Include extra form fields and/or submitter data?
     //false = do not include

    $extra = array(
        "form_subject"  => true,
        "your_number" =>true,
        "form_cc"   => (isset($form_data['enable_send_mail_copy']) ? true : false),
        "ip"        => false,
        "user_agent"    => false

    // Process
    $action = isset($_POST["action"]) ? $_POST["action"] : "";
    if (empty($action)) {
        // Send back the contact form HTML
        $output = "<div style='display:none'>
        <div class='contact-top'></div>
        <div class='contact-content'>
            <h1 class='contact-title'>Product Enquiry:</h1>
            <div class='contact-loading' style='display:none'></div>
            <div class='contact-message' style='display:none'></div>
            <form action='#' style='display:none'>

                <input type='text' id='wdm_product_name' class='contact-input' name='wdm_product_name' value='' readonly=true />
                <label for='contact-name'>*Name:</label>
                <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
                <label for='contact-email'>*Email:</label>
                <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";

        if ($extra["form_subject"] && $extra["your_number"]) {
            $output .= "
                <label for='contact-subject'>Subject:</label>
                <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
            $output .= "
                <label for='contact-phone'>Phone Number:</label>
                <input type='text' id='contact-phone' class='contact-input' name='phone' value='' tabindex='1007' />";

        $output .= "<input type='hidden' id='wdm_product_url' class='contact-input' name='wdm_product_url' value='' />";

        $output .= "<input type='hidden' id='wdm_form_mail_to' class='contact-input' name='wdm_form_mail_to' value='' />";

        $output .= "<input type='hidden' id='wdm_form_def_sub' class='contact-input' name='wdm_form_def_sub' value='' />";

        $output .= "<input type='hidden' id='wdm_website_name' class='contact-input' name='wdm_website_name' value='' />";

        $output .= "
                <label for='contact-message'>*Enquiry:</label>
                <textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>

        if ($extra["form_cc"]) {
            $output .= "
                <input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>

        $output .= "
                <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
                <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
                <input type='hidden' name='token' value='" . smcf_token($to) . "'/>
        <div class='contact-bottom'><a href='' target='_blank'>Powered by WisdmLabs</a></div>

     echo $output;

     $to = base64_encode($to);
     $subject = base64_encode($subject);
     $site_name = base64_encode($site_name);

      echo '<script type="text/javascript">


    else if ($action == "wdm_send") {
        // Send the email
        //echo "<pre>";print_r($_REQUEST);echo "</pre>";exit;
        $name = isset($_POST["name"]) ? $_POST["name"] : "";
        $email = isset($_POST["email"]) ? $_POST["email"] : "";
        $urphone = isset($_POST["phone"]) ? $_POST["phone"] : "";
        $subject = isset($_POST["wdm_form_def_sub"]) ? $_POST["wdm_form_def_sub"] : "";
        $subject = base64_decode($subject);
        $subject = !empty($_POST["subject"]) ? $_POST["subject"] : $subject;
        $product_url = isset($_POST["wdm_product_url"]) ? $_POST["wdm_product_url"] : "";
        $product_name = isset($_POST["wdm_product_name"]) ? $_POST["wdm_product_name"] : "";
        $message = isset($_POST["message"]) ? $_POST["message"] : "";
        $cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
        $token = isset($_POST["token"]) ? $_POST["token"] : "";
        $to = isset($_POST["wdm_form_mail_to"]) ? $_POST["wdm_form_mail_to"] : "";
        $to = base64_decode($to);
        //echo $to;exit;
        $site_name = isset($_POST["wdm_website_name"]) ? $_POST["wdm_website_name"] : "";
        $site_name = base64_decode($site_name);

        // make sure the token matches
        if ($token === smcf_token($to)) {
            smcf_send($name, $email, $subject, $urphone, $product_url, $product_name, $site_name, $message, $cc);
            echo "Your enquiry sent successfully. We will get back to you soon.";
        else {
            echo "Unfortunately, your enquiry could not be verified.";

    function smcf_token($s) {
        return md5("smcf-" . $s . date("WY"));

    // Validate and send email
    function smcf_send($name, $email, $subject, $urphone, $product_url, $product_name, $site_name, $message, $cc) {
        global $to, $extra;

        // Filter and validate fields
        $name = smcf_filter($name);
        $subject = smcf_filter($subject);
        $email = smcf_filter($email);

        if (!smcf_validate_email($email)) {
            $subject .= " - invalid email";
            $message .= "\n\nBad email: $email";
            $email = $to;
            $cc = 0; // do not CC "sender"

        // Add additional info to the message
        if ($extra["ip"]) {
            $message .= "\n\nIP: " . $_SERVER["REMOTE_ADDR"];
        if ($extra["user_agent"]) {
            $message .= "\n\nUSER AGENT: " . $_SERVER["HTTP_USER_AGENT"];

        // Set and wordwrap message body
        $body = "Product Enquiry from <strong>". $site_name . "</strong> <br /><br />";
        $body .= "<strong>Product Name:</strong> '". $product_name ."'<br /><br />";
        $body .= "<strong>Product URL:</strong> ". $product_url ."<br /><br />";
        $body .= "<strong>Customer Name:</strong> ". $name ."<br /><br />";
        $body .= "<strong>Customer Email:</strong> ". $email ."<br /><br />";
        $body .= "<strong>Customer Phone Number:</strong> ". $urphone ."<br /><br />";
        $body .= "<strong>Message:</strong> <br />". $message;
        $body = wordwrap($body, 100);

        // Build header
        $headers = "From: $email\n";
        $headers .= "Reply-To: $email \n";
        if ($cc == 1) {
            $headers .= "Cc: $email\n";
        // $headers .= "X-Mailer: PHP/SimpleModalContactForm";

        // UTF-8
        if (function_exists('mb_encode_mimeheader')) {
            $subject = mb_encode_mimeheader($subject, "UTF-8", "B", "\n");
        else {
            // you need to enable mb_encode_mimeheader or risk 
            // getting emails that are not UTF-8 encoded
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=utf-8\n";
        $headers .= "Content-Transfer-Encoding: quoted-printable\n";
        //echo "to ".$to."<br>";
        //echo "subject ".$subject."<br>";
        //echo "body ".$body."<br>";
        //echo "headers ".$headers."<br>";exit;
        wp_mail($to, $subject, $body, $headers) or die("Unfortunately, a server issue prevented delivery of your message.");

        //wp_mail($to, $subject, $body, $headers) or 
        //  die("Unfortunately, a server issue prevented delivery of your message.");

    // Remove any un-safe values to prevent email injection
    function smcf_filter($value) {
        $pattern = array("/\n/","/\r/","/content-type:/i","/to:/i", "/from:/i", "/cc:/i");
        $value = preg_replace($pattern, "", $value);
        return $value;

    // Validate email address format in case client-side validation "fails"
    function smcf_validate_email($email) {
        $at = strrpos($email, "@");

        // Make sure the at (@) sybmol exists and  
        // it is not the first or last character
        if ($at && ($at < 1 || ($at + 1) == strlen($email)))
            return false;

        // Make sure there aren't multiple periods together
        if (preg_match("/(\.{2,})/", $email))
            return false;

        // Break up the local and domain portions
        $local = substr($email, 0, $at);
        $domain = substr($email, $at + 1);

        // Check lengths
        $locLen = strlen($local);
        $domLen = strlen($domain);
        if ($locLen < 1 || $locLen > 64 || $domLen < 4 || $domLen > 255)
            return false;

        // Make sure local and domain don't start with or end with a period
        if (preg_match("/(^\.|\.$)/", $local) || preg_match("/(^\.|\.$)/", $domain))
            return false;

        // Check for quoted-string addresses
        // Since almost anything is allowed in a quoted-string address,
        // we're just going to let them go through
        if (!preg_match('/^"(.+)"$/', $local)) {
            // It's a dot-string address...check for valid characters
            if (!preg_match('/^[-a-zA-Z0-9!#$%*\/?|^{}`~&\'+=_\.]*$/', $local))
                return false;

        // Make sure domain contains only valid characters and at least one period
        if (!preg_match("/^[-a-zA-Z0-9\.]*$/", $domain) || !strpos($domain, "."))
            return false;   

        return true;

For the second procedure, we recommend the installation of Product Inquiry plugin on your website that will allow you to add numerous fields, and edit them multiple times.

We would like to suggest you WooCommerce product inquiry form plugin

Question No. 5

A user wants to display custom fields on the frontend in WooCommerce. He applies this code and fields are showing but labels are not.

    // Display custom field value
    echo get_post_meta( get_the_ID(), '_text_field', true );
    echo get_post_meta( get_the_ID(), '_number_field', true );
    echo get_post_meta( get_the_ID(), '_textarea', true );

After displaying field labels, he also wants to customize the labels


You can do this by applying this code

// Display custom field value 
    echo '<strong>Manufacturer: </strong>';
    echo get_post_meta( get_the_ID(), '_text_field_1', true );
    echo '<br>';
    echo '<strong>Brand: </strong>';
    echo get_post_meta( get_the_ID(), '_text_field', true );
    echo '<br>';
    echo '<strong>Retail Price: </strong>';
    echo get_post_meta( get_the_ID(), '_number_field_1', true );
    echo '<br>';
    echo '<strong>Arnolds Price: </strong>';
    echo get_post_meta( get_the_ID(), '_number_field', true );

    echo '<strong>Features: </strong>';
    echo get_post_meta( get_the_ID(), '_textarea_1', true );
    echo '<br>';
    echo '<strong>Additional Information: </strong>';
    echo get_post_meta( get_the_ID(), '_textarea', true );

Download WooCommerce Product Custom Option Plugin 

woocommerce product custom options

Leave a Reply