Woocommerce and Vend integration

Hi all,
I’m trying to create a connection between Woocommerce and Vend. Two things need to happen. I need to create a sale and create a customer.

I can create the customer no problem.
I can’t create the sale with the correct sale or with the customer that I have created.

I have already got the products from the order but I am having trouble mapping the product to the vend product. I am using SKU to map them. I am trying to check if there is a SKU from Woccommerce and a SKU from Vend that match, and then I need to create a sale of the Vend product_id to relates to the Vend SKU.

This is the workflow:

  1. I watch the order
  2. I then use the Iterator to get the (order_id) and the Aggregator to create an array.
  3. Then from the router if the user doesn’t exist in Vend I create a customer. after which I set a variable to call later in the create sale.
  4. Then I list the products from vend (200 or so) so that I can search for the correct SKU.
  5. I Use the aggregator to create an array of the product ID, SKU, and bundle position from each product bundle.

Then I’m stuck.

I have tried an if statement on the get product to compare SKUs.
{{if(7.array.lineItems.sku = 12.array.sku; 12.array.id; “nomatch”)}}

(If woocommerce lineitem sku = one of the skus from vend, then return the product ID of the Vend product, else return nomatch.) I always get no match.

image

image

I don’t know if I should use map(), switch(), if() or a completely different function to be able to achieve this.

I basically need to map the skus in the woocommerce order against the corresponding skus from Vend. Then return the product IDs of those vend skus for the sale.

In the sale I often get a Missing value of required parameter ‘product_id’. which I think just means that it can’t find anything?

Any help would be greatly appreciated.

Cheers,
Wade.

Just an update on this for anyone wanting a solution to this as well. I have found a workaround that is working now.

My case was a little different as I have a booking system on the site, and don’t want to sync all products with Vend, so I needed a way to only map the products that I wanted. It is a bit more manual than connecting with a plugin or using Vend’s connection directly, but it works for what I need.

Firstly I created a Vend ID for the product. I manually copy and paste that from Vend (You could do something else to get this automatically but I didn’t).

/* -------- MAP ADDON TO ATTRIBUTE FOR VEND */
// Hook to add custom field in WooCommerce product general settings
function add_vend_id_custom_field() {
    echo '<div class="options_group">';
    woocommerce_wp_text_input(
        array(
            'id' => 'vend_id',
            'label' => __('Vend ID', 'woocommerce'),
            'placeholder' => __('Enter Vend ID here', 'woocommerce'),
            'desc_tip' => true,
            'description' => __('Enter the Vend ID for this product. This will be visible only in the admin product page.', 'woocommerce'),
        )
    );
    echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'add_vend_id_custom_field');

// Save the custom field value
function save_vend_id_custom_field($product_id) {
    $vend_id = isset($_POST['vend_id']) ? sanitize_text_field($_POST['vend_id']) : '';
    update_post_meta($product_id, 'vend_id', $vend_id);
}
add_action('woocommerce_process_product_meta', 'save_vend_id_custom_field');

// Save vend_id as order item meta
function add_custom_hidden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
    $product = $values['data'];
    $vend_id = get_post_meta($product->get_id(), 'vend_id', true );

    if( $vend_id ) {
        $item->update_meta_data( '_vend_id', $vend_id );
    }
}
add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hidden_order_item_meta_data', 20, 4 );

// Display vend_id in admin order item meta
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
    if( $meta->key === '_vend_id' && is_admin() ) {
        $display_key = __("Vend ID", "woocommerce" );
    }
    return $display_key;
}
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );

Then I created a Vend Customer ID in wordpress:

// Add custom field to user profile
function add_vendwoo_customer_id_field($user) {
    ?>
    <h3><?php _e('VEND Customer ID', 'woocommerce'); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="vendwoo_customer_id"><?php _e('VEND Customer ID', 'woocommerce'); ?></label></th>
            <td>
                <input type="text" name="vendwoo_customer_id" id="vendwoo_customer_id" value="<?php echo esc_attr(get_user_meta($user->ID, 'vendwoo_customer_id', true)); ?>" class="regular-text" /><br />
                <span class="description"><?php _e('Enter the VEND Customer ID.', 'woocommerce'); ?></span>
            </td>
        </tr>
    </table>
    <?php
}
add_action('show_user_profile', 'add_vendwoo_customer_id_field');
add_action('edit_user_profile', 'add_vendwoo_customer_id_field');

// Save custom field to user meta
function save_vendwoo_customer_id_field($user_id) {
    if (current_user_can('edit_user', $user_id)) {
        update_user_meta($user_id, 'vendwoo_customer_id', sanitize_text_field($_POST['vendwoo_customer_id']));
    }
}
add_action('personal_options_update', 'save_vendwoo_customer_id_field');
add_action('edit_user_profile_update', 'save_vendwoo_customer_id_field');

// Add vendwoo_customer_id as order meta
function add_order_meta_vendwoo_customer_id($order_id) {
    $order = wc_get_order($order_id);
    $user_id = $order->get_user_id();
    $vendwoo_customer_id = get_user_meta($user_id, 'vendwoo_customer_id', true);

    $order->update_meta_data('vendwoo_customer_id', $vendwoo_customer_id); // Update the order meta with vendwoo_customer_id
    $order->save(); // Save the order
}
add_action('woocommerce_checkout_update_order_meta', 'add_order_meta_vendwoo_customer_id');

This allows me to link the woocommerce user with the Vend user via Make.

My Make flow looks like this now:

  1. Watch orders
  2. Iterator and Aggregator to get OrderID array
  3. I force Wordpress users to login to purchase so I create a customer in Vend using the woocommerce user details. I first set a variable of the Wordpress user vendwoo_customer_id.

If it is empty, create a new customer in Vend. Set a variable called customer_id, and then update the wordpress user with that variable. Now if the same user purchases again it won’t recreate them in Vend as the vendwoo_customer_id will be populated.

  1. I get those two customer variables on the other line so that I can call them in the create sale at the end.

  2. I set the vend product id variable that I have manually entered into woocommerce. This then means I don’t have to match via sku as I wanted originally. (More manual, but it works)

  3. Create a sale by making sure I have the correct customer id:
    image

and then by getting the product id from the variable I created earlier:

Hope it helps any newbie like me trying to map data between the two.

Hi @Wade welcome to the community :blob_wave:

Wow, truly awesome work getting this up and running! :clap:

Thanks a lot for keeping the community in mind and for stepping back in here and sharing your final setup in such detail. This is super valuable and I’m sure it will help many others looking for similar information in the future. :pray:

1 Like