From e43dc67d420deb359b47d28331718fcc1eaadb84 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 7 Mar 2018 11:10:21 +0100 Subject: [PATCH] Version 0.5.0: optimized bulk create consignments --- include/controllers/ShopOrderController.php | 43 ++++++++++++++++----- woocommerce-cargonizer.php | 2 +- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/controllers/ShopOrderController.php b/include/controllers/ShopOrderController.php index 407eea2..3351116 100644 --- a/include/controllers/ShopOrderController.php +++ b/include/controllers/ShopOrderController.php @@ -13,20 +13,43 @@ function __construct( ){ add_action( 'save_post', array($this, 'saveConsignmentSettgings'), 10, 1 ); add_action( 'save_post', array($this, 'saveConsignment'), 20, 1 ); add_action( 'init', array($this, 'resetConsignment') , 10, 2 ); - add_filter( 'bulk_actions-edit-shop_order', array($this, 'filterBulkActions' ) ); - add_action( 'init', array($this, 'bulkCreateConsigments' ) ); + add_filter( 'bulk_actions-edit-shop_order', array($this, 'filterBulkActions') ); + add_action( 'handle_bulk_actions-edit-shop_order', array($this, 'bulkCreateConsigments' ), 10, 3 ); + add_action( 'admin_notices', array($this, 'setBulkAdminNotice') ); } - function bulkCreateConsigments(){ - if ( _is($_REQUEST, 'action') == 'create_consignment' ){ - if ( isset($_REQUEST['post']) && is_array($_REQUEST['post']) && !empty($_REQUEST['post']) ) { - foreach ($_REQUEST['post'] as $key => $order_id) { - $_REQUEST['post_ID'] = $order_id; - $this->saveConsignment($order_id, $force=true, $create=false); - } + function setBulkAdminNotice() { + // _log('ShopOrderController::setBulkAdminNotice()'); + if ( isset($_REQUEST['bulk_created_consignments']) && is_numeric($_REQUEST['bulk_created_consignments']) ) { + printf( '
%s %s
', $_REQUEST['bulk_created_consignments'], __('consignments created', 'wc-cargonizer') ); + } + } + + + function bulkCreateConsigments( $redirect_to, $doaction, $post_ids ){ + _log('ShopOrderController::bulkCreateConsigments()'); + _log($doaction); + _log($post_ids); + + if ( $doaction !== 'create_consignment' ) { + return $redirect_to; + } + + if ( is_array($post_ids) && !empty($post_ids) ) { + foreach ($post_ids as $key => $order_id) { + $_REQUEST['post_ID'] = $order_id; + $this->saveConsignment($order_id, $force=true, $create=false); } } + else{ + _log('no post ids checked'); + } + + $redirect_to = add_query_arg( 'bulk_created_consignments', count( $post_ids ), $redirect_to ); + + + return $redirect_to; } @@ -88,6 +111,7 @@ function saveConsignmentSettgings( $post_id ){ function saveConsignment( $post_id, $force=false, $create=true ){ + if ( !isset($_REQUEST['post_ID']) or $_REQUEST['post_ID'] != $post_id ){ return false; } @@ -105,6 +129,7 @@ function saveConsignment( $post_id, $force=false, $create=true ){ _log('... is ready'); if ( $cid = Consignment::createOrUpdate( $Order, $recurring=false ) ){ _log('consignment created/updated: '.$cid); + if ( $create ){ _log('create consignment now'); $result = ConsignmentController::createConsignment($cid); diff --git a/woocommerce-cargonizer.php b/woocommerce-cargonizer.php index cd8d445..14c1ba0 100644 --- a/woocommerce-cargonizer.php +++ b/woocommerce-cargonizer.php @@ -2,7 +2,7 @@ /*woo Plugin Name: Woocommerce Cargonizer Description: -Version: 0.4.9 +Version: 0.5.0 Author: Mediebruket AS Author URI: http://mediebruket.no */