skip to Main Content

So I have created a custom action button in my woocommerce order list, but I want the button to link to a page where I will print out information attached to that order. Now to do so I would like to carry data from the order to the page where I am going to print the information.

So I have created my custom button in the woocommerce order list with this code:

add_action( 'woocommerce_admin_order_actions_end', 'add_content_to_wcactions_column' );
function add_content_to_wcactions_column() {

    // create some tooltip text to show on hover
    $tooltip = __('Print details', 'textdomain');

    // create a button label
    $label = __('P1', 'textdomain');

    $printurl = wc_get_order_item_meta($post_id, 'street-name', true);

    echo '<a class="button tips custom-class" href="/order-info/?orderid='.$printurl.'" data-tip="'.$tooltip.'" target="_blank">'.$label.'</a>';
}

The button links to the print page which should carry the orders ‘street-name’ meta, but the meta doesnt show, it just appears blank in the url.

Heres what my order item information looks like:

enter image description here

I should say im using the WooCommerce Custom Product Addons plugin if that helps.

2

Answers


  1. There are some mistakes and missing things in your code, like:

    • $order argument is missing from the hooked function
    • $post_id is not defined.
    • You need to get first the correct Item ID to use wc_get_order_item_meta() function

    Reminder: An order can have many items (products).

    In the following code we will get the first order Item (assuming that street-name is the correct meta_key to get the custom order item meta data value):

    add_action( 'woocommerce_admin_order_actions_end', 'admin_order_actions_custom_button' );
    function admin_order_actions_custom_button( $order ) {
    
        // create some tooltip text to show on hover
        $tooltip = __('Print details', 'textdomain');
    
        // create a button label
        $label = __('P1', 'textdomain');
    
        // get order line items
        $order_items = $order->get_items();
    
        // get the first item
        $first_item  = reset( $order_items );
    
        // get 'street-name' order item custom meta data
        $print_url   = $first_item->get_meta('street-name'); // or: wc_get_order_item_meta($first_item->get_id(), 'street-name', true);
    
        echo '<a class="button tips custom-class" href="/order-info/?orderid='.$print_url.'" data-tip="'.$tooltip.'" target="_blank">'.$label.'</a>';
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should work.

    Login or Signup to reply.
  2. I am not sure if you meant that street-name is the meta key for the order or the item, so I covered both cases in the following code, you can delete what’s not relevant for you.

    Here is an example of page template that will get order ID in the querystring, and will display some information.

    To make this happen, you need to create a php file in your theme directory, you can call it “template-order-info.php” and paste this content into it.

    Then, go to your admin and create a new page and assign the page template from the select box to be “Order Info” (the new file).

    Then view the file, and add the following query string: /?order=#### (replace with the order ID). so for example: http://www.mysite.com/orderinfo/?order=1001

        <?php
    /* Template Name: Order Info */
    
    $order_id = $_GET['order'];
    
    # Some basic filtering for wrong order ID passed
    if (!is_numeric($order_id)) wp_die('Invalid Order ID');
    
    $order = new WC_Order($order_id);
    
    if (!$order) wp_die('Invalid Order ID');
    
    get_header();
        ?>
    
    <h2>Order #<?php echo $order->get_id(); ?> Information</h2>
    
    <ul>
        <li>Full Name: <?php echo $order->get_billing_first_name().' '.$order->get_billing_last_name(); ?> </li>
        <li>Street Name: <?php echo $order->get_meta('street-name'); ?></li>
        <li>Billing Address: <?php echo $order->get_billing_address_1(); ?></li>
        <li>Billing Address 2: <?php echo $order->get_billing_address_2(); ?></li>
        <li>
            <h3>Order Items:</h3>
            <ol>
                <?php foreach ($order->get_items() as $item){ 
                    ?>
                    <li><strong><?php echo $item->get_name(); ?></strong> - Street Name: <?php echo $item->get_meta('street-name');?></li>
                <?php } ?>
            </ol>
        </li>
    </ul>
    
    
        <?php
    get_footer();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search