• I am writing a plugin that lets users add a tip on the cart page. I use two filters to do so: woocommerce_add_cart_item & woocommerce_add_cart_item. Both use the same callback:

    
        public function setTipPrice($cart_item_data)
        {
            if ($cart_item_data['product_id'] == App::tipId() && array_key_exists('tip_size', $cart_item_data)) {
                $cart_item_data['data']->set_price($cart_item_data['tip_size']);
            }
    
            return $cart_item_data;
        }
    

    This works as expected in the browser, but fails in PHPUnit. Is there something I have to call explicitly to fix this? The price of the tip is always 0.

    Here’s the test:

    
        //  This test fails, but works in the browser.
    
        /** @test */
        public function it_can_have_a_custom_price_when_adding_to_the_cart()
        {
            App::install(); // creates the tip product
            
            WC()->cart->add_to_cart(App::tipId(), 1, 0, [], ['tip_size' => 10.00]);
    
            $this->assertEquals(10.00, WC()->cart->get_total_ex_tax());
        }
    

    Any pointers?

  • The topic ‘Dynamic pricing, cart totals in PHPUnit’ is closed to new replies.