buildGroupPackage example

public function testPackageDoneWhenSumReached(): void
    {
        $items = new LineItemFlatCollection();
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 2 items, then we have reached 100.0         static::assertCount(2, $packageItems->getItems());
    }

    /** * This test verifies that our packager returns an empty result * if we dont have enough items to fill a group. * * @group lineitemgroup */
    
public function testPackageDoneWhenCountReached(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $packageItems = $this->packager->buildGroupPackage(2, $items$this->context);

        // verify we have only 2 items         static::assertCount(2, $packageItems->getItems());

        // test that we have the first 2 from our list         static::assertEquals($p1->getId()$packageItems->getItems()[0]->getLineItemId());
        static::assertEquals($p2->getId()$packageItems->getItems()[1]->getLineItemId());
    }

    /** * This test verifies, that we do not get any results, if not * enough items exist, to build a package. * * @group lineitemgroup */

    public function testPackageDoneWhenSumReached(): void
    {
        $items = new LineItemFlatCollection();
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 4 items, then we have reached 100.0         static::assertCount(4, $packageItems->getItems());
    }

    /** * This test verifies that our packager returns an empty result * if we dont have enough items to fill a group. * * @group lineitemgroup */
    
// we have to sort our items first             // otherwise it would be a "random" order when             // adjusting the rest of our cart...             $restOfCart = $sorter->sort($restOfCart);

            // try as long as groups can be             // found for the current definition             while (true) {
                $itemsToConsider = $this->ruleMatcher->getMatchingItems($groupDefinition$restOfCart$context);

                // now build a package with our packager                 $group = $packager->buildGroupPackage($groupDefinition->getValue()$itemsToConsider$context);

                // if we have no found items in our group, quit                 if (!$group->hasItems()) {
                    break;
                }

                // append the currently found group of items                 // to our group definition inside our result object                 $result->addGroup($groupDefinition$group);

                // decrease rest of cart items for next search
Home | Imprint | This part of the site doesn't use cookies.