@myparcel/delivery-options
MyParcel delivery options module for checkouts.
Last updated a month ago by myparcel-nl .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @myparcel/delivery-options 
SYNC missed versions from official npm registry.

MyParcel Delivery Options

Build Status Coverage Status

Introduction

This is the MyParcel delivery options module for use in any e-commerce platform's checkout. It's used to show your customers the possible delivery and/or pickup options for their location, based on your settings. It only has the bare minimum css styling so it should integrate with the design of your webshop easily.

screenshot screenshot screenshot screenshot screenshot

Browser support

This app is written in Vue.js, it supports IE9 and up.

Example

An example of the delivery options functionality can be found in our sandbox. Here you can try out every combination of settings and copy the code for use in your project.

Installation

  1. Clone the repository or download the latest package from releases.
  2. Run the following commands:
   $ npm i
   $ npm run build
  1. Include dist/myparcel.js in your project.
  2. Place <div id="myparcel-delivery-options"></div> in your HTML.
  3. Follow the usage instructions.
  4. The delivery options will be rendered inside the div created in step 4.

Usage

All options

window.MyParcelConfig = {
  config: {
    platform: 'belgie',
    carriers: ['bpost', 'dpd'],

    // All settings below can be overridden per carrier via the carrierSettings object
 
    // The price for each option
    priceMorningDelivery: 7.95,
    priceStandardDelivery: 5.85,
    priceEveningDelivery: 6.25,
    priceSignature: 0.35,
    priceOnlyRecipient: 0.30,
    pricePickup: 5.85,
    
    // Shipment options
    allowSaturdayDelivery: true,
    allowPickupLocations: true,
    allowSignature: true,
    
    // Other settings
    dropOffDays: '1;2;3;4;5;6',
    cutoffTime: '15:00',
    deliveryDaysWindow: 4,
    dropOffDelay: 1,
    
    // Use this object to override settings per carrier.   
    carrierSettings: {
      bpost: {
        deliveryDaysWindow: 7,
      },
      dpd: {},
    },
  },
  strings: {
    wrongPostalCodeCity: 'Zaterdaglevering',
    saturdayDeliveryTitle: 'Combinatie postcode/plaats onbekend',
    
    // Address strings
    city: 'Plaats',
    postcode: 'Postcode',
    houseNumber: 'Huisnummer',
    addressNotFound: 'Adresgegevens niet ingevuld',

    // Delivery moment titles. 
    // If any of these is not set, the delivery time will be visible instead.
    deliveryEveningTitle: 'Avondlevering',
    deliveryMorningTitle: 'Ochtendlevering',
    deliveryStandardTitle: 'Standaardlevering',
    
    deliveryTitle: 'Bezorgen op',
    pickupTitle: 'Afhalen op locatie',

    // Shipment options
    onlyRecipientTitle: 'Home address only',
    signatureTitle: 'Handtekening',

    pickUpFrom: 'Afhalen vanaf',
    openingHours: 'Openingstijden',

    // Other strings
    closed: 'Gesloten',
    free: 'Gratis',
    from: 'Vanaf',
    loadMore: 'Laad meer',
    retry: 'Opnieuw',
  }, 
  address: {
    cc: 'BE',
    city: 'Antwerpen',
    postalCode: '2000',
  }
};

When there is no title set for deliveryMorningTitle, deliveryStandardTitle or deliveryEveningTitle , the delivery time will automatically be visible. For more in-depth explanation of each config item and string and an interactive demo please see our sandbox

To get the object with the selected options from the delivery options do the following:

const data = document.querySelector('#mypa-input').value;
const obj = CODE_FORMAT_JSON.parse(data);

// `obj` will be something like this:
// {
//   "delivery": "deliver", 
//   "deliveryDate": "8-8-2019", 
//   "deliveryMoment": "standard", 
//   "shipmentOptions": {signature: true, only_recipient: false}
// }

Examples

These examples assume you've already loaded the delivery options in your page. See Installation if you haven't. You have to provide a configuration file in the following format as window.MyParcelConfig and initialize the delivery options with an event.

Setting up the configuration

