Magento: adding simple products from a package to individual lines in a basket

My client asks that each simple product in the product bundle they sell (outerwear and lower part) be added as a separate item in the basket whenever the user adds it. Can someone guide me on how to do this? I'm pretty good at MVC and the Zend Framework, but I need help finding the exact files that control the addition of packages to the cart, or an alternative method to add these items separately. Please suggest that the only possible product type for this garment is the Bundled product type.

+6
source share
3 answers

You will need an observer:

<checkout_cart_product_add_after> <observers> <reporting> <type>singleton</type> <class>Yourcompany_yourmodelname_Model_Observer</class> <method>onCartProductAdd</method> </reporting> </observers> </checkout_cart_product_add_after> 

Then the observer:

 class ourcompany_yourmodelname_Model_Observer extends Mage_Core_Model_Abstract { /** * Binds to the checkout_cart_product_add_after Event and passes control to the helper function to process the quote * * @param Varien_Event_Observer $observer * @return void */ public function onCartProductAdd($observer){ $product = $observer->getProduct(); $isProductBundle = ($product->getTypeId() == 'bundle'); $items_to_add = array(); $oTypeInstance = $oProduct->getTypeInstance(true); $aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $product ); $aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $product); $bundleOptions = $aOptions->appendSelections($aSelections, true); foreach ($bundleOptions as $bundleOption) { if ($bundleOption->getSelections()) { $bundleSelections = $bundleOption->getSelections(); foreach ($bundleSelections as $bundleSelection) { $items_to_add[] = $bundleSelection.getID(); } } } insertExtractedProducts($items_to_add); } /** * Add extracted products into quote * * @param array $items_to_add */ public function insertExtractedProducts($items_to_add){ /**@var $cart Mage_Checkout_Model_Cart**/ $cart = Mage::helper('checkout/cart')->getCart(); $ids_to_add = array(); foreach($items_to_add as $item_to_be_added){ $ids_to_add[] = $item_to_be_added->getProductId(); } $cart->addProductsByIDs($ids_to_add); $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); } } 

Just a simple example, but it can help.

Related products can be difficult to understand when working with it through code: Here is an example image: enter image description here

Each batch product has one, many options, which at the end will be links to products that will be added to the package in the shopping cart.

Each option consists of one, many options, which will be related products that will fall into the shopping cart for this complete product. One choice, as a rule, can be set by default and is already selected on the product page. More information can be found at this link on how to create and customize related products, because in this document we will discuss only its programming side. The related products display page will look like this: enter image description here

It might look like this in a shopping cart as soon as you click "Add to Cart": Sample bundle

 Sample Shampoo 1 x Moisturiser-125ml $29.95 Default Conditioner 1 x Moisturiser-60g $99.95 

When polling this product through code, you download it like any normal product:

 $oProduct->load($vProductId); 

After downloading it, you need to get an instance of the product type so that you can load the parameters for this product type.

 $oTypeInstance = $oProduct->getTypeInstance(true); 

Now we can get the list of parameter identifiers for this product in this way:

 $oTypeInstance = $oProduct->getTypeInstance(true); 

To poll selections, we add a list of options to the collection, then we get a collection of options, as well as their corresponding selections:

 $aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $oProduct ); $aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $oProduct); $bundleOptions = $aOptions->appendSelections($aSelections, true); 

