0

I've simple product assign in bundle items of Bundle product.

Now I've simple product ID and I'm trying to find in which bundle product it's given in bundle items using following code but it returns an empty array.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$parent = $objectManager->create('Magento\Bundle\Model\Product\Type')->getParentIdsByChild($product->getId());
var_dump($parent);
2

2 Answers 2

1

I've done in following way

public function getParentsId(){
        $products =  $this->_productCollection->create();
        $productcollection =$products->addAttributeToFilter([
                                ['attribute'=>'event_start_date','gt'=> date("Y/m/d") ]
                            ]);
        $parents = array();
        foreach ($productcollection as $product) {
            $parent = $this->findBundledProductsWithThisChildProduct($product->getId());
            array_push($parents, $parent);
        }
        return array_unique(array_reduce($parents, 'array_merge', array()));
    }


//Function by Alan Strom
function findBundledProductsWithThisChildProduct($id_to_find)
    {
        //grab all bundled products
        $bundles = array();
        $products = $this->_productCollection->create();
        $products = $products->addFieldToFilter('type_id','bundle');
        //loop over bundled products
        foreach($products as $product)
        {
            //get child product IDs
            $children_ids_by_option = $product
            ->getTypeInstance(true)
            ->getChildrenIds($product->getId(),false); //second boolean "true" will return only
                                                       //required child products instead of all
            //flatten arrays (which are grouped by option)
            $ids = array();
            foreach($children_ids_by_option as $array)
            {
                $ids = array_merge($ids, $array);
            }

            //perform test and add to return value
            if(in_array($id_to_find, $ids))
            {
              $bundles[] = $product->getId();
            }
        }
        return $bundles;
    }
0

Get Parent Product from Child Id.

protected $_productloader;
protected $configurable;
protected $grouped;
protected $bundleSelection;

public function __construct(
    ...........................................................
    \Magento\Catalog\Model\ProductFactory $_productloader,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
    \Magento\GroupedProduct\Model\Product\Type\Grouped $grouped,
    \Magento\Bundle\Model\Product\Type $bundleSelection,
    ...........................................................
) {
    ...........................................................
    $this->_productloader = $_productloader;
    $this->configurable = $configurable;
    $this->grouped = $grouped;
    $this->bundleSelection = $bundleSelection;
    ...........................................................
}

...........................................................
...........................................................
...........................................................

public function getParentProductData($productData)
{
    $prdData = '';
    $parentProductId = $this->getParentProductId($productData->getEntityId());
    if ($parentProductId) {
        $prdData = $this->_productloader->create()->load($parentProductId);
    } else {
        $prdData = $this->_productloader->create()->load($productData->getEntityId());
    }
    // parent Product Data
    return $prdData;
}

public function getParentProductId($childId)
{
    $parent_id = 0;
    // configurable product
    $product = $this->configurable->getParentIdsByChild($childId);
    if (isset($product[0])) {
        $parent_id = $product[0];
        return $parent_id;
    }
    // group product
    $groupIds = $this->grouped->getParentIdsByChild($childId);
    if (isset($groupIds[0])) {
        $parent_id = $groupIds[0];
        return $parent_id;
    }
    // bundel product
    $budel_id = $this->bundleSelection->getParentIdsByChild($childId);
    if (isset($budel_id[0])) {
        $parent_id = $budel_id[0];
        return $parent_id;
    }
    return $parent_id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.