2

In sales_order_item table, there's a column product_options to store some custom values.

I need to get the deliverydates from the following value (value from the db column product_options)

{"info_buyRequest":{"qty":"1","super_attribute":{"137":"2803","93":"17717"},"product":"37702","deliverydates":"2018-04-07"}}

I have tried the following code, but is not working.

    foreach ($this->order->getAllVisibleItems() as $orderItem) {
        $options = $orderItem->getProductOptions(); 
        #$options = $orderItem->getProduct()->getTypeInstance(true)->getOrderOptions($orderItem->getProduct());
        if(!empty($options['options'])) {        
            foreach ($options['options'] as $option) {
                if ($option['label'] == 'deliverydates'){
                    $deliverydate = $option['value'];
                }
            }
        }

3 Answers 3

8

Based on your json, you cannot get the delivery dates by options key, you have to use info_buyRequest, Try this

foreach ($this->order->getAllVisibleItems() as $orderItem) {
     $options = $orderItem->getProductOptions(); 
        if(!empty($options['info_buyRequest'])) {
            if(!empty($options['info_buyRequest']["deliverydates"])) {
                $deliverydates = $options['info_buyRequest']["deliverydates"];
             }
        }
}
3
  • Just tried your code. I got an error "Notice: Undefined index: info_buyRequest.". it seems info_buyRequest is empty. I've checked in my DB again, I'm sure the data is there as the JSON data i posted in the answer. I'm don't know why it didn't get me the productOptions. Commented Apr 7, 2018 at 5:04
  • Sorry it was my fault, Please try to use [ $options = $orderItem->getProductOptions(); ] instead of [$options = $orderItem->getProduct()->getTypeInstance(true)->getOrderOptions($orderItem->getProduct());] , If you are working on the order you need to use [ $options = $orderItem->getProductOptions();] same as your code hope this will help Commented Apr 7, 2018 at 5:13
  • I am glad that i could help you Commented Apr 7, 2018 at 5:19
2

you can use below code to get all configurable product options from order

   $opts=array();
   $items = $order->getAllVisibleItems();
      foreach ($items as $item) 
      {       
        $options = $item->getProductOptions();          
        if (isset($options['attributes_info']) && !empty($options['attributes_info'])) 
        {             
        foreach ($options['attributes_info'] as $option) 
        {      
             $opts[strtolower($option['label'])] = $option['value'];   
        }

        }
      }
   print_r($opts);
0

Use this code to get product attribute for product items in Order.

$orderAllItems = $this->order->getAllItems();
foreach ($orderAllItems as $orderItem) {
    $options = $orderItem->getProductOptions();
    if ($options) {
        $info = $options['info_buyRequest'];
    }
}

You can get all custom options from $info.

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.