如何从依赖于 WooCommerce 变量的另一个插件正确调用一个插件中的函数
How to correctly call a function in one plugin, from another plugin, which relies on WooCommerce variables
我正在使用带有两个插件的 WooCommerce:
- Yith 礼品卡和
- WooCommerce 排名
Yith 礼品卡插件,允许您为您的商店销售礼品卡代币。
当有人购买礼品卡时,WooCommerce 订单确认函上印有礼品代码。
WooCommerce POS 插件允许您从打印机打印收据。问题是优惠券代码未显示在此打印的收据上。
如何将优惠券代码添加到 WooCommerce 电子邮件中
Yith 礼品卡插件通过 WooCommerce 电子邮件挂钩添加一个操作,这里是代码,摘自 Yith 插件 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 );
}
}
这会导致优惠券代码显示在 WooCommerce 订单电子邮件中。
我希望在打印的 POS 收据上显示相同的优惠券代码。
打印的POS收据是如何生成的
我找到了负责打印打印的 POS 收据的文件。
它在这里:https://github.com/kilbot/WooCommerce-POS/blob/master/includes/views/print/receipt-html.php
如何从 receipt-html.php 中调用 show_gift_card_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>
WooCommerce POS 是一个 javascript 应用程序,因此收据模板呈现一次,然后填充从 WC REST API 检索到的每个订单。尝试为特定订单插入数据将无法按预期工作。
对于这种情况,您的订单项元数据使用键 _ywgc_gift_card_number
存储。前面带有下划线的元数据通常被认为是私有的,因此大多数模板(包括 WooCommerce POS)不会显示此元数据。
一种解决方案是过滤 WC REST API 响应以将元键更改为不带下划线的内容。一些 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');
我正在使用带有两个插件的 WooCommerce:
- Yith 礼品卡和
- WooCommerce 排名
Yith 礼品卡插件,允许您为您的商店销售礼品卡代币。 当有人购买礼品卡时,WooCommerce 订单确认函上印有礼品代码。
WooCommerce POS 插件允许您从打印机打印收据。问题是优惠券代码未显示在此打印的收据上。
如何将优惠券代码添加到 WooCommerce 电子邮件中
Yith 礼品卡插件通过 WooCommerce 电子邮件挂钩添加一个操作,这里是代码,摘自 Yith 插件 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 );
}
}
这会导致优惠券代码显示在 WooCommerce 订单电子邮件中。
我希望在打印的 POS 收据上显示相同的优惠券代码。
打印的POS收据是如何生成的
我找到了负责打印打印的 POS 收据的文件。 它在这里:https://github.com/kilbot/WooCommerce-POS/blob/master/includes/views/print/receipt-html.php
如何从 receipt-html.php 中调用 show_gift_card_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>
WooCommerce POS 是一个 javascript 应用程序,因此收据模板呈现一次,然后填充从 WC REST API 检索到的每个订单。尝试为特定订单插入数据将无法按预期工作。
对于这种情况,您的订单项元数据使用键 _ywgc_gift_card_number
存储。前面带有下划线的元数据通常被认为是私有的,因此大多数模板(包括 WooCommerce POS)不会显示此元数据。
一种解决方案是过滤 WC REST API 响应以将元键更改为不带下划线的内容。一些 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');