0

I have two different types of products.

Product type B is a simple product with a set of defined subscription plans per context. Product type A is a product with option containing one or many products of type B.k

At the moment, if I save a B product. Then add it to my A product. It works fine. But if after that I go again to my B product and add another subscription plan in the context. Then it looks like, product B is properly saved but my product A doesn't take the information that my product B has a new subscription plan in his context.

I'm trying to make this issue as general as I can otherwise there are too many specific codes.

My question is When I save the product type B, how can I update the offer configurations of the A product to get the extra subscriptions plans added in the product of type B ? Considering I'm already retrieving the ids of all the products of type B containing the saved product.

This is the point I reach in my save method (If condition on the id 1093 is only there to simplify the problem by limiting to the product I'm looking, you can ignore it).

    $currentProduct = $this->productRepository->get($this->getRequest()->getParam('product')['sku']);
    $currentProductId = $currentProduct->getId();
    $searchFilter = $this->searchCriteriaBuilder
        ->addFilter('type_id', Classique::TYPE)
        ->create();
    $productsClassique =  $this->productRepository->getList($searchFilter)->getItems();

    foreach ($productsClassique as $productClassique) {
        if($productClassique->getId() == "1093"){
            $bundleProductOptions = $productClassique->getExtensionAttributes()->getBundleProductOptions();
            foreach ($bundleProductOptions as $option){
                $linksIdsInOption = [];
                $productLinksInOption = $option->getProductLinks();
                foreach ($productLinksInOption as $link){
                    $linksIdsInOption[] = $link->getId();
                }
                $bundleSelections = $this->getBundleOptionSelections($option);
                foreach ($bundleSelections as $bundleSelection) {
                    $id = $bundleSelection->getProductId();
                    if($id == $currentProductId){
                        $sku = $currentProduct->getSku();
                        $subPrice = $currentProduct->getExtensionAttributes()->getSubscriptionPrice();

                        //TODO find how to update the productClassique with the new subscription plan from the current product

If it might help here is what I'm doing to print the subscription plan of the product of type A

/**
 * @param OfferConfigurationInterface $configuration
 * @param ProductInterface $product
 * @param $formulas
 * @param array $parameters*
 */
public function getColumnsJson($configuration, $product, $formulas, $durations, $metadata, $parameters = [])
{
    $columns = [];
    if($product->getExtensionAttributes()->getBundleProductOptions())
    {
        $isOfferClassique = $product->getTypeId() == Classique::TYPE;
        $classiqueProductFormulaLabels = [];
        $firstOption = true;

        $durationSortedIds = [];
        if($isOfferClassique)
        {
            foreach($durations as $duration)
            {
                $durationSortedIds[] = $duration->getClassicSortOrder();
            }
        }

        foreach($product->getExtensionAttributes()->getBundleProductOptions() as $bundleProductOption)
        {
            /* @var $bundleProductOption \Magento\Bundle\Model\Option $bundleOption */
            $bundleSelections = $this->getBundleOptionSelections($bundleProductOption);
            $bundleSelections = $this->reorderBundleSelections($bundleSelections);
            foreach($formulas as $formulaCounter => $formula)
            {
                if($firstOption)
                {
                    foreach($bundleSelections as $bundleSelectionCounter => $bundleSelection)
                    {
                        if($formulaCounter == $bundleSelectionCounter)
                        {
                            $classiqueProductFormulaLabels[$bundleSelection['product_id']] = ['label' => $formula->getLabel(), 'id' => false];

                            $fid = $formula->getRowId();
                            if($fid && strpos($fid, '|') !== false)
                            {
                                $fid = explode('|', $fid);
                                if(count($fid) == 2 && $fid[0] == $product->getId())
                                {
                                    $classiqueProductFormulaLabels[$bundleSelection['product_id']]['id'] = $fid[1];
                                }
                            }
                        }
                    }
                }
            }

            $contextIdentifiers = [];
            foreach($bundleSelections as $bundleSelection) {
                foreach ($metadata['subscription_modes_per_product'][$bundleSelection->getProductId()] as $k => $subscriptionPlanId) {
                    $contextIdentifiers[] = ['context_id' => $subscriptionPlanId, 'product_id' => $bundleSelection->getProductId()];
                }
            }

            foreach($bundleSelections as $bundleSelection)
            {
                $linkedProduct = $this->getProductById($bundleSelection->getProductId());
                $linkedProductSku = $linkedProduct->getSku();
                $typeInstance = $this->typeHelper->getProductTypeInstance($linkedProduct);
                if($firstOption)
                {
                    if($isOfferClassique)
                    {
                        if(isset($classiqueProductFormulaLabels[$linkedProduct->getId()]))
                        {
                            $productConfiguration['product_formula_name'] = $classiqueProductFormulaLabels[$linkedProduct->getId()]['label'];
                            $productConfiguration['product_formula_id'] = $classiqueProductFormulaLabels[$linkedProduct->getId()]['id'];
                        }
                        else
                        {
                            $productConfiguration['product_formula_name'] = 'Formula #'.$linkedProduct->getId();
                            $productConfiguration['product_formula_id'] = false;
                        }
                    }
                }
                if($typeInstance instanceof BcAbonnementInterface)
                {
                    if($isOfferClassique)
                    {
                        if(isset($metadata['subscription_modes_per_product'][$bundleSelection->getProductId()]))
                        {
                            if(isset($classiqueProductFormulaLabels[$linkedProduct->getId()]))
                            {
                                if($firstOption)
                                {
                                    $productsData = $metadata['products_data'];
                                    foreach ($productsData as $currentSku => $productData) {
                                        if($currentSku == $linkedProductSku){
                                            foreach ($productData as $productDataWebsite) {
                                                foreach ($productDataWebsite as $productDataWebsiteCustomerGroup) {
                                                    foreach ($productDataWebsiteCustomerGroup as $iContext => $context) {
                                                        foreach ($context as $storedContextIdentifier => $contextValue) {
                                                            $identifier = $this->findValue($contextIdentifiers,$storedContextIdentifier,$bundleSelection->getProductId());
                                                            if($iContext == $configuration->getContextId() && $identifier != -1){
                                                                $formulaId = $classiqueProductFormulaLabels[$linkedProduct->getId()]['id'];
                                                                $columns[] = [
                                                                    'formula_id' => $formulaId, 'duration_id' => $identifier, 'product_id' =>$bundleSelection->getProductId()
                                                                ];
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $firstOption = false;
        }
    }
    return $columns;
}

Thanks for the assistance. Please let me know if there is some piece of information I can provide to bring some more light to the issue.

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.