By Walnef

2019-04-15 09:00:38 8 Comments

I'm currently doing a registering service for products with WordPress and WooCommerce.

All products that customers can register will be in the WooCommerce database, but not visible on frontend.

Here is my goal: make a form with a dropdown list (items inside it will be all my WooCommerce products), where user can pick his product. He will then write when he bought his product, name, address, etc.

The path will be : get all information I want from product the database of WooCommerce before using it later -> get personal info written inside form on frontend -> write all that data inside a table in MySQL.

I tried with wc_get_products(); but I do not know how to get specific information (name, URL of photo, SKU, and some specific information like warranty period), it returns some weird array.

I already mapped my table for MySQL. I just need to know how to get info and write to it.

Any help is welcome.


@LoicTheAztec 2019-04-15 17:22:08

With wc_get_products() you will get an array of WC_Product Objects.

Get product properties

Each WC_Product Object is a CRUD object with protected data. That data is accessible using related WC_Product and WC_Data methods.

For custom meta data (or custom fields), you will use the WC_Data get_meta() method on your specific meta_keys that you can find in wp_postmeta database table for a product ID (post_id). So for example if the custom meta_key is _my_image_url you will get the value from the WC_Product Object using:

$value = $product->get_meta('_my_image_url');

Below is a more complete example, using everything:

// The WC_Product_Query
$products = wc_get_products('limit' => 10);

// Loop though `WC_Product` Objects
foreach ( $products as $product ) {
    $product_id      = $product->get_id(); // The product ID
    $product_name    = $product->get_name(); // The product name
    $product_sku     = $product->get_sku(); // The product SKU
    $product_price   = $product->get_price(); // The product price
    $image_url       = wp_get_attachment_image_src( $product->get_image_id() ); // The product image URL

    ## ----- Custom meta data ----- ##

    $custom_value    = $product->get_meta('_custom_key'); // Custsom meta data

You can also use get_post_meta() WordPress function from the product Id (or the post ID) like:

$product_name    = get_the_title($product_id); // The product name
$product_sku     = get_post_meta($product_id, '_sku', true); // The product SKU
$product_price   = get_post_meta($product_id, '_price', true); // The product price
$image_url       = wp_get_attachment_image_src( get_post_meta($product_id, '_thumbnail_id', true) ); // The product image URL

## ----- Custom meta data ----- ##

$custom_value    = get_post_meta($product_id, '_custom_key', true); // Custsom meta 

Updating product properties

Now you can also use product setter methods to change product properties values and once done you will use at the end $product->save(); to update the data in database.

Transfer to a custom DB table

To transfer some data in a custom table, you will use WPDB WordPress class that allow you to make any SQL query to WordPress custom tables.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to get Woocommerce order product info

  • 2015-03-03 22:14:52
  • geoffs3310
  • 36923 View
  • 7 Score
  • 1 Answer
  • Tags:   php woocommerce

2 Answered Questions

[SOLVED] Retrieve products with specific attribute and in category - woocommerce

  • 2017-07-07 10:08:08
  • Jermaine Hercules
  • 5727 View
  • 1 Score
  • 2 Answer
  • Tags:   wp-query woocommerce

2 Answered Questions

Create an unique ID number after submit form

  • 2018-12-30 10:57:14
  • Juraj
  • 43 View
  • 0 Score
  • 2 Answer
  • Tags:   php database

1 Answered Questions

[SOLVED] Update a user meta key based on WooCommerce product purchase

1 Answered Questions

Get all woocommerce product names with price and show using core php

  • 2018-07-14 14:13:17
  • fahad siddiqui
  • 37 View
  • 0 Score
  • 1 Answer
  • Tags:   php woocommerce

0 Answered Questions

1 Answered Questions

[SOLVED] Sum the total amount of a specific product purchased per user and display in table

0 Answered Questions

How to get product variation SKU?

  • 2018-06-01 11:38:28
  • Agitated_Knowledge
  • 966 View
  • 0 Score
  • 0 Answer
  • Tags:   woocommerce

1 Answered Questions

How do I update product shipping using PHP in WooCommerce?

  • 2015-12-09 20:47:47
  • Krista Lynn Kelly
  • 3842 View
  • -1 Score
  • 1 Answer
  • Tags:   php woocommerce

1 Answered Questions

WooCommerce: Order Details Table > Add Product Meta

Sponsored Content