根据产品 ID 向 WooCommerce "my account" 订单添加操作按钮

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, 因为如果您的代码中不满足某些条件,则永远不会达到 return

  • 要将订单中的多个产品 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 );