問題描述
在 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 index1
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模板網!