Home » Php » wordpress – Delete a product by ID using PHP in WooCommerce

wordpress – Delete a product by ID using PHP in WooCommerce

Posted by: admin July 12, 2020 Leave a comment

Questions:

Since there is a command:

wp_insert_post()

shouldn’t there be a command:

wp_delete_post()

Seems like it does not exist, what is an alternative that you use when you have the ID of a product in the database and you want to delete it?

How to&Answers:

WooCommerce has a methods to delete a product via API, so I have built a method from that function which can delete a product easily.

/**
 * Method to delete Woo Product
 * 
 * @param int $id the product ID.
 * @param bool $force true to permanently delete product, false to move to trash.
 * @return \WP_Error|boolean
 */
function wh_deleteProduct($id, $force = FALSE)
{
    $product = wc_get_product($id);

    if(empty($product))
        return new WP_Error(999, sprintf(__('No %s is associated with #%d', 'woocommerce'), 'product', $id));

    // If we're forcing, then delete permanently.
    if ($force)
    {
        if ($product->is_type('variable'))
        {
            foreach ($product->get_children() as $child_id)
            {
                $child = wc_get_product($child_id);
                $child->delete(true);
            }
        }
        elseif ($product->is_type('grouped'))
        {
            foreach ($product->get_children() as $child_id)
            {
                $child = wc_get_product($child_id);
                $child->set_parent_id(0);
                $child->save();
            }
        }

        $product->delete(true);
        $result = $product->get_id() > 0 ? false : true;
    }
    else
    {
        $product->delete();
        $result = 'trash' === $product->get_status();
    }

    if (!$result)
    {
        return new WP_Error(999, sprintf(__('This %s cannot be deleted', 'woocommerce'), 'product'));
    }

    // Delete parent product transients.
    if ($parent_id = wp_get_post_parent_id($id))
    {
        wc_delete_product_transients($parent_id);
    }
    return true;
}

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.

USAGE

wh_deleteProduct(170); //to trash a product
wh_deleteProduct(170, TRUE); //to permanently delete a product

Code is tested and works.

Hope this helps!

Answer:

becuase every thing in the wordpress is post so the product is also a post and according to Word press Codex this order is exist you can pass the post id to the

wp_delete_post((int)ID) as integer value 

Answer:

You can use the WC API PHP Library
https://github.com/woocommerce/wc-api-php

Setup

First from Woocommerce > settings > API and add a new key
https://docs.woocommerce.com/document/woocommerce-rest-api/

And add WP REST API integration

require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/Client.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/BasicAuth.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/HttpClient.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/HttpClientException.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/OAuth.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/Options.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/Request.php');
require_once( __DIR__ . '/wc-api-php-master/src/WooCommerce/HttpClient/Response.php');

use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;


    $woocommerce = new Client(
        home_url(), 
        'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', //Consumer Key
        'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', //Consumer Secret
        [
            'wp_api' => true,
            'version' => 'wc/v2',
        ]
    );

Delete Product

And use this line to delete product

<?php  
$PRODUCT_ID = 40;
print_r($woocommerce->delete('products/'.$PRODUCT_ID, ['force' => true])); 
?>

Also, you can use Batch update products to delete the product.

You must read the documentation
https://woocommerce.github.io/woocommerce-rest-api-docs

Answer:

In WordPress, The product is also the post and it is stored also in the post ad post_meta table so you can use the following code for that.

$product_id = 108;

wp_delete_post( $product_id );