Skip to content

Update shipment collection to unserialize packages attribute after load #17679

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

Merged
merged 1 commit into from
Sep 24, 2018
Merged

Update shipment collection to unserialize packages attribute after load #17679

merged 1 commit into from
Sep 24, 2018

Conversation

dnsv
Copy link

@dnsv dnsv commented Aug 18, 2018

Description

The column packages in sales_shipment is in its resource model \Magento\Sales\Model\ResourceModel\Order\Shipment characterized as a serializable field. But packages doesn't get unserialized when shipments are loaded with a collection. This pull request fixes the issue by unserializing the field after the collection is loaded.

Manual testing scenarios

  1. Create a shipment through the backend. packages is in my case set to [].
  2. Load this shipment with a collection. The attribute packages is a string, but should be an array.
class Shipment
{
    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory
     */
    protected $shipmentCollectionFactory;

    /**
     * @var \Magento\Sales\Api\ShipmentRepositoryInterface
     */
    protected $shipmentRepository;

    public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory $shipmentCollectionFactory,
        \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
    ) {
        $this->shipmentCollectionFactory = $shipmentCollectionFactory;
        $this->shipmentRepository = $shipmentRepository;
    }

    public function execute()
    {
        /** @var \Magento\Sales\Model\Order\Shipment $shipment */
        $shipment = $this->shipmentRepository->get(1);
        echo gettype($shipment->getPackages()) . "\n";

        /** @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Collection $shipmentCollection */
        $shipmentCollection = $this->shipmentCollectionFactory->create();
        $shipment = $shipmentCollection->addAttributeToFilter('entity_id', 1)
            ->getFirstItem();

        echo gettype($shipment->getPackages());
    }
}

In this example the same shipment is loaded with 1.) the shipment repository and 2.) the shipment collection. The output is:

array
string

This consequently causes further problems if the shipment loaded with the shipment collection is being updated, because packages gets saved as "[]" (the quotation marks are now a part of the string). packages now can't get unserialized when it's loaded with any method. Also an error is shown if this shipment is opened in the backend because an array is expected but a string is given:

Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /path/to/magento2/app/code/Magento/Shipping/view/adminhtml/templates/order/packaging/packed.phtml on line 12

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @dnsv. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me {$VERSION} instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@dnsv dnsv changed the title Update shipment collection to unserialize packages attribute after load Update shipment collection to unserialize packages attribute after load Aug 18, 2018
@magento-engcom-team magento-engcom-team added this to the Release: 2.2.7 milestone Sep 18, 2018
@magento-engcom-team
Copy link
Contributor

@dnsv thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

@magento-engcom-team magento-engcom-team merged commit 8f4efee into magento:2.2-develop Sep 24, 2018
magento-engcom-team pushed a commit that referenced this pull request Sep 24, 2018
@magento-engcom-team
Copy link
Contributor

Hi @dnsv. Thank you for your contribution.
We will aim to release these changes as part of 2.2.8.
Please check the release notes for final confirmation.

Please, consider to port this solution to 2.3 release line.
You may use Porting tool to port commits automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants