0

I have a magento 2.2.5 based website and i need to get a custom product attribute on my order success page for each product. The custom attribute is called site_owned_buy any suggestions how to get it on the below code for each product I loop through

<?php
      $ga_data = [];

      foreach ($order->getAllVisibleItems() as $item) {
          $ga_item = array(
              'name' => $item->getName(), 
              'sku' => $item->getSku(),
              'qty' => $item->getQtyordered(),
              'price' => $item->getPrice()
          );

          array_push($ga_data, $ga_item);
      }

      $subtotal = $order->getSubtotal();
      $order_id = $block->getOrderId();
      $gtotal = $order->getGrandTotal();
      $address = $order->getBillingAddress()->getData();
      $city = $address['city'];
      $state = $address['region'];
      $country = $address['country_id'];
      $shippingVal = is_null($order->getShippingAmount()) ? 0 : $order->getShippingAmount();
      $taxAmount=$order->getTaxAmount();

    ?>



  <script type="text/javascript">

   dataLayer.push({
        'event': 'purchase',
        'ecommerce': {
          'purchase': {
            'actionField': {
               'id': '<?php echo $order_id; ?>',                      // Transaction ID. Required for purchases and refunds.
               'revenue': '<?php echo $gtotal;?>',            // Total transaction value (incl. tax and shipping)
               'tax':'<?php echo $taxAmount;?>',                     // Total tax value
               'shipping': '<?php echo $shippingVal;?>',          // Total shipping value
               'coupon': ''                           // Optional field and may be set to empty string

        },
        'products': [ 
            <?php foreach ($ga_data as $item) 
                  { 
            ?>        
              {
                'name': '<?php echo $item['name']; ?>',                      // Name or ID is required
                'id': '<?php echo $item['sku']; ?>',                          // Name or ID is required
                'price': '<?php echo $item['price']; ?>',                    // Insert product price
                'brand': '',                    // Insert product brand
                'category': '',              // Insert product category
                'variant': '',                // Insert product variant (e.g. red, black, etc.)
                'quantity': '<?php echo $item['qty']; ?>',                      // Insert product quantity
                'coupon': '',                          // Optional field and may be set to empty string
                'site': ''
            },
  <?php 
    } 
?> 
           ]
    }
}
});

1

Modified your code below. you need to load product from ID to get Attribute value:

<?php
  $ga_data = [];

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();


  foreach ($order->getAllVisibleItems() as $item) {


    $productId = $item->getProductId();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

    $ga_item = array(
          'name' => $item->getName(), 
          'sku' => $item->getSku(),
          'qty' => $item->getQtyordered(),
          'price' => $item->getPrice(),
          'site_owned_buy'=> $product->getData('site_owned_buy')
      );

      array_push($ga_data, $ga_item);
  }

?>

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.