Magento 2 help validate function to count bundle product child items of an order

I’m trying to write a function that takes an order object, and returns an array of sku => qty (key-value) showing how many of each sku was sold/shipped.

The tricky part is, magento formats line items differently on configurable and bundle products.

Example:

Line1: name shirt-X2, type bundle, ordered 2, shipped 2. Line2: name shirt, type simple, ordered 4, shipped 0. // <-- my problem 

I’ve written this code:

/**  * @param \Magento\Sales\Model\Order $  order  * @return array  */ public function getOrderData($  order) {     $  result = [];     foreach ($  order->getAllItems() as $  item) {         if ($  item->getProductType() != 'simple') {             continue;         }         $  sku = $  item->getSku();         $  orderedReal = $  item->getQtyOrdered();         $  orderedB = $  item->getQtyOrdered() - $  item->getQtyRefunded() - $  item->getQtyCanceled();         $  orderedB = max(round($  orderedB, 8), 0);         $  invoicedB = $  item->getQtyOrdered() - $  item->getQtyInvoiced() - $  item->getQtyCanceled();         $  invoicedB = max(round($  invoicedB, 8), 0);         $  shippedReal = 0;         $  isShipped = false;         if ($  item->getParentItem()) {             $  parent = $  item->getParentItem();             if ($  parent->getQtyOrdered() > 0 && $  parent->getQtyOrdered() == $  parent->getQtyShipped()) {                 $  isShipped = true;             }         } else {             if ($  item->getQtyOrdered() > 0 && $  item->getQtyOrdered() == $  item->getQtyShipped()) {                 $  isShipped = true;             }         }         if ($  isShipped) {             $  shippedReal = $  orderedReal;         }         $  result[] = [             'sku' => $  sku,             'invoiced' => $  invoicedB,             'ordered' => $  orderedB,             'shipped' => $  shippedReal         ];     }     return $  result; } 

Is this function valid, or could it be improved ? Any help is appreciated.

Thanks

Magento 2 help validate function to count bundle product child items of an order

I’m trying to write a function that takes an order object, and returns an array of sku => qty (key-value) showing how many of each sku was sold/shipped.

The tricky part is, magento formats line items differently on configurable and bundle products.

Example:

Line1: name shirt-X2, type bundle, ordered 2, shipped 2. Line2: name shirt, type simple, ordered 4, shipped 0. // <-- my problem 

I’ve written this code:

/**  * @param \Magento\Sales\Model\Order $  order  * @return array  */ public function getOrderData($  order) {     $  result = [];     foreach ($  order->getAllItems() as $  item) {         if ($  item->getProductType() != 'simple') {             continue;         }         $  sku = $  item->getSku();         $  orderedReal = $  item->getQtyOrdered();         $  orderedB = $  item->getQtyOrdered() - $  item->getQtyRefunded() - $  item->getQtyCanceled();         $  orderedB = max(round($  orderedB, 8), 0);         $  invoicedB = $  item->getQtyOrdered() - $  item->getQtyInvoiced() - $  item->getQtyCanceled();         $  invoicedB = max(round($  invoicedB, 8), 0);         $  shippedReal = 0;         $  isShipped = false;         if ($  item->getParentItem()) {             $  parent = $  item->getParentItem();             if ($  parent->getQtyOrdered() > 0 && $  parent->getQtyOrdered() == $  parent->getQtyShipped()) {                 $  isShipped = true;             }         } else {             if ($  item->getQtyOrdered() > 0 && $  item->getQtyOrdered() == $  item->getQtyShipped()) {                 $  isShipped = true;             }         }         if ($  isShipped) {             $  shippedReal = $  orderedReal;         }         $  result[] = [             'sku' => $  sku,             'invoiced' => $  invoicedB,             'ordered' => $  orderedB,             'shipped' => $  shippedReal         ];     }     return $  result; } 

Is this function valid, or could it be improved ? Any help is appreciated.

Thanks

Magento 2 help validate function to count bundle product child items of an order

I’m trying to write a function that takes an order object, and returns an array of sku => qty (key-value) showing how many of each sku was sold/shipped.

The tricky part is, magento formats line items differently on configurable and bundle products.

Example:

Line1: name shirt-X2, type bundle, ordered 2, shipped 2. Line2: name shirt, type simple, ordered 4, shipped 0. // <-- my problem 

I’ve written this code:

/**  * @param \Magento\Sales\Model\Order $  order  * @return array  */ public function getOrderData($  order) {     $  result = [];     foreach ($  order->getAllItems() as $  item) {         if ($  item->getProductType() != 'simple') {             continue;         }         $  sku = $  item->getSku();         $  orderedReal = $  item->getQtyOrdered();         $  orderedB = $  item->getQtyOrdered() - $  item->getQtyRefunded() - $  item->getQtyCanceled();         $  orderedB = max(round($  orderedB, 8), 0);         $  invoicedB = $  item->getQtyOrdered() - $  item->getQtyInvoiced() - $  item->getQtyCanceled();         $  invoicedB = max(round($  invoicedB, 8), 0);         $  shippedReal = 0;         $  isShipped = false;         if ($  item->getParentItem()) {             $  parent = $  item->getParentItem();             if ($  parent->getQtyOrdered() > 0 && $  parent->getQtyOrdered() == $  parent->getQtyShipped()) {                 $  isShipped = true;             }         } else {             if ($  item->getQtyOrdered() > 0 && $  item->getQtyOrdered() == $  item->getQtyShipped()) {                 $  isShipped = true;             }         }         if ($  isShipped) {             $  shippedReal = $  orderedReal;         }         $  result[] = [             'sku' => $  sku,             'invoiced' => $  invoicedB,             'ordered' => $  orderedB,             'shipped' => $  shippedReal         ];     }     return $  result; } 

Is this function valid, or could it be improved ? Any help is appreciated.

Thanks

magento 2 ; only want custom product attributes to show on custom product

I created a custom virtual product that is based on a date attribute. i.e product is available on certain days a month.

However, the only attributes I want for it are name, sku, quantity, date.

I built a custom date feature but this date is now showing up on all other type of products.

question 1:

how can i ensure that the custom attributes only show on my custom virtual product.

QUESTION TWO

How can I remove all other attributes/form values like content, design etc

this is what i did to create the custom date attribute:

Test/Example/Ui/Component/DataProvider/Product/Form/Modifier/Datetime.php

public function install(ModuleDataSetupInterface $  setup, ModuleContextInterface $  context)     {         $  eavSetup = $  this->eavSetupFactory->create(['setup' => $  setup]);          $  eavSetup->addAttribute(                 \Magento\Catalog\Model\Product::ENTITY,         'dome_dates',         [             'group' => 'Custom Attribute',             'label' => 'Dome Date',             'type' => 'datetime',             'input' => 'date',             'class' => 'validate-date',             'required' => false,             'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,             'visible' => true,             'required' => false,             'user_defined' => true,             'default' => '',             'searchable' => true,             'filterable' => true,             'filterable_in_search' => true,             'visible_in_advanced_search' => true,             'comparable' => false,             'visible_on_front' => false,             'used_in_product_listing' => true,             'unique' => false         ]     );      } 

and in my Di:

 <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">         <arguments>             <argument name="modifiers" xsi:type="array">                 <item name="dome_dates" xsi:type="array">                     <item name="class" xsi:type="string">Test\Example\Ui\DataProvider\Product\Form\Modifier\Datetime</item>                     <item name="sortOrder" xsi:type="number">100</item>                 </item>             </argument>         </arguments>     </virtualType>