dpq59734 2019-05-23 09:00
浏览 91
已采纳

对于已完成状态的WooCommerce订单上的特定产品,请执行操作

IN WooCommerce, I would like to perform an action if at least one product from a list is bought and if the current order status for that product is completed.

For instance I can only verify if the product is bought:

global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$product_list = array('11', '12', '13', '14', '15','16');
$text= false;
  foreach ($product_list as $value):
    if (wc_customer_bought_product( $customer_email, $user_id, $value) ) {
        $text = true;
     }
  endforeach;
  • 写回答

1条回答 默认 最新

  • dongsigan2044 2019-05-23 09:14
    关注

    Try the following hooked function, that will be triggered each time an order gets "completed" status, checking if the current order has a product from your defined list, allowing you to perform an action:

    add_action('woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 );
    function action_on_order_status_completed( $order_id, $order ){
        // Here below your product list
        $products_ids = array('11', '12', '13', '14', '15','16');
        $found = false;
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            if ( in_array($item->product_id(), $products_ids) ) {
                $found = true;
                break;
            }
        }
    
        if ( $found ) {
            // HERE do your action
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.


    Related: How to get WooCommerce order details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab