php – Create order details shortcode for WooCommerce

Question

Hey I am trying to build a shortcode for my order details on the order received page.

The code below will generate the last result and then on top of it, it will display the word Array.

I feel like i am doing something wrong with the shortcode_atts but I honestly have no idea what it is.

Any help would be appreciated.

function getOrderItemList($atts){

  //set up array and Count
  $item_data = "";

  extract( shortcode_atts( array(
      'product_image' => '',
      'product_name_quantity' => '',
      'product_total' => '',
  ), $atts ) );

  //get order ID
  global $wp;
  $order_id  = absint( $wp->query_vars['order-received'] );
  $order = wc_get_order( $order_id );

  foreach ($order->get_items() as $item_id => $item) {
    $item_data .= '<tr class="order-item-row">';

    $product = $item->get_product();
    $product_id = $item->get_product_id();
    $item_data = $item->get_data();

    $item_data .= '<td class="order-item-image">' . $product->get_image('order-received-item-image', $item) . '</td>';
    $item_data .= '<td class="order-item-name"><p>' . $item->get_name() . ' x ' . $item->get_quantity() . '</p></td>';
    $item_data .= '<td class="order-item-total"><p>' . $order->get_formatted_line_subtotal( $item ) . '</p></td></tr>';
    $item_data .= '</tr>';

  }

  $table = <<<EOD
              <table>
                $item_data
              </table>
              EOD;



  return $table;

}

function order_items_shortcodes_init() {
    add_shortcode('order-line-item', 'getOrderItemList');
}

add_action('init', 'order_items_shortcodes_init');

0
Andrew Miller 5 months 2022-07-09T01:22:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse