久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Woocommerce 中僅向 PayPal 發送訂單號而不是商品

Sending only order number instead of item names to PayPal in Woocommerce(在 Woocommerce 中僅向 PayPal 發送訂單號而不是商品名稱)
本文介紹了在 Woocommerce 中僅向 PayPal 發送訂單號而不是商品名稱的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

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

這篇關于在 Woocommerce 中僅向 PayPal 發送訂單號而不是商品名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品久久久久久久粉嫩 | 日韩在线中文 | 天天色天天射天天干 | 国精品一区二区 | 成人一区二区三区在线 | 国产高清视频一区二区 | 亚洲国产一区二区三区在线观看 | 久久精品成人一区 | 国产一区二区三区四区区 | 超碰人人91 | 精品综合久久 | 久久爆操 | 成人福利网站 | 成人久久18免费网站 | 午夜免费在线电影 | 精品91| 91一区二区三区 | h片在线观看网站 | 久久99深爱久久99精品 | 久久久久国产一区二区三区 | av毛片| 亚洲免费人成在线视频观看 | www.日韩系列 | 久久久久久99 | 亚洲精品一 | 狠狠色综合久久丁香婷婷 | 九九九视频在线观看 | 日韩电影中文字幕 | 国产高清精品在线 | 国产精品一区一区三区 | 国产欧美日韩综合精品一 | 午夜精品网站 | 国产精品91视频 | 日本亚洲一区二区 | 亚洲一区二区久久久 | 一区二区三区视频在线免费观看 | 伊人精品一区二区三区 | 播放一级黄色片 | 性高湖久久久久久久久aaaaa | 日韩色视频 | 午夜在线小视频 |