簡體   English   中英

根據產品 ID 向 WooCommerce“我的帳戶”訂單添加操作按鈕

[英]Add an action button to WooCommerce “my account” orders according to product id

我有一個 WooCommerce b2b 商店,我在那里銷售不同類型的產品。 例如,可通過代碼兌換訪問的電子學習課程。 我也賣書。

我嘗試的是在“我的訂單”部分創建一個客戶操作,它允許發送我的電子學習課程的參與者數據列表。

我希望此操作僅針對課程產品而不是書籍顯示。 我試過這個,但動作仍然顯示為書籍產品

function add_my_account_order_actions( $actions, $order ) {
    foreach( $order->get_items() as $item ) {
    if ( array( $item[ 'variation_id' ] ) &&  $item[ 'variation_id' ] == (3558 or 3559 or 3560 or 3561 or 3557));  {
    $actions['tn'] = array(
        // adjust URL as needed
        'url'  => '/teilnehmer/?&order=' . $order->get_order_number(),
        'name' => __( 'Teilnehmerdaten', 'my-textdomain' ),
    );
    }
    return $actions;
    }
}
    
add_filter( 'woocommerce_my_account_my_orders_actions', 'add_my_account_order_actions', 10, 2 );

我在這里缺少什么?

提前致謝!

  • 在 foreach 循環外使用return $actions ,因為如果代碼中不滿足某些條件,則永遠不會達到返回值

  • 要將訂單中的多個產品 ID 與預定義數組進行比較,您可以使用in_array

function add_my_account_order_actions( $actions, $order ) {
    
    // Set Id's
    $product_ids = array ( 3558, 3559, 3560, 3561, 3557 );
    
    // Loop through order
    foreach( $order->get_items() as $item ) {
    
        // Product Id is in the array
        if ( in_array( $item['product_id'], $product_ids ) ) {
            $actions['tn'] = array(
                // adjust URL as needed
                'url'  => '/teilnehmer/?&order=' . $order->get_order_number(),
                'name' => __( 'Teilnehmerdaten', 'my-textdomain' ),
            );
        }
    }

    // Return
    return $actions;
}
    
add_filter( 'woocommerce_my_account_my_orders_actions', 'add_my_account_order_actions', 10, 2 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM