Set delivery method based on the items added to the cart

We can set delivery method based on the items added to the cart also based on the subscribed user or address with residential

// Get the shipping configuration and profile
let shippingConfig = Configuration.ShippingItems.config;
let profile = ProfileModel.getInstance();
let isSpecialSubscriber = profile.attributes.customfields.find(field => field.name === 'yourCustomFieldName')?.value === 'T'; // Modify field name as needed


// Check if the address is residential
let isResidential = this.model.get('addresses')?.models?.[0]?.get('isresidential') === 'T';


// Define the code to find the appropriate shipping method
let codeToFind = '';


if (isSpecialSubscriber) {
    // Define conditions when the user is a special subscriber
    if (someCondition) {
        codeToFind = 'CODE1';  // Set the appropriate code
    } else if (otherCondition) {
        codeToFind = 'CODE2';  // Set another code
    } else {
        codeToFind = isResidential ? 'CODE3' : 'CODE4';  // Set based on residential status
    }
} else {
    // Define conditions for general users
    if (someCondition) {
        codeToFind = 'GENCODE1';  // Set the appropriate code for general users
    } else if (otherCondition) {
        codeToFind = 'GENCODE2';  // Set another general code
    } else {
        codeToFind = isResidential ? 'GENCODE3' : 'GENCODE4';  // Set based on residential status
    }
}


// Find the matching shipping configuration based on the code
let matchingConfig = shippingConfig.find(config => config.code === codeToFind);


// Set the shipping method if a matching configuration is found
if (matchingConfig) {
    this.model.setShipMethod(matchingConfig.internalId);
} else {
    console.error('No matching shipping method found for code:', codeToFind);
}

Leave a comment

Your email address will not be published. Required fields are marked *