在 Woocommerce 中仅向 PayPal 发送订单号而不是商品名称
Sending only order number instead of item names to PayPal in Woocommerce
在 Woocommerce 的 PayPal 标准网关中,我想让 Woocommerce
只发送“订单号”作为购物车中的唯一项目,而不是逐项列出的产品列表。
为此,我尝试在此处编辑负责发出 PayPal 请求的 class:
woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
我尝试将 get_order_item_names()
函数编辑为 return "invoice" . $order->get_order_number()
作为唯一项目的名称,但没有成功,因为如果有多个项目,只有第一个 return 使用订单号编辑,其他项目保留。
此外,我确定了 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['quantity'];
$this->line_items[ 'amount_' . $index ] = $item['amount'];
$this->line_items[ 'item_number_' . $index ] = $this->limit_length( $item['item_number'], 127 );
这里也没有成功。
非常感谢你的帮助。
我正在使用以下函数获取订单 ID,您可以根据需要编辑和使用以下函数。
public function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
Recommendation: You should really avoid overriding woocommerce core files.
在这种特殊情况下,您可以使用 过滤器挂钩 woocommerce_paypal_args
,其中 您将能够操纵 get_request_url()
函数中使用的参数 (这将获得 PayPal 请求 URL 的订单)。
1) 测试并获取发送的数据
只是为了注册并获取发送到paypal的参数,我是这样使用hook的:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
现在我可以简单地使用这个(设置正确的订单 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>';
或者在挂钩中(在本示例中的商店页面中仅对管理员可见):
// 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 );
这给了我这样的输出(对于 2 种产品和不同数量):
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] =>
)
)
正如您现在看到的,使用 woocommerce_paypal_args
您将能够更改或删除任何参数。
There is always only one: 'item_name_1'
, 'quantity_1'
, 'amount_1'
and 'item_number_1'
with always index 1
sent to paypal.
2 处理发送的数据 (示例):
我们仍然可以使用 woocommerce_paypal_args
,例如 'item_name_1'
键上的过滤器挂钩,用订单号替换项目名称,就像您一样想要:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并适用于 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.
查看相关回答:
在 Woocommerce 的 PayPal 标准网关中,我想让 Woocommerce
只发送“订单号”作为购物车中的唯一项目,而不是逐项列出的产品列表。
为此,我尝试在此处编辑负责发出 PayPal 请求的 class:
woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
我尝试将 get_order_item_names()
函数编辑为 return "invoice" . $order->get_order_number()
作为唯一项目的名称,但没有成功,因为如果有多个项目,只有第一个 return 使用订单号编辑,其他项目保留。
此外,我确定了 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['quantity'];
$this->line_items[ 'amount_' . $index ] = $item['amount'];
$this->line_items[ 'item_number_' . $index ] = $this->limit_length( $item['item_number'], 127 );
这里也没有成功。
非常感谢你的帮助。
我正在使用以下函数获取订单 ID,您可以根据需要编辑和使用以下函数。
public function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
Recommendation: You should really avoid overriding woocommerce core files.
在这种特殊情况下,您可以使用 过滤器挂钩 woocommerce_paypal_args
,其中 您将能够操纵 get_request_url()
函数中使用的参数 (这将获得 PayPal 请求 URL 的订单)。
1) 测试并获取发送的数据
只是为了注册并获取发送到paypal的参数,我是这样使用hook的:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
现在我可以简单地使用这个(设置正确的订单 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>';
或者在挂钩中(在本示例中的商店页面中仅对管理员可见):
// 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 );
这给了我这样的输出(对于 2 种产品和不同数量):
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] =>
)
)
正如您现在看到的,使用 woocommerce_paypal_args
您将能够更改或删除任何参数。
There is always only one:
'item_name_1'
,'quantity_1'
,'amount_1'
and'item_number_1'
with always index1
sent to paypal.
2 处理发送的数据 (示例):
我们仍然可以使用 woocommerce_paypal_args
,例如 'item_name_1'
键上的过滤器挂钩,用订单号替换项目名称,就像您一样想要:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并适用于 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.
查看相关回答: