Support » Fixing WordPress » Multiple orders

  • Good morning everyone,

    currently I am working on a new project of which I need some help with. In the back-end every user is a company, I created a seperate page where the administrator can add affiliates to every user (company).

    Now, once a company is logged in at the shop he can choose to send the products to one or multiple affiliates (every affiliate has its own address). How can I send multiple orders to these affiliates?
    Also I would like to duplicate each order, one with a code of 10 and one with a code of 90.
    This is for administration purposes. Anyone has any expertience with this?

    Thanks for the help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Sounds like a custom coded solution to me. Maybe someone else knows of additional plugins that would help, but I don’t. I’m not available to write code for you, but I can advise when the going gets tough. If you’re not able to write code, professional help is available at jobs.wordpress.net.

    Sending products to affiliates. What is the nature of this action and how is it triggered? Sending electronic product via email? Posting physical objects? Or just sending the order data to affiliates? Triggered by checkmarking affiliates, then clicking Send? Automatically when something happens, say creating a new order?

    Duplicate orders with different codes. This is possible, but seems unnecessary. Redundant data really should be avoided. There’s likely a more efficient way to handle this than duplicating orders. If you’re willing to describe the need further, I might have some alternative ideas.

    Good morning bcworkz and thank you for replying,

    currently I have created a start:
    1. In the back-end all created users will be called in a seperate page with a link to an ID. In here back-end moderators can add affiliate companies to the users.
    2. Once a user is logged in, they can choose all their added affiliates as billing addresses in the checkout page of WooCommerce.

    What should happen now?
    1. I should make the checkbox required (at least 1 should be selected).
    2. For each checkbox their should be a different order + email (different addresses, same products). Emails are sent to the same person, because he is the one ordering for all affiliates so name and email are all the same over the process, just address / zipcode / city and company name are different.

    This is all I should have in the beginning, the duplicate orders for each checkout can be done later. This is done for administration purposes. Thanks for assisting.

    I added images to:
    Image 1
    Image 2
    Image 3

    Moderator bcworkz

    (@bcworkz)

    The default WC process when a new order is placed is an order post type is created and email notification is sent to the site owner’s representative (typically a fulfillment dept. email address). The customer is not notified until the order status changes to processing. Continuing to do this isn’t a problem. You can choose to not send email to the customer in settings.

    The fulfillment email address in settings is a set it and forget it value. You can’t vary who gets this email in settings, but you can through the “woocommerce_email_get_option” filter. If the passed parameters indicate WC is getting the new order recipient email address, return the email address of the user and they will get the new order notification.

    Hook the action “woocommerce_before_checkout_process” to process the selected affiliates which are in $_POST. If there is no such data in $_POST, none were selected and you should throw an exception or some sort of error process. Ideally, you also have some JavaScript that verifies at least one affiliate is selected before the form is submitted, but you need to recheck this in PHP in case the JavaScript was disabled or bypassed.

    The affiliate selections need to be saved somewhere. The order’s post meta should work just fine. Ideally, you could simply store an array of affiliate IDs, assuming such a thing exists. This should be a consideration in how you store affiliate data in general. Hook the action “woocommerce_payment_complete” to save the selected affiliates. Also consider how you will access this data from an action hook callback for added email content.

    To insert the selected affiliate data in the email, hook one of the actions on templates/emails/admin-new-order.php and output the content. Which action you hook will determine where in the email the data appears.

    Hi bcworkz,

    sorry for the late reply, got a bit caught up.
    Your replies have helped me realy far, but I decided to change a small factor on how we gather the information. At first we used the checkbox options, well, we changed it to a dropdown selection (for each affiliate create one selection).

    We applied this to the functions.php file instead of hardcoded inside the Woocommerce templates.
    Code looks like:

    add_filter( 'woocommerce_checkout_fields' , 'dropdown' );
    			
    			function dropdown( $fields ) {
    					global $current_user;
          				get_currentuserinfo();
    					$i = 0;
    					global $wpdb;
    					$queries25 = $wpdb->prepare("SELECT * FROM kdEksID_affiliates WHERE display_name='$current_user->display_name' ORDER BY display_name+0 ASC", GID);
    					$Finished25 = $wpdb->get_results($queries25);
    			foreach ( $Finished25 as $finish25 ) {
         			$fields['billing']['dropdown-' . $i++ . ''] = array(
            		'label'     => __('Send products to affiliate '.$finish25->company_name  . '', 'woocommerce'),
        			'placeholder'   => _x('dropdown', 'placeholder', 'woocommerce'),
        			'required'  => false,
        			'class'     => array('form-row-wide'),
        			'clear'     => true,
        			'type'      => 'select',
         				'options'     => array(
            				$finish25->company_name.' - yes' => __('YES', 'woocommerce' ),
            				$finish25->company_name.' - no' => __('NO', 'woocommerce' )
            			)//end of options
         			);
    			}
    
         		return $fields;
    			}

    But how can I add this data to the order page in backend and add it to the emails being send to our client?
    Also would like to send an email for each affiliate selected as YES, how can I make this happen. Probably by disabling the standard email and create an after_submit hook?

    Thanks for the great support.

    • This reply was modified 3 years, 8 months ago by helldog2004. Reason: typo 'dropbox' to 'checkbox'
    Moderator bcworkz

    (@bcworkz)

    Disabling normal email and using some other hook is a good approach. You’d want a hook that fires after payment is confirmed, the process could still fail after a mere submit. Perhaps “woocommerce_payment_complete”. Using this approach, you would create your own email to be sent with wp_mail(). Naturally the content can be anything you want, just add your data to the message body. If you want the message to be HTML instead of the default plain text, some special handling is required.

    To display the data on the admin order screen, there’s a number of actions you could use. Which one depends on where you want the data inserted. Everything on the order screen is some sort of meta box. To add content to the main box, look at includes/admin/meta-boxes/class-wc-meta-box-order-data.php. Locate the output() function (starting line 139). Within this function are several calls to do_action(). Hook whichever action fires closest to where you want to insert your data and have your callback simply output the data.

    It’ll likely take some trial and error with HTML and CSS to get it looking right, but that’s trivial compared to everything else 🙂

    Okay, currently I recreated the select dropdown again to make it custom instead of adding it to the billing section. I am now able to get the value of a single select dropdown, but once I get a load of select dropdowns compaired with my database it stops working.

    This is how I added the select dropdown:

    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    global $current_user;
    get_currentuserinfo();
    $i = 0;
    global $wpdb;
    $queries25 = $wpdb->prepare("SELECT * FROM kdEksID_affiliates WHERE display_name='$current_user->display_name' ORDER BY display_name+0 ASC", GID);
    $Finished25 = $wpdb->get_results($queries25);
    echo '<div class="my_custom_checkout_field"><h2>' . __('SELECT SHIPPING ADDRESSES') .'</h2>';
    foreach ( $Finished25 as $finish25 ) {
    
    woocommerce_form_field( 'affiliate['.$finish25->id.']', array(
       'type' => 'select',
       'label'      => __('Send products to ' . $finish25->company_name, 'woocommerce'),
       'placeholder'   => _x('affiliate', 'placeholder', 'woocommerce'),
       'required'   => false,
       'class'      => array('form-row-wide'),
       'clear'     => true,
       'options' => array($finish25->id => 'YES', 'No ' . $finish25->company_name => 'NO'),
           ), $checkout->get_value( 'affiliate['.$finish25->id.']' ));
    }
    echo '</div>';
    }
    
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
    
       if ( ! empty( $_POST['affiliate['.$finish25->id.']'] ) ) {
           update_post_meta( $order_id, 'affiliate['.$finish25->id.']', $_POST['affiliate['.$finish25->id.']'] );
       }
    }

    As you can see all names of these select dropdowns have their own id captured from the database this is working perfectly. But now I am trying to get the data by the following code:

    <?php 	global $current_user;
    			get_currentuserinfo();
    			global $wpdb;
    			$queries26 = $wpdb->prepare("SELECT * FROM kdEksID_affiliates WHERE display_name='$current_user->display_name' ORDER BY display_name+0 ASC", GID);
    			$Finished26 = $wpdb->get_results($queries26);
    			foreach ( $Finished26 as $finish25 ) { ?>
        			<tr><th>Send to <?php echo $finish25->company_name;?>:</th><td><?php echo get_post_meta( $order->id, 'affiliate['.$finish25->id.']', true ); ?> </td></tr>
    			<?php }?>

    It is adding two rows inside my table, it also shows the two companies added for this account, but it ain’t getting the selected values from the two selected dropdowns earlier.
    How do I make this work? Got any idea?

    Moderator bcworkz

    (@bcworkz)

    What you have seems generally OK, I think there’s just a glitch somewhere, as opposed to this being the wrong approach or something. For example, where is $order->id coming from? Maybe it’s fine, but it’s not clear to me. If that’s not it, it’s probably something similar — a variable not having the value you think it does. To find it, do some basic debugging. Echo out various variables at strategic points to confirm they have the values they should.

    A side note: This wouldn’t be the cause of your trouble, but you are using $wpdb->prepare() wrong. If you check your error logs you should see a bunch of user errors to this effect. Failure to use prepare() properly can open up a security vulnerability, so it’s important to do this right.

    There must never be a normal PHP $variable in the initial string argument. The presence of such is a sure sign you’re dong it wrong. I don’t know what GID is supposed to be either. Every place you have a variable, you need a place holder like %s. Then the variables to use for the place holders are listed in order after the first string argument. It’s the same concept as sprintf(), except only string, integer, and float formats are supported. You essentially have something like this:
    $query = $wpdb->prepare("SELECT * FROM example WHERE foo = " . $variable . " ORDER BY bar;");

    which is doing it wrong. This is how it’s done:
    $query = $wpdb->prepare("SELECT * FROM example WHERE foo = %s ORDER BY bar;", $variable );

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Multiple orders’ is closed to new replies.