Sunday, October 16, 2022
HomeWordPress Developmentplugin growth - PHPUnit and Woocommerce

plugin growth – PHPUnit and Woocommerce


I am making a plugin that ought to make it possible for the consumer can have just one product at time within the cart so if a consumer add a product the plugin ought to empty it earlier than including.

My plugin use woocommerce_add_to_cart_validation filter and that is the perform:

/**
     * Empty cart earlier than add new merchandise
     *
     * @param blended $cart_item_data Cart merchandise information.
     */
    public perform woo_custom_add_to_cart_before($cart_item_data)
    {
        international $woocommerce;

        $woocommerce->cart->empty_cart();

        return true;
    }

I wish to make it possible for this works with a phpunit check:
NB: the check is just not accomplished but, I am attempting to make use of WC perform earlier than full it

public perform test_only_one_product_in_cart()
{
    activate_plugin('woocommerce/woocommerce.php');
    require_once WP_PLUGIN_DIR . '/woocommerce/woocommerce.php';

    $product_id = $this->factory->post->create(
        array(
            'post_type' => 'product'
        )
    );
    WC()->cart->add_to_cart($product_id);

    $cart_content = WC()->cart->get_cart_contents_count();

    $this->assertEquals(1, $cart_content);
}

If I run the check it says:

Error: Name to a member perform add_to_cart() on null

How can I exploit Woocommerce perform in a unit check?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments