Jump to content
thirty bees forum

[solved with paid module] Customization with one button ; Remove "save" button from customization.


Recommended Posts

Posted (edited)

I make a lot of mistakes; I might have messed-up something obvious like picking the wrong file.

I paid a Prestashop developer to try his quick fix on Thirtybees, adapted nemops.com/save-prestashop-custom-fields-add-to-cart , page one for text. I make it look complicated by trying to spot the difference between the files here and the Nemops ones. There are only 4 files to replace, saved on follow-ups to this post.

It worked for a bit but doesn't update when the code changes for bug fixing; it has stopped working after a month or two, with customizations no longer added to the cart automatically, even if I re-install the four files and re-enable the blockcart module. So the rest of this is redundant but left for reference.

I also tried onchange="javascript:this.form.submit();"  on the input form as it says here:
https://www.prestashop.com/forums/topic/76874-how-to-remove-save-button-for-customized-fields/page/3/?tab=comments#comment-2429519 . Last time I tried, there was a caching problem I sippose; it tried to sell the last customers' order to the current customer. This time I find that the quantity changes to zero if I update the customization - something to do with this code
https://forum.thirtybees.com/topic/1325-1325/customized-products-bug-in-cart/

(I tried searching "Prestashop" and "PHP "on freelancer sites - then asking one freelancer at a time if they can help, so they have time to think, and adding a link to the source code on github so they can have a look)

Edited by veganline
make a lot of mistakes / doesn't work any more
Posted (edited)

ajax-cart.js replaces these, or the second one at least
modules/blockcart/ajax-cart.js
theme/niara/js/modules/blockcart/ajax-cart.js

-----------------------------------------------------comparisons

The Thirtybees file says at about line 179
// add a product in the cart via ajax
Line 180 is the unchanged, then there are a new 25 lines, identical to the ones in the Nemops.com explanation

Quote
if(addedFromProductPage && $('#customizationForm').length > 0)
{
$('#quantityBackup').val($('#quantitywanted').val());
customAction = $('#customizationForm').attr('action');
$('body select[id^="group
"]').each(function() {
customAction = customAction.replace(new RegExp(this.id + '=\d+'), this.id +'=' + this.value);
});
// ajax to product page with custom action
var customization_entries = $('#customizationForm').serialize();
$.ajax({
type: 'POST',
data: customization_entries+ '&ajax=1',
dataType: 'json',
url: customAction,
success: function(data){
if(typeof(data.errors) !== 'undefined')
{
alert('Error while saving customization data');
return;
}
}
})
}
Edited by veganline
bit on the end / link to github version / override path
Posted (edited)

[tools.js]

replaces
js/
tools.js

-----------------------------------------------------comparisons; site updates
CURRENCY
The Thirtybees tools.js file on about line 116 goes like this:
___// format number as an USD currency in specific locale, and then replace USD symbol with currencySign___
and continues till var blank = ''; about line 127 which is the same again in both files.

The hack version line 116 begins and differs till about line 127
___var formattedCurrency = price.toLocaleString(locale, {___

FUNCTION CHECK CUSTOMIZATIONS HACKED VERSION LINE 377

function writeBookmarkLinkObject(url, title, insert) {
  if (window.sidebar || window.external) {
    return ('<a href="javascript:addBookmark(\'' + escape(url) + '\', \'' + removeQuotes(title) + '\')">' + insert + '</a>');
  } else if (window.opera && window.print) {
    return ('<a rel="sidebar" href="' + escape(url) + '" title="' + removeQuotes(title) + '">' + insert + '</a>');
  }
  return ('');
}

function checkCustomizations()
{
    var pattern = new RegExp(' ?filled ?');
 
    if (typeof customizationFields != 'undefined')
        for (var i = 0; i < customizationFields.length; i++)
        {
            /* If the field is required and empty then we abort */
           if (parseInt(customizationFields[1]) == 1 && ($('#' + customizationFields[0]).val() == ''))
    return false;
        }
    return true;
}

FUNCTION CHECK CUSTOMIZATIONS THIRTYBEES VERSION LINE 385

function checkCustomizations() {
  var pattern = new RegExp(' ?filled ?');

  if (typeof customizationFields != 'undefined') {
    for (var i = 0; i < customizationFields.length; i++) {
      /* If the field is required and empty then we abort */
      if (parseInt(customizationFields[1]) == 1 && ($('#' + customizationFields[0]).html() == '' || $('#' + customizationFields[0]).text() != $('#' + customizationFields[0]).val()) && !pattern.test($('#' + customizationFields[0]).attr('class'))) {
        return false;
      }
    }
  }
  return true;
}

 

Edited by veganline
editing product.tpl - wish I could link to a guide
Posted (edited)

ProductController.php
replaces
/controllers/front/ProductController.php
and longer term...
/override/controllers/front/ProductController.php

-----------------------------------------------------comparisons

public function postProcess()
{
    if (Tools::getValue('ajax') && Tools::isSubmit('submitCustomizedDatas'))
    {
        if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()]))
        {
            $this->context->cart->add();
            $this->context->cookie->id_cart = (int)$this->context->cart->id;
        }
 
        $this->pictureUpload();
        $this->textRecord();
        $this->formTargetFormat();
 
        if($this->errors)
        {
            $error_list = implode('; ', $this->errors);
            die(Tools::jsonEncode(array('errors' => $error_list)));
        } else
            die(Tools::jsonEncode(array('success' => true)));
             
    }
}
Edited by veganline
spacing / nemops comparison
Posted (edited)

product.tpl 

replaces
/themes/niara/product.tpl

-----------------------------------------------------comparisons ; editing
This isn't needed for testing, but it works nicely if the other three files work
Move the Customization form to just above the Add to Cart form on the product template.
Conceal the surplus "save" button. 

The "If..." code for customization has two small changes to hide the buttons.
This is one blocked-out.

  Quote

<!-- {l s='After saving your customized product, remember to add it to your cart.'}--> 

This one has some inline styling to hide it.

  Quote

<button class="btn btn-lg btn-success" name="saveCustomization" style="display:none;">

About moving customization. It's a loop of code within a form.
 
It begins

  Quote
{if isset($product) && $product->customizable}    
   

and ends

  Quote

</div>

{/foreach}

</div>

</section>

{/if}


In my case I used a <select...> </select> form instead of <textarea...> <textarea> and that worked. So I could have belt lengths selected like an attribute, but without the software counting the stock at all, as it does with other attributes on my site like shoe sizes. I can cut belts to length, but I can't stretch or shrink shoes.

-----------------------------------------------------note about editing

I'd like to find a guide to editing the product.tpl file, and link to it.
Meanwhile -
It is hard to spot the end of the loop of code that goes 
{if isset($product) && $product->customizable} {/if

There are two <form...></form> loops - hence two buttons - and they can't overlap. There are layers of {if}..{if} loops that need to be identified so you don't cut the toes off.
It is hard to spot where the loop can be moved to and still work.
Even on a text editor that indents each sub-loop, as github does here.

The text editor built-in to Cpanel is good. It can condense a long loop of code into two lines, which are easy to move. You have to guess from the indentations where they can be moved to. Notepad++ is an alternative.

Edited 14 hours ago by veganline
editing product.tpl - wish I could link to a guide

Edited by veganline
made template the last post
Posted

Surely this could be implemented without core files modification.

- The changes to ProductController are fine (from backwards compatibility point of view), as it's just addition. However, I think it's not necessary, as there is already a functionality that saves the customization -- we could simply call this existing one using ajax.

- The change to tools.js is more severe, as it might impact other themes that does not implement this 'auto save' functionality. I'm sure we could maintain the original tools.js, and add some more preparation inside theme-specific ajax-cart.js

If we could do that, than this would be theme-only change, with no side effects or compatibility problems

  • Like 1
Posted (edited)

That sounds great for what I need, which is something I can maintain without understanding it.
I don't need the ajax cart turned on either, but maybe other people prefer it.
----------------added 3/4/20
I once tried writing a javascript to do the equivalent of pressing "save customization" when "order" is pressed. It was my first script - inline Javascript - and the orders got cached. If someone is better at writing scripts than me, maybe they can solve the problem.

Edited by veganline
added 3/4/20
Posted
On 4/2/2020 at 1:31 PM, veganline said:

I also tried this method
https://www.prestashop.com/forums/topic/76874-how-to-remove-save-button-for-customized-fields/page/3/?tab=comments#comment-2429519 but don't know how to stop the inline javascript caching and selling the last customer's order to the next customer

I tried as well, it's a very dirty solution. When clicking on 'Add to cart' with a forgotten customization save, it doesn't add the product to the cart, but does this save instead. Along the way it forgets wanted quantity, then reloads the page. This may get rid of unsaved customizations, but also get rid of the entire customer.

Getting rid of caching would be a matter of removing server side caching, including Cloudflare. Always a good idea, because saving & adding to the cart as designed uses the exact same pages, so one likely see the same caching issue.

Posted
On 4/2/2020 at 7:44 PM, datakick said:

- The changes to ProductController are fine (from backwards compatibility point of view), as it's just addition. However, I think it's not necessary, as there is already a functionality that saves the customization -- we could simply call this existing one using ajax.

If (Front)ProductController would support Ajax: yes, absolutely. Unfortunately it doesn't. One whould have to request the regular page by Ajax, then extract the needed return data from this huge blob of HTML coming back.

Posted (edited)

paid help welcome.🙂 
My customers don't need to order more than one belt at a time; I take out that part of the product template. Don't know if that makes fixes easier.

Edited by veganline
last 2 lines
Posted

Looking for work-arounds, does anyone know a good module for this?
I see a simple solution on https://www.kakkupaperi.fi/en/cake-toppers/the-moomins/the-moomins-moomin-lovely-tree-edible-cake-topper.html#/2-paper_type-vanilla_paper/4-size-a5/21-shape-rectangle/57-background_color-yellow

Two buttons.
"save customization" displays the new text in bold, for re-assurance, until
"add to cart" is pressed.

This is better than the default which refreshes the page and looses the customisation until the customer might see it on the layer cart a couple of clicks later.

  • 3 weeks later...

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...