在 Woocommerce 中仅向 PayPal 发送订单号而不是商品名称
在 Woocommerce 的 PayPal 标准网关中,我想让 Woocommerce
仅发送订单号"作为购物车中的唯一项目,而不是逐项产品列表.
为此,我尝试在此处编辑负责发出 PayPal 请求的类:woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
我尝试编辑 get_order_item_names()
函数以返回 "invoice" .$order->get_order_number()
作为唯一项的名称,但不成功,因为如果有多个项,则只返回第一个和订单号,其他项保留.
另外,我钉了add_line_item()
函数,因为为了达到目的,实际上应该只有item_name_1"和卡的总金额.
$this->line_items[ 'item_name_' .$index ] = $this->limit_length( $item['item_name'], 127 );$this->line_items[ 'quantity_' .$index ] = $item['数量'];$this->line_items[ 'amount_' .$index ] = $item['amount'];$this->line_items[ 'item_number_' .$index ] = $this->limit_length( $item['item_number'], 127 );
这里也没有成功.
非常感谢您的帮助.
解决方案建议:你真的应该避免覆盖 woocommerce 核心文件.
相反,您可以使用在这种特殊情况下过滤器钩子woocommerce_paypal_args
,其中您将能够操纵参数 用于 get_request_url()
函数(将获取订单的 PayPal 请求 URL).
1) 测试并获取发送的数据
只是为了注册并获取发送到 paypal 的参数,我以这种方式使用了钩子:
add_filter('woocommerce_paypal_args', 'custom_paypal_args', 10, 2 );函数 custom_paypal_args ( $args, $order ) {//保存数据以订购元数据(自定义字段)update_post_meta( $order->get_id(), '_test_paypal', $args );返回 $args;}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
现在我可以简单地使用这个来获取数据(设置正确的订单 ID):
$order_id = 648;$data_sent_to_paypal = get_post_meta( $order_id, '_test_paypal' );echo '';print_r( $data_sent_to_paypal );echo '</pre>';
或者在一个钩子里(在这个例子中的商店页面中只对管理员可见):
//只有管理员在商店页面上可见add_action('woocommerce_before_main_content', function(){//只有管理员可见if(!current_user_can('manage_options')) 返回;$order_id = 648;$data_sent_to_paypal = get_post_meta( $order_id, '_test_paypal' );echo '';print_r( $data_sent_to_paypal );echo '</pre>';}, 10, 0 );
这给了我这样的输出(对于 2 种产品和不同数量):
数组([0] =>大批([cmd] =>_大车[业务] =>电子邮件@example.com[no_note] =>1[货币代码] =>欧元[字符集] =>utf-8[rm] =>2[上传] =>1[返回] =>https://example.com/checkout/order-received/8877?key=wc_order_55445ndfgbulfdf&utm_nooverride=1[cancel_return] =>https://example.com/cart/?cancel_order=true&order=wc_order_55445ndfgbulfdf&order_id=8877&redirect&_wpnonce=34m7kl83455[页面样式] =>[image_url] =>https://example.com/wp-content/uploads/2012/06/logo.png[付款操作] =>销售[bn] =>WooThemes_Cart[发票] =>pp-8877[自定义] =>{"order_id":8877,"order_key":"wc_order_55445ndfgbulfdf"}[notify_url] =>https://example.com/wc-api/WC_Gateway_Paypal/[名字] =>约翰[姓氏] =>美国能源部[地址 1] =>测试圣.[地址 2] =>[城市] =>wef[状态] =>增强现实[zip] =>43242[国家] =>我们[电子邮件] =>myemail@example.com[night_phone_a] =>078[night_phone_b] =>653[night_phone_c] =>6216[no_shipping] =>1[tax_cart] =>16.55[item_name_1] =>测试产品 - 服务[数量_1] =>1[amount_1] =>71[item_number_1] =>[item_name_2] =>测试产品 1[数量_2] =>1[amount_2] =>66[item_number_2] =>[item_name_3] =>测试产品 2[数量_3] =>1[amount_3] =>120[item_number_3] =>[item_name_4] =>测试产品 3[数量_4] =>1[amount_4] =>45[item_number_4] =>))
正如您现在看到的,使用 woocommerce_paypal_args
,您将能够更改或删除任何参数.
总是只有一个:'item_name_1'
、'quantity_1'
、'amount_1'
和 <代码>'item_number_1' 始终将索引 1
发送到 paypal.
<小时>
2 处理发送的数据 (示例):
我们仍然可以使用woocommerce_paypal_args
,例如在'item_name_1'
键上过滤钩子,用订单号替换项目名称,随心所欲:
add_filter('woocommerce_paypal_args', 'custom_paypal_args', 10, 2 );函数 custom_paypal_args ( $args, $order ) {$$args_keys = array_keys($args);$i = 0;//遍历订单项foreach( $order->get_items() as $item_id => $item_product ){$i++;//更新计数.如果(!空($args[item_name_$i"])){//返回商品名称中的订单发票$args["item_name_$i"] = "发票#" .$order->get_id();}}返回 $args;}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
代码经过测试,可在 WooCommerce 3+ 上运行
<块引用>结束: 当您在钩子函数中获得 WC_Order 对象作为参数时,您可以使用它从订单中获取任何数据,并按照您喜欢的方式操作发送到贝宝网关的数据.
请参阅此相关答案:如何获取 WooCommerce 订单详细信息一个>
In PayPal standard gateway of Woocommerce, I want to make Woocommerce
sends only "order number" as the only item in the cart, instead of the itemized product list.
For that, I tried to edit the class which is responsible for making a PayPal request here:
woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
I tried to edit get_order_item_names()
function to return "invoice" . $order->get_order_number()
as the name of the only item, but it wasn't successful since if there would be several items, Only the first one was returned with the order number, and other items remained.
Also, I nailed the add_line_item()
function because to approach the purpose, practically there should be only "item_name_1" with the amount of total amount of the card.
$this->line_items[ 'item_name_' . $index ] = $this->limit_length( $item['item_name'], 127 );
$this->line_items[ 'quantity_' . $index ] = $item['quantity'];
$this->line_items[ 'amount_' . $index ] = $item['amount'];
$this->line_items[ 'item_number_' . $index ] = $this->limit_length( $item['item_number'], 127 );
No success here too.
I'd appreciate your assistance.
解决方案Recommendation: You should really avoid overriding woocommerce core files.
Instead you could use in this particular case the filter hook woocommerce_paypal_args
, where you will be able to manipulate the arguments that are used in get_request_url()
function (that will get the PayPal request URL for an order).
1) TESTING AND GETTING THE DATA SENT
Just to register and get the arguments sent to paypal, I have used the hook this way:
add_filter('woocommerce_paypal_args', 'custom_paypal_args', 10, 2 );
function custom_paypal_args ( $args, $order ) {
// Saving the data to order meta data (custom field)
update_post_meta( $order->get_id(), '_test_paypal', $args );
return $args;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Now I can get the data simply using this (setting the correct order ID):
$order_id = 648;
$data_sent_to_paypal = get_post_meta( $order_id, '_test_paypal' );
echo '<pre>'; print_r( $data_sent_to_paypal ); echo '</pre>';
Or in a hook ( visible in shop pages in this example only for admins ):
// Only visible on shop pages by admins
add_action( 'woocommerce_before_main_content', function(){
// Only visible by admins
if( ! current_user_can( 'manage_options' ) ) return;
$order_id = 648;
$data_sent_to_paypal = get_post_meta( $order_id, '_test_paypal' );
echo '<pre>'; print_r( $data_sent_to_paypal ); echo '</pre>';
}, 10, 0 );
This gives me an output like that (for 2 products and different quantities):
Array
(
[0] => Array
(
[cmd] => _cart
[business] => email@example.com
[no_note] => 1
[currency_code] => EUR
[charset] => utf-8
[rm] => 2
[upload] => 1
[return] => https://example.com/checkout/order-received/8877?key=wc_order_55445ndfgbulfdf&utm_nooverride=1
[cancel_return] => https://example.com/cart/?cancel_order=true&order=wc_order_55445ndfgbulfdf&order_id=8877&redirect&_wpnonce=34m7kl83455
[page_style] =>
[image_url] => https://example.com/wp-content/uploads/2012/06/logo.png
[paymentaction] => sale
[bn] => WooThemes_Cart
[invoice] => pp-8877
[custom] => {"order_id":8877,"order_key":"wc_order_55445ndfgbulfdf"}
[notify_url] => https://example.com/wc-api/WC_Gateway_Paypal/
[first_name] => John
[last_name] => Doe
[address1] => Test st.
[address2] =>
[city] => wef
[state] => AR
[zip] => 43242
[country] => US
[email] => myemail@example.com
[night_phone_a] => 078
[night_phone_b] => 653
[night_phone_c] => 6216
[no_shipping] => 1
[tax_cart] => 16.55
[item_name_1] => Test Product - Service
[quantity_1] => 1
[amount_1] => 71
[item_number_1] =>
[item_name_2] => Test Product 1
[quantity_2] => 1
[amount_2] => 66
[item_number_2] =>
[item_name_3] => Test Product 2
[quantity_3] => 1
[amount_3] => 120
[item_number_3] =>
[item_name_4] => Test Product 3
[quantity_4] => 1
[amount_4] => 45
[item_number_4] =>
)
)
As you can see now, with woocommerce_paypal_args
you will be able to alter or remove any arguments.
There is always only one:
'item_name_1'
,'quantity_1'
,'amount_1'
and'item_number_1'
with always index1
sent to paypal.
2 MANIPULATING THE DATA SENT (example):
We can still use woocommerce_paypal_args
, filter hook for example on 'item_name_1'
key, to replace the items names by the order number, just as you want:
add_filter('woocommerce_paypal_args', 'custom_paypal_args', 10, 2 );
function custom_paypal_args ( $args, $order ) {
$$args_keys = array_keys($args);
$i = 0;
// Iterating through order items
foreach( $order->get_items() as $item_id => $item_product ){
$i++; // updating count.
if( ! empty($args["item_name_$i"]) ){
// Returning the order invoice in the item name
$args["item_name_$i"] = "invoice #" . $order->get_id();
}
}
return $args;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works on WooCommerce 3+
To finish: As you get the WC_Order object as argument in your hooked function, you can use it to get any data from the order, and manipulate as you like the data sent to paypal gateway.
See this related answer: How to get WooCommerce order details
相关文章