【问题标题】:Woocommerce: Set order status to complete when QR code (containing order id) is scannedWoocommerce:将订单状态设置为在扫描 QR 码(包含订单 ID)时完成
【发布时间】:2021-03-07 17:11:14
【问题描述】:

如何在 Woocommerce 管理面板的某处显示一个简单的表单(类似于搜索表单)(可能作为单独的菜单条目),我可以在其中输入订单号并设置该订单(使用该订单号) 来完成,而无需重新加载页面。

为什么我需要这个:在我的运输标签上有一个包含订单号的二维码。当我扫描该二维码时,它应该将该订单设置为完成(并因此通知客户订单现已完成)。如前所述,表单应该可以工作,而无需在每次扫描后重新加载页面,这样我就可以连续扫描订单。

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce barcode-scanner


    【解决方案1】:

    关于不重新加载页面的表单提交,您必须使用 AJAX 提交值。 jQuery 有 $.ajax() 和 $.post() 方法,您可以使用它们来实现此目的。

    示例(复制自Codex):

    jQuery(document).ready(function($) {
        var data = {
            'action': 'my_action',
            'whatever': ajax_object.we_value      // We pass php values differently!
        };
        // We can also pass the url value separately from ajaxurl for front end AJAX implementations
        jQuery.post(ajax_object.ajax_url, data, function(response) {
            alert('Got this from the server: ' + response);
        });
    });
    

    数据提交后,需要使用wp_ajax_(action)来处理请求。

    示例(复制自Codex):

    <?php
    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    function my_enqueue($hook) {
        if( 'index.php' != $hook ) {
        // Only applies to dashboard panel
        return;
        }
            
        wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
    
        // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
        wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    }
    
    // Same handler function...
    add_action( 'wp_ajax_my_action', 'my_action' );
    function my_action() {
        global $wpdb;
        $whatever = intval( $_POST['whatever'] );
        $whatever += 10;
            echo $whatever;
        wp_die();
    }
    

    你可以从官方文档中得到详细的解释:https://codex.wordpress.org/AJAX_in_Plugins

    而且,您可以使用update_status() 函数更新订单的订单状态。示例:

    $order_id = ''; //-- received from AJAX request
    $order = new WC_Order($order_id);
    
    if (!empty($order)) {
     $order->update_status( 'completed' );
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2018-09-10
      • 2017-05-24
      • 2020-06-21
      • 2018-02-20
      • 2020-09-01
      相关资源
      最近更新 更多