How to create a product via SQL in Woocommerce

Asked

Viewed 426 times

-1

I am creating a snippet to add a product in wordpress. But I realized that it is not enough to insert the product in the wp_posts table. The query works, the item is inserted in the database, but the product is not displayed in the product screen.

    <?php
$connect = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql     = "INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (49, 1, '2019-07-31 11:15:10', '2019-07-31 11:15:10', 'texto_sql_diff', 'novidadediff', 'texto_sql_desc', 'publish', 'open', 'closed', '', 'novidadediff', '', '', '2019-07-31 11:15:14', '2019-07-31 11:15:14', '', 0, 'http://duffplugin.atwebpages.com/?post_type=product&#038;p=48', 0, 'product', '', 0)";
if ($connect->query($sql) === TRUE) {
    echo "<script>console.log('tudo OK!)</script>";
} else {
    echo "Error " . $sql . ' ' . $connect->connect_error;
}
$connect->close();
?>

1 answer

0


Wordpress is a CMS platform created initially for use in blogs. With the support of plugins, it was possible to add features such as Woocommerce.

The fields initially provided in the table wp_posts are insufficient for what the WC proposes, so it makes use (like most plugins) of the table wp_postmeta.

The fields that are recorded in wp_postmeta depends on what functionality of Woocommerce you use and if there are auxiliary plugins.

For this, check some product that has been registered by the WC. For this you can use:

SELECT id, post_title FROM wp_posts WHERE post_type = "product"; 

From the returned records, choose an ID and run the following query:

SELECT * FROM wp_postmeta WHERE post_id = {id_escolhido};

This way you will get what are the goals added to the database and can add the records you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.