admin管理员组

文章数量:1295939

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.

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.

Share Improve this question edited Apr 15, 2019 at 21:13 LoicTheAztec 3,39117 silver badges24 bronze badges asked Apr 15, 2019 at 9:00 WalnefWalnef 111 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 any available setter method to change product properties values and you will use at the end save(); method to save data and refresh cached data.

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.

本文标签: phpGet WooCommerce product details and transfer them to a custom DB table