发货地址的管理员通知在"处理"订单

时间:2017-03-08 21:47:04

标签: php wordpress woocommerce shipping orders

我拥有/经营一家WooCommerce商店,我正在寻找一种方法来设置商店通知,当多个订单来自同一客户,运送到同一地址时。

我很幸运能够拥有大量的回头客。他们将从我的商店订购,让我们说星期一,并支付他们的物品的运费和&然后在星期二订购(在我能够发出第一个订单之前),所以我尝试将第二个产品包含在第一个订单中,以便" SAVE"在运输上。但是,如果我注意到客户的送货地址并捆绑物品,我只能节省运费。

我希望我的网站在这方面更有效率,并检查" OPEN"或"处理"订单具有匹配的送货地址,并在我打开其中一个订单时弹出提醒...
这样的事情可能吗?

我搜索过搜索过的搜索结果并没有...我不完全确定从哪里开始...

这是否可以在functions.php文件中使用自定义脚本?
是否有可以帮助实现此目的的插件?

非常感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

  

适用于WooCommerce版本2.5.x和2.6.x

     

对于WOOCOMMERCE VERSION 3.0+,请参阅 startsWith()

完全可以在 woocommerce_admin_order_data_after_order_details 操作中使用自定义钩子函数,其中您必须在数组中定义 订单状态为您的“公开订单”。

以下是这个功能齐全且经过测试的代码:

add_action( 'woocommerce_admin_order_data_after_order_details', 'same_shipping_open_order', 10, 1 );
function same_shipping_open_order( $order ){

    // Define HERE, in the array, your "OPEN" orders statuses
    $open_order_statuses = array('wc-pending','wc-processing','wc-on-hold');

    // Initialising variables
    $matching = false;

    // Get the shipping 'address_1' & 'postcode' fields for the CURRENT ORDER
    $order_ship_address1 = $order->shipping_address_1;
    $order_ship_postcode = $order->shipping_postcode;

    // Getting customer orders, with an open status
    $open_orders = wc_get_orders( array(
        'numberposts' => -1,
        'meta_key' => '_customer_user',
        'meta_value' => $order->get_user_id(),
        'post_type' => 'shop_order',
        'post_status' => $open_order_statuses,
        'exclude' => array($order->id),
    ) );

    // Other "Open" orders for this customer
    if( count($open_orders) != 0 ){
        // Iterating through each orders
        foreach($open_orders as $open_order){
            if( $order_ship_address1 == $open_order->shipping_address_1 && $order_ship_postcode == $open_order->shipping_postcode ){
                $matching = true; // set condition to true
                $open_order_id = $open_order->id;
                // Other orders edit url
                $order_edit_url = home_url( "/wp-admin/post.php?post=$open_order_id&action=edit/" );
                // Storing orders edit url + ID
                $results_arr[] = "<a href='$order_edit_url'>#$open_order_id</a>";
            }
        }
    }

    // If there is matching "Open" orders shipping addresss with this order
    if ( $matching ) {

        ## 0. Converting the array in a string for output
        $output_html = implode(', ', $results_arr);

        ## 1. Displaying an alert message on the order
        echo '<br clear="all"><p style="margin-top:12px !important;"><strong style="color:red;">'. __("Same Shipping on Open Orders IDs: ").'</strong><br>'.$output_html.'</p>';

        ## 2. Javascript Alert message
        ?>
        <script>
            (function($){
                alert('SAME SHIPPING ON OPEN ORDERS!');
            })(jQuery);
        </script>
        <?php
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

  

打开/编辑订单时会收到提醒信息,还会显示相关“打开订单ID和编辑链接”的文本。请参阅下面的屏幕截图:

THIS UPDATE

答案 1 :(得分:1)

  

WooCommerce 3 +的版本代码

add_action( 'woocommerce_admin_order_data_after_order_details', 'same_shipping_open_order', 10, 1 );
function same_shipping_open_order( $order ){

    // Define HERE, in the array, your "OPEN" orders statuses
    $open_order_statuses = array('wc-pending','wc-processing','wc-on-hold');

    // Initialising variables
    $matching = false;

    // Get Order data (WC 3+ compatibility)
    $order_data = $order->get_data();

    // Get the shipping 'address_1' & 'postcode' fields for the CURRENT ORDER
    $order_ship_address1 = $order_data['shipping']['address_1']; # (WC 3+ compatibility)
    $order_ship_postcode = $order_data['shipping']['postcode']; # (WC 3+ compatibility)

    // Getting customer orders, with an open status
    $open_orders = wc_get_orders( array(
        'numberposts' => -1,
        'meta_key' => '_customer_user',
        'meta_value' => $order->get_user_id(),
        'post_type' => 'shop_order',
        'post_status' => $open_order_statuses,
        'exclude' => array($order_data['id']), # (WC 3+ compatibility)
    ) );

    // Other "Open" orders for this customer
    if( count($open_orders) != 0 ){
        // Iterating through each orders
        foreach($open_orders as $open_order){

            // Get "open" Order data (WC 3+ compatibility)
            $open_order_data = $open_order->get_data();
            // Orders Id
            $open_order_id = $open_order_data['id']; 

            if( $order_ship_address1 == $open_order_data['shipping']['address_1'] && $order_ship_postcode == $open_order_data['shipping']['postcode'] ){
                // set condition to true (There is at once 1 matched order)
                $matching = true; 

                // Other orders edit url
                $order_edit_url = home_url( "/wp-admin/post.php?post=$open_order_id&action=edit/" ); 

                // Storing orders edit url + ID
                $results_arr[] = "<a href='$order_edit_url'>#$open_order_id</a>";
            }
        }
    }

    // If there is matching "Open" orders shipping addresss with this order
    if ( $matching ) {

        ## 0. Converting the array in a string for output
        $output_html = implode(', ', $results_arr);

        ## 1. Displaying an alert message on the order
        echo '<br clear="all"><p style="margin-top:12px !important;"><strong style="color:red;">'. __("Same Shipping on Open Orders IDs: ").'</strong><br>'.$output_html.'</p>';

        ## 2. Javascript Alert message
        ?>
        <script>
            (function($){
                alert('SAME SHIPPING ON OPEN ORDERS!');
            })(jQuery);
        </script>
        <?php
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码经过测试,在WooCommerce 3 +上的工作方式相同

请参阅此相关更新回答:How to get order details in WooCommerce 3.0+