1

I recently migrated from M1 to M2 and when I am attempting to retrieve a product's Parent ID I am getting a zero value on some products but on others I'm getting the correct parent IDs. It's definitely a code issue, as my store displays all of the simple products correctly based on my configurations as expected.

After doing some research I found that the products affected match the exact same products that belong to configurable products with a value other than "1" in the "created_in" column. I presume "created_in" refers to the store ID? I can't find any documentation on that.

Question is - will changing the 'created_in' value to '1' solve the problem or does that seem as far fetched as I think it does?

Here is the code I'm using - we're running EE so using 'row_id' as the $productIDLink:

        protected function getFirstParentProductId($product)
        {
            // Check is Magento EE >=2.1, if so use different catalog_product_entity link field name (row_id in EE 2.1)
            if ($this->utilsHelper->isMagentoEnterprise() && $this->utilsHelper->mageVersionCompare($this->productMetadata->getVersion(), '2.1.0', '>=')) {
                $productIdLink = 'row_id';
            } else {
                $productIdLink = 'entity_id';
            }

            $parentId = null;
            #if ($product->getTypeId() == 'simple') {
            $parentIds = $this->productTypeGrouped->getParentIdsByChild($product->getId());
            if (!$parentIds) {
                $parentIds = $this->productTypeConfigurable->getParentIdsByChild($product->getId());
            }
            foreach ($parentIds as $possibleParentId) {
                // Check if parent product exists, if yes return first existing parent product
                $readAdapter = $this->resourceConnection->getConnection();
                $select = $readAdapter->select()
                    ->from($this->resourceConnection->getTableName('catalog_product_entity'), ['entity_id'])
                    ->where($productIdLink . " = ?", $possibleParentId);
                $possibleParentId = $readAdapter->fetchOne($select);
                if ($possibleParentId) {
                    $parentId = $possibleParentId;
                }
            }
            #}

            return (int)$parentId;
        }

catalog_product_entity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.