首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从依赖WooCommerce变量的另一个插件中正确调用一个插件中的函数

如何从依赖WooCommerce变量的另一个插件中正确调用一个插件中的函数
EN

Stack Overflow用户
提问于 2018-10-23 04:08:41
回答 1查看 3.1K关注 0票数 0

我使用的是WooCommerce和两个插件:

  • Yith礼品卡和
  • WooCommerce Pos.

Yith礼品卡插件,允许您为您的商店出售礼品卡代币。当某人购买礼品卡时,WooCommerce订单确认书上印有礼品代码。

WooCommerce POS插件允许您从打印机打印收据。问题是优惠券代码没有显示在打印的收据上。

如何将优惠券代码添加到WooCommerce电子邮件

Yith Gift Card插件通过WooCommerce电子邮件挂钩添加了一个操作,下面是从Yith Plugin php摘录的代码:

代码语言:javascript
运行
复制
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 );
            }
        }

这将导致优惠券代码显示在WooCommerce订单电子邮件中.

我希望打印的POS收据上显示相同的优惠券代码。

打印POS收据是如何生成

我发现了打印POS收据的文件。就在这里:https://github.com/kilbot/WooCommerce-POS/blob/master/includes/views/print/receipt-html.php

如何从接收-html.php中调用show_gift_card_code函数?这样它就能成功地显示礼品卡优惠券代码?

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-23 17:31:46

WooCommerce POS是一个javascript应用程序,因此接收模板只呈现一次,然后填充从with检索的每个订单。试图插入特定订单的数据将不像预期的那样工作。

在这种情况下,您的订单项元与键_ywgc_gift_card_number一起存储。前面带有下划线的元通常被认为是私有的,所以大多数模板(包括WooCommerce POS)都不会显示这个元数据。

一种解决方案是过滤WC响应,将元键更改为不带下划线的内容。下面显示了一些示例代码,您需要将其放在主题functions.php文件中。

代码语言:javascript
运行
复制
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');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52941042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档