php - 在 Woocommerce 中仅向 PayPal 发送订单号而不是商品名称

标签 php wordpress paypal woocommerce orders

在 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 );

这里也没有成功。

非常感谢您的帮助。

最佳答案

Recommendation: You should really avoid overriding woocommerce core files.

相反,您可以使用在这种特殊情况下过滤器钩子(Hook)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>'; 

或者在钩子(Hook)中(在这个例子中仅对管理员可见的商店页面):

// 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' 键上的过滤器 Hook ,用订单号替换商品名称,随心所欲:

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.

请参阅此相关答案:How to get WooCommerce order details

关于php - 在 Woocommerce 中仅向 PayPal 发送订单号而不是商品名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221852/

相关文章:

javascript - 如何使用 PHP、MySqL 和 JQuery 将动态字段的值插入数据库

PHP 5.3.3 - 未加载 Mysql 扩展

ios - 仅从 WordPress 网站抓取文本,显示在 iOS 应用程序上

paypal - 将 PayPal Mass Pay 的费用转嫁给收款人

paypal - WPS 中的 PayPal "amount"格式是什么?

javascript - s在 wordpress 中提交 ajax 数据时没有任何反应

php - 从一个表中选择另一个表中的 id 并用与第二个表不同的 char 替换整数

wordpress - Fishpig:由于index.php,自动登录无法正常工作身份验证异常

wordpress - 在 AWS EC2 ELB SSL 上运行 WordPress 时出现 503 错误

paypal - Coinbase 重定向和回调 URL 设置