This is an example.

window.MyParcelConfig = {
  config: {
    platform: 'belgie', // REQUIRED!
    carrierSettings: {
      bpost: {
        allowDeliveryOptions: true,
        allowPickupLocations: true,
      },
      dpd: {
        allowDeliveryOptions: true,
        allowPickupLocations: true,
      },
    },
  },
  address: {
    cc: 'BE',
    city: 'Antwerpen',
    postalCode: '2000',
  },
};

// Trigger this event on the document to tell the delivery options to update.
// Usually only needed on initializing and when the address changes.
document.dispatchEvent(new Event('myparcel_update_delivery_options'));

Getting the address from your environment

  1. Set up the link between the address fields and the delivery options:
    /**
     * Get data from form fields and put it in the global MyParcelConfig.
     */
    function updateAddress() {
      window.MyParcelConfig.address = {
        cc: document.querySelector('#country').value,
        postalCode: document.querySelector('#house_number').value,
        number: document.querySelector('#postcode').value,
        city: document.querySelector('#address_1').value,
      };
    
      /* 
       * Send the event that tells the delivery options module to reload data. 
       */
      document.dispatchEvent(new Event('myparcel_update_delivery_options'));
    
      // IE9–11 compatible example
      var event = document.createEvent('HTMLEvents');
      event.initEvent('myparcel_update_delivery_options', true, false);
      document.querySelector('body').dispatchEvent(event);
    }
    
  2. Add event listeners to each address field to execute this function:
    // ES6 example, use var for older environments.
    const addressFields = [
      '<Country field selector>',
      '<Postal code field selector>',
      '<Address line 1 field selector>',
    ];
    
    addressFields.forEach((field) => {
      document.querySelector(field).addEventListener('change', updateAddress);
    });
    
  3. Now, when an user changes the value in any of the fields set in addressFields the window.MyParcelConfig will be updated and the delivery options module will receive the event that tells it to update. The delivery options will reload and fetch the available options for the new address.

Usage in forms

You'll often want to use the delivery options module in a checkout form in your webshop software. Below are some things to keep in mind, but if you're interested in doing this you should checkout our Magento2 and WooCommerce plugins locally and read through these implementations. You can find the best files to get started with in Integration examples. We also recommend you join our Slack support channel to get (fast!) answers to any questions you might have.

  1. Follow the steps in Installation and copy node_modules/@myparcel/delivery-options/dist/myparcel.js to your js folder.
  2. The things you'll need to do :
    • Have either a window.MyParcelConfig or dispatch a CustomEvent with the settings you want in the page where you're loading the delivery options.
    • Send events to the delivery options telling it when to update or rerender.
    • Listen to the delivery options' events to update your application according to the changes.
    • Attach the output data to the order that is being created.

Integration examples

Attach data to an order using a hidden input

In WooCommerce and Magento 2 we injected a hidden text input inside the checkout <form> elements to hold this data and automatically pass it to the $_POST variable.

// Listen to the CustomEvent the delivery option sends back out once its data has been updated
document.addEventListener('myparcel_updated_delivery_options', (event) => {
  myHiddenInput.value = JSON.stringify(event.detail);

  // And now trigger updating the checkout of whatever platform you're using.
  // WooCommerce example:
  document.dispatchEvent(new Event('update_checkout'));

  // In Magento 2 we would update quote.shippingMethod here to trigger the changes.
});
WooCommerce

Files that can help you get started:

Magento 2

Files that can help you get started:

Note: It's more complex in Magento 2 because of the way shipping methods work.

Contribute

  1. Clone this repository
  2. Run npm install
  3. Make your changes conforming to the existing code style, we recommend enabling ESLint and using our configuration to make this easier.
  4. Test the new code using npm run serve
  5. Create a pull request!

Support

If you're experiencing trouble with the implementation we're ready to help you out! Please reach out to us via support@sendmyparcel.be or join our support community on Slack.

Current Tags

  • 3.3.0                                ...           latest (a month ago)

2 Versions

  • 3.3.0                                ...           a month ago
  • 3.2.0                                ...           2 months ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 13
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |