在 woocommerce 中使用表达式获取产品名称
Get product name using expression in woocommerce
我可以使用表达式 {order_id} 获取订单 ID,如下面的屏幕截图所示。
有什么表达式可以像Order Id一样获取产品名称吗?这是一个支付网关插件,所以编辑插件文件是我最不想做的事情。谢谢
一些知识分子在没有透露他们的知识基础的情况下对我的问题投了反对票。但是对于像我这样的穷人,这是我发现的:
文件:plugins/telr/class-wc-gateway-telr-checkout-handler.php
方法:私有函数generate_request($order)
查找:
$cart_desc = trim(wc_gateway_telr()->settings->__get('cart_desc'));
if (empty($cart_desc)) {
$cart_desc ='Order {order_id}';
}
$cart_desc = preg_replace('/{order_id}/i', $order_id, $cart_desc);
add_post_meta($order_id, '_telr_cartdesc', $cart_desc);
替换为:
$cart_desc = trim(wc_gateway_telr()->settings->__get('cart_desc'));
if (empty($cart_desc)) {
$cart_desc ='Order {order_id}';
}
$order_items = $order->get_items();
$productNames = array();
foreach ($order_items as $product_data) {
$productInfo = $product_data->get_data();
$productNames[] = $productInfo['name'];
}
$cartProducts = implode(", ", $productNames);
$cart_desc = preg_replace('/{order_id}/i', $order_id, $cart_desc);
$cart_desc = preg_replace('/{product_names}/i', $cartProducts, $cart_desc);
$cart_desc = substr($cart_desc, 0, 63);
add_post_meta($order_id, '_telr_cartdesc', $cart_desc);
此后,{product_names} 将替换为“,”分隔的产品名称,最终描述字符串将被截断至 63 个字符(因为这是描述的长度)
我可以使用表达式 {order_id} 获取订单 ID,如下面的屏幕截图所示。
有什么表达式可以像Order Id一样获取产品名称吗?这是一个支付网关插件,所以编辑插件文件是我最不想做的事情。谢谢
一些知识分子在没有透露他们的知识基础的情况下对我的问题投了反对票。但是对于像我这样的穷人,这是我发现的:
文件:plugins/telr/class-wc-gateway-telr-checkout-handler.php
方法:私有函数generate_request($order)
查找:
$cart_desc = trim(wc_gateway_telr()->settings->__get('cart_desc'));
if (empty($cart_desc)) {
$cart_desc ='Order {order_id}';
}
$cart_desc = preg_replace('/{order_id}/i', $order_id, $cart_desc);
add_post_meta($order_id, '_telr_cartdesc', $cart_desc);
替换为:
$cart_desc = trim(wc_gateway_telr()->settings->__get('cart_desc'));
if (empty($cart_desc)) {
$cart_desc ='Order {order_id}';
}
$order_items = $order->get_items();
$productNames = array();
foreach ($order_items as $product_data) {
$productInfo = $product_data->get_data();
$productNames[] = $productInfo['name'];
}
$cartProducts = implode(", ", $productNames);
$cart_desc = preg_replace('/{order_id}/i', $order_id, $cart_desc);
$cart_desc = preg_replace('/{product_names}/i', $cartProducts, $cart_desc);
$cart_desc = substr($cart_desc, 0, 63);
add_post_meta($order_id, '_telr_cartdesc', $cart_desc);
此后,{product_names} 将替换为“,”分隔的产品名称,最终描述字符串将被截断至 63 个字符(因为这是描述的长度)