Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

php - Sending only order number instead of item names to PayPal in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 protected]
        [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] => [email protected]
        [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 index 1 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...