仅向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['quantity'];
$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 );
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;
}

代码进入函数.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您将能够更改或删除任何参数。

总是只有一个:'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 );
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;
}

代码进入函数.php活动子主题(或主题)的文件或任何插件文件中。

该代码经过测试并适用于WooCommerce 3+

完成:当您在钩子函数中获取WC_Order对象作为参数时,您可以使用它从订单中获取任何数据,并根据需要操作发送到网关PayPal数据。

查看此相关答案: 如何获取WooCommerce订单详细信息

我正在使用以下函数来获取订单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 )
);

最新更新