0

Suppose I added one bundle product in cart.When I call \carts\mine\items,it show respnse of bundle options

"extension_attributes": {
        "bundle_options": [{
            "option_id": 643,
            "option_qty": 1,
            "option_selections": [704]
            }, {
            "option_id": 644,
            "option_qty": 1,
            "option_selections": [705]
            }, {
            "option_id": 645,
            "option_qty": 1,
            "option_selections": [706]
            }]
        }

Can I get sku of bundle option also?

Please suggest me.

  • I've done a lot with bundles in 2.1 but I need a bit more to go on.. What are you trying to do, are you looking for the info in js or PHP, etc. Please describe your question more extensively. – Rian May 30 '18 at 11:32
  • I want to sku in response of /carts/mines/items api . – Lovely Setia May 30 '18 at 12:35
  • tell me anyone.................... – Lovely Setia May 31 '18 at 8:15
  • writing as we speak – Rian May 31 '18 at 8:28
1

Alright, so first things first. We need to find where things are happening. I searched the vendor/magento folder for carts/mine/items and that came up with module-quote/etc/webapi.xml. Which shows that Magento\Quote\Api\CartItemRepositoryInterface and getList is what is used for the output.

The interface we found obviously doesn't return anything, but Magento tends to preference them somewhere with a class, so a quick search turned up module-quote/etc/di.xml which has <preference for="Magento\Quote\Api\CartItemRepositoryInterface" type="Magento\Quote\Model\Quote\Item\Repository" /> in it.

And now we're talking! This Reposity class implements the getList. Though it doesn't actually show us anything useful in regards to the options for bundles. However, that method does show us this: $item = $this->getCartItemOptionsProcessor()->addProductOptions($item->getProductType(), $item);.

From that it's reasonable to deduct that there's a product type specific options processor. Our type is a bundle, so we take a look in module-bundle and there it is! Model/CartItemProcessor.php.

In that class there's a processOptions method which seems to be the place where we want to work.

So, we create a fresh module of our own and create a Plugin:

<type name="Magento\Bundle\Model\CartItemProcessor">
    <plugin name="my_module_sku_in_bundle_cart_output"
            type="My\Module\Plugin\BundleCartItemProcessor" sortOrder="1"/>
</type>

And then in our Plugin file (My\Module\Plugin\BundleCartItemProcessor):

class BundleCartItemProcessor
{
    public function afterprocessOptions($subject, $result)
    {
        /**
         * @var Magento\Quote\Api\Data\CartItemInterface $result
         * @var \Magento\Bundle\Model\CartItemProcessor $subject
         */

        $extensionAttributes = $result->getProductOption()->getExtensionAttributes();

        // here is where you go through the result of extensionAttributes
        // and load up the bundle selectionsCollection
        // which you can then use to find the associated
        // product and it's sku. You can then add that
        // sku to the result.

        return $result;
    }
}

If I have time later I might implement the actual solution, but this should get you there, I think.

  • Please provide me implemented solution @Rian – Lovely Setia Nov 27 '18 at 6:09

Your Answer

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

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