Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra consignments created in php sample code #137

Open
Gribanov opened this issue Feb 19, 2025 · 0 comments
Open

Extra consignments created in php sample code #137

Gribanov opened this issue Feb 19, 2025 · 0 comments

Comments

@Gribanov
Copy link

I'm using code sample from docs to create consignment and return consignments.
When there are more than 1 consigmnet in $yourShipments this code creates extra return consignments because generateReturnConsignments() method is called in loop and on second iteration it will handle regular consignments and 1 return consignment created on 1st iteration and so on.
Most probably generateReturnConsignments() should be called once after the loop.

// Create the collection before the loop
$consignments = new MyParcelCollection();

// Loop through your shipments, adding each to the same MyParcelCollection
foreach ($yourShipments as $yourShipment) {
    $consignment = ((ConsignmentFactory::createByCarrierId(PostNLConsignment::CARRIER_ID))
        ->setApiKey('api_key_from_backoffice')
        ->setCountry($yourShipment['cc'])
        ->setPerson($yourShipment['person'])
        ->setCompany($yourShipment['company'])
        ->setFullStreet($yourShipment['full_street_input'])
        ->setPostalCode($yourShipment['postal_code'])
        ->setCity($yourShipment['city'])
        ->setLabelDescription($yourShipment['label_description'])
    );

    // Add the consignment to the collection
    $consignments
        ->addConsignment($consignment);
}
// Generate the return consignment
// When there are no options set, the options from the parent consignment are used
$consignments
->generateReturnConsignments(
    false,
    function (
        AbstractConsignment $returnConsignment,
        AbstractConsignment $parent
    ): AbstractConsignment {
        $returnConsignment->setLabelDescription(
            'Return: ' . $parent->getLabelDescription() .
            ' This label is valid until: ' . date("d-m-Y", strtotime("+ 28 days"))
        );
        $returnConsignment->setSignature(true);

        return $returnConsignment;
    }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant