A wp-eval.php file allows you to execute WordPress functions outside the normal theme structure. This is useful for testing WooCommerce functionality.

Steps:

  1. Navigate to your theme folder (wp-content/themes/your-theme/).
  2. Create a new file named wp-eval.php.
  3. Add the following code:

<?php
require_once( dirname(__FILE__) . '/../../../wp-load.php' );

echo "<pre>";
echo "Testing WordPress Evaluation File...\n";

// Fetch site URL
echo "Site URL: " . get_site_url() . "\n";

// Fetch total products
echo "Total Products: " . wp_count_posts('product')->publish . "\n";

echo "</pre>";
?>
  1. Open the file in your browser: https://your-site.com/wp-content/themes/your-theme/wp-eval.php

Step 2: Programmatically Create a Test Product

To create a test product in WooCommerce, use the following code:

<pre><code class="language-php">
<?php
require_once( dirname(__FILE__) . '/../../../wp-load.php' );

$test_product = array(
    'post_title'    => 'Test Product',
    'post_content'  => 'This is a test product.',
    'post_status'   => 'publish',
    'post_type'     => 'product',
);

$product_id = wp_insert_post( $test_product );

if ($product_id) {
    update_post_meta( $product_id, '_price', '9.99' );
    update_post_meta( $product_id, '_regular_price', '9.99' );
    echo "Test product created with ID: $product_id";
} else {
    echo "Failed to create test product.";
}
?>
</code></pre>

Step 3: Simulating a Test Purchase

To programmatically create an order:

<pre><code class="language-php">
<?php
require_once( dirname(__FILE__) . '/../../../wp-load.php' );

$customer_id = get_current_user_id();
$order = wc_create_order(array('customer_id' => $customer_id));
$product_id = 123; // Replace with your test product ID

$order->add_product(wc_get_product($product_id), 1);
$order->set_address(array(
    'first_name' => 'John',
    'last_name'  => 'Doe',
    'email'      => 'john.doe@example.com',
    'address_1'  => '123 Main St',
    'city'       => 'Test City',
    'postcode'   => '12345',
    'country'    => 'US'
), 'billing');

$order->calculate_totals();
$order->update_status('completed');

echo "Test order placed: " . $order->get_id();
?>
</code></pre>

Step 4: Styling the Output like a Blog Post

If you want to display test results in a styled manner, you can wrap the output in HTML and apply CSS. Example:

<pre><code class="language-html">
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test WooCommerce Purchase</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: auto;
            padding: 20px;
            line-height: 1.6;
        }
        pre {
            background: #f4f4f4;
            padding: 10px;
            border-radius: 5px;
        }
        h1, h2 {
            color: #333;
        }
    </style>
</head>
<body>
    <h1>WooCommerce Test Purchase</h1>
    <h2>Results:</h2>
    <pre><code class="language-php">// Results will be displayed here</code></pre>
</body>
</html>
</code></pre>

Now you can run your test product purchase and display the results in a nicely formatted blog post! 🚀