-1

I am unable to create custom fields through WooCommerce Hooks as shown

function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id'          => '_custom_product_title',
            'placeholder' => 'Arabic Product Title',
            'label'       => __('Arabic Product Title', 'woocommerce'),
            'desc_tip'    => 'true'
        )
    );

    woocommerce_wp_textarea_input(
        array(
            'id'          => '_arabic_desc',
            'placeholder' => 'Arabic Description',
            'label'       => __('Arabic Description', 'woocommerce')
        )
    );
    echo '</div>';
}

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_title'];
  if (!empty($woocommerce_custom_product_text_field))
     update_post_meta($post_id, '_custom_product_title', esc_attr($woocommerce_custom_product_text_field));
    
/*$woocommerce_custom_product_textarea = $_POST['_arabic_desc'];
  if (!empty($woocommerce_custom_product_textarea))
     update_post_meta($post_id, '_arabic_desc', esc_html($woocommerce_custom_product_textarea));*/
}
// Save Fields
//add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

But the Fields do not appear in Admin panel > New Products or in the Vendor > Add product pages.

I found two mentions in the WP.org from the WooCommerce authority here also this thread

So, did they just remove the functionality of these hooks in free method? Why wont that be documented then?

**

Update: I was looking at the wrong place to see the code running. And as the asnwerer reported it worked for him. I checked more to find out any output this could have generated, and found that, I had a tab collapsed which was recently updated due to WooCommerce plugin update at the previous day. Of course was some frustrating moments for a couple of days. Partly was for the typos I had at the 2nd TextArea name initially, but later it was due to collapsed tab and expecting result at another dashboard run by WCMarketPlace (another addon for vendors from WooCommerce).

**

Lenin
  • 570
  • 16
  • 36

1 Answers1

1

I just checked the latest WooCommerce files since the update three days ago to verify the hooks and methods are still the same, and they are. woocommerce_product_options_general_product_data is still being used, as well as woocommerce_wp_text_input.

I then tested your code and for me it works.

enter image description here

You do have some typos in the code where you save your textarea. Here's the code without your typos (I believe you wrote procut instead of product).

$woocommerce_custom_product_textarea = $_POST['_arabic_desc'];
  if (!empty($woocommerce_custom_product_textarea))
     update_post_meta($post_id, '_arabic_desc', esc_html($woocommerce_custom_product_textarea));
Tom
  • 387
  • 2
  • 12
  • Yeah there had been typos but, I have them corrected too. I wonder why all my tests failed. And I also wonder how these typos even came upto here, I corrected them previously! – Lenin Oct 17 '20 at 11:04
  • Happens to the best, anyway there's nothing wrong with the code. I added it to the bottom of my functions.php in a clean WordPress install with just WooCommerce. Where did you put your code? – Tom Oct 17 '20 at 11:05
  • are you using free WooCommerce or Pro? – Lenin Oct 17 '20 at 11:06
  • I did put the code into the snippets list with high priority. I avoid using functions.php for clutters. – Lenin Oct 17 '20 at 11:07
  • I created a clean install of WordPress with Local and downloaded the free WooCommerce plugin. Is it still not working for you? – Tom Oct 17 '20 at 11:07
  • Please try the code on the bottom of your functions.php first, to see if it works. If you think it clutters the file too much, you can then put it in a separate file and include it in your functions afterwards. You might want to read this for best practices: https://wordpress.stackexchange.com/questions/1403/organizing-code-in-your-wordpress-themes-functions-php-file – Tom Oct 17 '20 at 11:08
  • 1
    Oh man! you are a life-saver. I just checked again at the Product page. The interface got changed a little and the General Tab was collapsed so I did not notice it appeared! Whoa! – Lenin Oct 17 '20 at 11:09
  • WooCommerce got updated yesterday and the Look-n-Feel is a lot changed. I was so frustrated for last couple of days that I failed to see the result :) – Lenin Oct 17 '20 at 11:10
  • Now, I think, the WCMP vendor dashboard needs their generic approach for adding the custom fields there. I was checking both the product pages from Admin and Vendor Dashboard. This field does not appear at the vendor dashboard. – Lenin Oct 17 '20 at 11:15
  • I'm sorry, I never used the WCMP plugin, but I'll look into it. You should make a new question for that to keep questions separated though. – Tom Oct 17 '20 at 11:18
  • 1
    WCMP solution seemed easier. They have their hardcoded php files to override. But, I thought I'd use the WooCommerce hooks. But, to my bad luck it either worked and I didn't notice due to collapsed section at admin panel and I mostly checked at WCMP panel. Seeing your result, I opened everything to check if there's already an output. Been two frustrated days. I am sure I'd be able to accomplish the WCMP part without posting here at all. – Lenin Oct 17 '20 at 11:24