2

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.