0
votes

I am using WooCommerce with two plugins:

  • Yith Gift Card and
  • WooCommerce Pos.

Yith Gift Card plugin, allows you to sell Gift Card tokens for your store. When someone purchases a gift card the WooCommerce order confirmation has the Gift Code printed on it.

The WooCommerce POS plugin, allows you to print a receipt from a printer. The problem is the coupon code does not display on this printed receipt.

How the coupon code is added to the WooCommerce e-mail

The Yith Gift Card Plugin adds an action via a WooCommerce e-mail hook, here is the code, excerpted from the Yith Plugin php:

class YITH_WooCommerce_Gift_Cards {
    ...

            add_action( 'woocommerce_order_item_meta_start', array(
                $this,
                'show_gift_card_code',
            ), 10, 3 );

        }


        public function show_gift_card_code( $order_item_id, $item, $order ) {

            $code = wc_get_order_item_meta( $order_item_id, YWGC_META_GIFT_CARD_NUMBER );

            if ( ! empty( $code ) ) {

                printf( '<br>' . __( 'Gift card code: %s', 'yith-woocommerce-gift-cards' ), $code );
            }
        }

This results in the coupon code being displayed on the WooCommerce order e-mail.

I want the same coupon code to appear on the printed POS receipt.

How the printed POS receipt is generated

I've found the file responsible for printing the printed POS receipt. It is here: https://github.com/kilbot/WooCommerce-POS/blob/master/includes/views/print/receipt-html.php

How can I call the show_gift_card_code function from within receipt-html.php? So that it successfully displays the Gift Card Coupon code?

<table class="order-items">
  <thead>
    <tr>
      <th class="product"><?php /* translators: woocommerce */ _e( 'Product', 'woocommerce' ); ?></th>
      <th class="qty"><?php _ex( 'Qty', 'Abbreviation of Quantity', 'woocommerce-pos' ); ?></th>
      <th class="price"><?php /* translators: woocommerce */ _e( 'Price', 'woocommerce' ); ?></th>
    </tr>
  </thead>
  <tbody>
  {{#each line_items}}
    <tr>
      <td class="product">
        {{name}}
        [*I WOULD LIKE THE COUPON CODE DISPLAYED HERE*]
        {{#with meta}}
        <dl class="meta">
          {{#each []}}
          <dt>{{label}}:</dt>
          <dd>{{value}}</dd>
          {{/each}}
        </dl>
        {{/with}}
      </td>
1
Try this. You can create your own custom function in your active theme's function.php file and create a action/hook out of it. Then you can use that action/hook in the POS plugin at required location.zipkundan
Thanks for the suggestion, unfortunately this didn't work.Gary

1 Answers

1
votes

WooCommerce POS is a javascript application, so the receipt template is rendered once and then populated with each order retrieved from the WC REST API. Trying to insert data for a specific order will not work as expected.

For this case, your order item meta is stored with the key _ywgc_gift_card_number. Meta with an underscore at the front are generally considered private so most templates (including WooCommerce POS) will not display this meta data.

One solution would be to filter the WC REST API response to change the meta key to something without the underscore. Some example code is shown below, you would need to place this in your theme functions.php file.

function my_custom_wc_rest_shop_order_object($response)
{
  if (function_exists('is_pos') && is_pos()) {
    $data = $response->get_data();
    if (is_array($data['line_items'])) : foreach ($data['line_items'] as &$line_item) :
      if ($code = wc_get_order_item_meta($line_item['id'], '_ywgc_gift_card_number')) {
        $line_item['meta_data'][] = new WC_Meta_Data(array(
          'id' => '',
          'key' => 'Gift Card',
          'value' => $code,
        ));
      }
    endforeach; endif;
    $response->set_data($data);
  }
  return $response;
}
add_filter('woocommerce_rest_prepare_shop_order_object', 'my_custom_wc_rest_shop_order_object');