Skip to content

$Cart->conditions('type', true) does not return item-level conditions #53

@travisulrich

Description

@travisulrich

$cart->conditions(null) includes items, but $cart->conditions(‘type’) does not.

TestCase:

    public function test_item_level_conditions_are_returned()
    {
        $product = FactoryMuffin::create('Acme\Models\Product', ['title'=>'A New Product']);
        $coupon = $this->createCondition('Test Coupon', 'coupon', '5', 'subtotal');
        $discount = $this->createCondition('Test Discount', 'discount', '10%', 'subtotal');
        $this->cart->add($this->createItemFromProduct($product, 1, 10, [$coupon, $discount]));
        $this->assertEquals(2, count($this->cart->conditions()));

        //The following assertion fails with: Failed asserting that 0 matches expected 1.
        $this->assertEquals(1, count($this->cart->conditions('coupon')));
    }

This test passes after updating the $cart->conditions() method to the following:

    public function conditions($type = null, $includeItems = true)
    {
        $conditions = [];

        if ( ! $type) {
            if ($includeItems) {
                foreach ($this->items as $item) {
                    $conditions = array_merge($conditions, $item->conditions());
                }
            }

            return array_merge($conditions, $this->conditions);
        }

        if ($includeItems) {
            foreach ($this->items as $item) {
                $conditions = array_merge($conditions, $item->conditions($type));
            }
        }
        foreach ($this->conditions as $condition) {
            if ($condition->get('type') === $type) {
                $conditions[] = $condition;
            }
        }

        return $conditions;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions