PHP code example of level-level / wp-browser-woocommerce

1. Go to this page and download the library: Download level-level/wp-browser-woocommerce library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

level-level / wp-browser-woocommerce example snippets


 // ./tests/wpunit/ExampleTest.php

use LevelLevel\WPBrowserWooCommerce\WCTestCase;

class ExampleTest extends WCTestCase{
    public function test_something(){
        // Create a WooCommerce product.
        $product = $this->factory()->product->create_and_get(
			array(
				'name'          => 'test',
				'regular_price' => '12.12',
			)
		);

        // Create a WooCommerce order with two products.
		$order   = $this->factory()->order->create_and_get(
			array(
				'payment_method'       => 'bacs',
				'payment_method_title' => 'BACS',
				'set_paid'             => true,
				'line_items'           => array(
					array(
						'product_id' => $product->get_id(),
						'quantity'   => 2,
					),
				),
			)
		);

        // Make sure the order total price is correct.
        $this->assertEquals( 24.24, $order->get_total() );
    }
}

$order = $this->factory()->order->create_and_get(
    array(
        'payment_method'       => 'bacs',
        'payment_method_title' => 'BACS',
        'set_paid'             => true,
        'billing'              => array(
            'first_name'   => 'John',
            'last_name'    => 'Doe',
            'address_1'    => 'Market',
            'house_number' => '1',
            'address_2'    => '',
            'city'         => 'Rotterdam',
            'postcode'     => '3456AB',
            'country'      => 'NL',
            'email'        => '[email protected]',
        ),
        'shipping'             => array(
            'first_name'   => 'John',
            'last_name'    => 'Doe',
            'address_1'    => 'Memory Lane',
            'house_number' => '1',
            'address_2'    => '',
            'city'         => 'Rotterdam',
            'postcode'     => '3456AB',
            'country'      => 'NL',
        ),
        'line_items'           => array(
            array(
                'product_id' => 1,
                'quantity'   => 1,
                'meta_data'  => array(
                    array(
                        'key'   => 'made_by',
                        'value' => 'Level Level',
                    ),
                    array(
                        'key'   => 'with_love',
                        'value' => 'obviously'
                    ),
                ),
            ),
        ),
        'shipping_lines': array(
            array(
                'method_id': 'flat_rate',
                'method_title': 'Flat Rate',
                'total': '10.00'
            )
        )
    )
);

$this->factory()->product->create_and_get(
    array(
        'name'            => 'test',
        'regular_price'   => '103.11',
        'weight'          => '14',
        'dimensions'      => array(
            'height' => '1',
        ),
        'reviews_allowed' => false,
        'manage_stock'    => true,
        'stock_status'    => 'onbackorder',
        'backorders'      => 'yes',
        'meta_data'       => array(
            array(
                'key'   => 'made_in',
                'value' => 'Rotterdam',
            ),
        ),
    )
);

$this->factory()->tax_rate->create_and_get(
    array(
        'country'=>'NL',
        'rate'=> '21',
        'name'=>'BTW hoog tarief',
        'shipping'=>false,
    )
);

$this->factory()->coupon->create_and_get(
    array(
        'code'               => '25off',
        'discount_type'      => 'percent',
        'amount'             => '10',
        'individual_use'     => true,
        'exclude_sale_items' => true,
        'minimum_amount'     => '100.00',
    )
);

$this->factory()->shipping_zone->create_and_get(
    array(
        'name' => 'Global'
    )
);

$this->factory()->shipping_zone->set_zone_locations(
    1, array(
        array(
            'code' => '3024*',
            'type' => 'postcode',
        ),
    )
);

$this->factory()->shipping_zone_method->zone_id(5)->create_and_get(
    array(
        'method_id' => 'flat_rate',
        'settings'  => array(
            'cost'=> '20.00',
        ),
    ),
);

$this->factory()->subscription->create_and_get(
    array(
        'customer_id' => 1,
        'parent_id' => 1,
        'status' => 'pending',
        'billing_period' => 'month',
        'billing_interval' => 1,
        'start_date' => ( new DateTime( 'now', wp_timezone() ) )->format( 'Y-m-d H:i:s' ),
        'next_payment_date' => ( new DateTime( '+1 month', wp_timezone() ) )->format( 'Y-m-d H:i:s' ),
        'payment_method' => '',
        'billing' => array(
            'first_name' => 'John',
            'last_name' => 'Doe',
            'address_1' => 'Market',
            'house_number' => '1',
            'address_2' => '',
            'city' => 'Rotterdam',
            'postcode' => '3456AB',
            'country' => 'NL',
            'email' => '[email protected]',
        ),
        'shipping' => array(
            'first_name' => 'John',
            'last_name' => 'Doe',
            'address_1' => 'Memory Lane',
            'house_number' => '1',
            'address_2' => '',
            'city' => 'Rotterdam',
            'postcode' => '3456AB',
            'country' => 'NL',
        ),
        'line_items' => array(
            array(
                'product_id' => 1,
                'quantity' => 1,
                'subtotal' => '10.00',
                'total' => '10.00',
            )
        )
    )
);

public function test_can_add_sample_to_cart() {
    WC_AJAX::init();

    $product = $this->factory()->product->create_and_get(
        array(
            'name'          => 'test',
            'regular_price' => '12.12',
        )
    );
    
    // ... testing logic ...
    
    try {
        $this->_handleAjax( 'woocommerce_add_to_cart' );
    } catch ( WPAjaxDieContinueException $e ) {
        ob_end_flush();
    }
    $this->assertEmpty( wc_get_notices( 'error' ), 'There should be no error notices after making this ajax call.' );
}