Now we have a collection of options, and each option will have a set of Selections. Now we can look at the options and see their Selctions.

 foreach ($bundleOptions as $bundleOption) { if ($bundleOption->getSelections()) { $bundleSelections = $bundleOption->getSelections(); foreach ($bundleSelections as $bundleSelection) { // get some data here $vName = $bundleOption->getTitle(); } } } 

To get the list of required products for the supplied product, you can use the following code:

 $requiredChildren = $this->getChildrenIds($product->getId(),$required=true); 

You can then iterate over the array of identifiers and load products by their identifiers to get more information about these products.

Related products in the basket

To scroll through the selected paired product options in the shopping card, you can use something like this:

 /** * @var Mage_Bundle_Model_Product_Type */ $typeInstance = $this->getProduct()->getTypeInstance(true); // get bundle options $optionsQuoteItemOption = $this->getItem()->getOptionByCode('bundle_option_ids'); $bundleOptionsIds = unserialize($optionsQuoteItemOption->getValue()); if ($bundleOptionsIds) { /** * @var Mage_Bundle_Model_Mysql4_Option_Collection */ $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $this->getProduct()); // get and add bundle selections collection $selectionsQuoteItemOption = $this->getItem()->getOptionByCode('bundle_selection_ids'); $selectionsCollection = $typeInstance->getSelectionsByIds( unserialize($selectionsQuoteItemOption->getValue()), $this->getProduct() ); $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true); foreach ($bundleOptions as $bundleOption) { if ($bundleOption->getSelections()) { $label = $bundleOption->getTitle() $bundleSelections = $bundleOption->getSelections(); foreach ($bundleSelections as $bundleSelection) { $sName = $bundleSelection->getName(); } // some more code here to do stuff } } } 

This code gets the parameters from the related product Quote Item, and then gets the options for this product in the collection, and then finds the selection "Selected".

NTN, Shaun

+8
source

I did this to solve the client’s request, to add the contents of the package as separate elements to the cart. Just replace

 $cart->addProduct($product, $params) 

from

 if ($product->getTypeId() == 'bundle') { $request = new Varien_Object($params); $cartCandidates = $product->getTypeInstance(true)->prepareForCartAdvanced($request, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL); $idstoadd = array(); foreach ($cartCandidates as $cartCandidate) { if ($cartCandidate->getTypeId() == 'simple') { for ($i = 0; $i < $cartCandidate->getCartQty(); $i++) { $idstoadd[] = $cartCandidate->getId(); } } } $cart->addProductsByIds($idstoadd); } else { $cart->addProduct($product, $params); } 

in the cartController file.

+1
source

Here's how you can do it in Magento 2.3 using the plugin around (interceptor) for the Magento \ Checkout \ Model \ Cart-> addProduct () method.

The addProduct () method is called when the buyer adds the product to the cart. By adding the code to this method through the plugin, you can change the way you add the product to the basket.

  1. Define the plugin in Vendor / Module / etc / frontend / di.xml:
 <?xml version="1.0"?> <!-- /** * Copyright Β© 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="\Magento\Checkout\Model\Cart"> <plugin name="add-bundle-products-separate" type="Vendor\Module\Plugin\Checkout\Model\CartPlugin" sortOrder="1"/> </type> </config> 

This is what Magento says there is a plugin for this particular method.

Link: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

  1. Create a plugin class. At Seller / Module / Plugin / Place an order / Model / CartPlugin.php:
 <?php namespace Vendor\Module\Plugin\Checkout\Model; use \Magento\Catalog\Model\Product; use \Magento\Framework\DataObject; use \Magento\Checkout\Model\Cart; /** * Class CartPlugin * * @package Ppwd\CrossSell\Plugin\Checkout\Model * */ class CartPlugin { /** * @param Cart $subject * @param \Closure $proceed * @param Product $productInfo * @param DataObject|int|array $requestInfo * @return Cart $subject * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundAddProduct( $subject, $proceed, $productInfo, $requestInfo = null ) { // Detect if we are adding a bundle product to cart if (!is_numeric($productInfo) && $productInfo->getTypeId() == 'bundle') { $buyRequest = new DataObject($requestInfo); // List of products selected as part of the bundle $cartCandidates = $productInfo->getTypeInstance()->prepareForCartAdvanced($buyRequest, $productInfo); $productIds = []; // Add each item in bundle as if it were separately added to cart /** @var Product $cartCandidate */ foreach ($cartCandidates as $cartCandidate) { if ($cartCandidate->getTypeId() != 'bundle') { for ($i = 0; $i < $cartCandidate->getCartQty(); $i++) { $productIds[] = $cartCandidate->getId(); } } } $subject->addProductsByIds($productIds); return $subject; } // Return original result from addProduct() as if plugin didn't exist $result = $proceed($productInfo, $requestInfo); return $result; } } 

When you finish, if you add the package to the basket, the individual items will be displayed separately, and not grouped together, as is usually the case in the package.

0
source

Source: https://habr.com/ru/post/911894/


All Articles