Jump to content

marketplace module best practice to add images for new products


Recommended Posts

I'm using Prestashop version 1.6.1.18 and I installed a module that turns my web application into a marketplace for multisellers.

Sellers can add new products from the front end, but first they have to save the product in order to add an image (because of an issue with the product ID).

I want to change that function and allow sellers to add images when they're creating a new product so they can save everything at once.

I would like to know the best practice (logic) to do that.

This is the javascript function of the current method:

<script>
    function uploadNewImage()
    {
        if(kb_id_product > 0){
            var img_data = new FormData();
            $.each(img_file, function(key, value){img_data.append('file['+key+']', value);});

            img_data.append('ajax', true);
            img_data.append('id_product', kb_id_product);
            img_data.append('method', 'addProductImage');
            img_data.append('token', static_token);
            img_data.append('legend', $('input[name="legend"]').val());

            $.ajax({
                type: 'POST',
                headers: { "cache-control": "no-cache"},
                processData: false,
                contentType: false,
                url: kb_current_request + ((kb_current_request.indexOf('?') < 0) ? '?' : '&')+'rand=' + new Date().getTime(),
                async: true,
                cache: false,
                dataType : "json",
                data: img_data,
                beforeSend: function() {
                    $('#img-upload-error').hide();
                    $('#img-upload-error').removeClass('kbalert-danger');
                    $('#img-upload-error').removeClass('kbalert-success');
                    $('#pro-img-upload-section #star-upload-btn').attr('disabled', 'disabled');
                    $('#pro-img-upload-section #remove-upload-btn').attr('disabled', 'disabled');
                    $('#uploading-progress').show();
                },
                complete: function() {
                    $('#pro-img-upload-section #star-upload-btn').removeAttr('disabled');
                    $('#pro-img-upload-section #remove-upload-btn').removeAttr('disabled');
                },
                success: function(json)
                {
                    var mesg = '';
                    var has_error = false;
                    if(json.file != undefined && json.file.length > 0){
                        for(var i in json.file){
                            if(json.file[i].error != ''){
                                mesg += json.file[i].error+'<br>';
                                has_error = true;
                            }else{
                                mesg = json.file[i].status;
                                var legend = '';
                                if(json.file[i].legend != null || json.file[i].legend != ''){
                                    legend = json.file[i].legend;
                                }
                                displayNewLineImage(json.file[i].id, json.file[i].path, json.file[i].position, json.file[i].cover, legend);

                                //Update combination form
                                if(typeof('updateCombinationFormImage') === typeof(Function))
                                    updateCombinationFormImage(json.file[i].id, json.file[i].path, legend);
                            }
                        }
                    }

                    if(has_error){
                        $('#img-upload-error').html('<i class="icon-exclamation-sign"></i> '+mesg);
                        $('#img-upload-error').addClass('kbalert-danger');
                    }else{
                        $('#img-upload-error').html('<i class="icon-check"></i> '+mesg);
                        $('#img-upload-error').addClass('kbalert-success');
                        $('#img-upload-info-block').hide();
                        $('input[name="legend"]').val('');
                    }
                    $('#img-upload-error').show();
                    setTimeout(function(){$('#img-upload-error').hide();}, message_delay);
                    $('#uploading-progress').hide();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    jAlert('Technical Error: Contact to support');
                    $('#uploading-progress').hide();
                }
            });    
        }else{
            return jAlert('Before adding images, you must have to save this product.');
        }
    }
</script>
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...