Skip to content

Extra consignments created in php sample code #137

@Gribanov

Description

@Gribanov

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;
    }
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions