Prints a form where the customer can enter a discount campaign or gift card code.

Kuvaus

The tag is available in any theme view, but it is most often used in the shopping cart.

Depending on the situation, the tag can also print a preview of the entered code and a related button to remove the code instead of the form. If you want to print only the code, use the {CartCouponCode} tag.

It is also possible to include all notifications from the Coupon group in the tag output.

Syntaksi

{CampaignCode(
    action: string,
    ajax: boolean,
    label: string,
    mode: string,
    notifications: boolean,
    placeholder: string
)}

Näkyvyys

Toimii näkyvyydessä: globaali

It is possible to use global visibility tags in any page template and within any tag.

Sending the discount code form to the server by using JavaScript

With the {CampaignCode} tag's ajax attribute, you can set the tag to return only the content it produces, which is useful when the tag is called with an AJAX request. This allows the discount code form to be submitted to the server without needing to reload the page.

With the following Interface markup, you can create a starting point for an asynchronous discount code form creation:

<div id="CampaignCodeFormContainer">
{CampaignCode(
    ajax: true
)}
</div>

By setting the mode attribute to the default value 'auto', the tag is configured to display either the code input form or a preview of the entered code, depending on the situation.

The following JavaScript code sends the entered code to the online store's server. An example of using jQuery's ajax() function:

function submitCampaignCode() {
  $.ajax({
    type: "POST",
    url: "/cart",
    data: $("#CampaignCodeFormContainer>form").serialize(),
    success: function(response) {
      reloadCampaignCodeForm();
    }
  });
}

The function is tied to the campaign code form's submit button with the on() function, and by using the preventDefault() function, you can prevent the form from being automatically submitted when the button is pressed:

$("#CampaignCodeFormContainer>form button").on("click", function(event) {
  event.preventDefault();
  submitCampaignCode();
});

Kutsun onnistuessa reloadCampaignCodeForm()-funktio saa syöttölomakkeen latautumaan uudelleen:

function reloadCampaignCodeForm() {
  $("#CampaignCodeFormContainer").empty();
  $.ajax({
    type: "GET",
    url: "/interface/CampaignCode?mode=auto&ajax=true",
    success: function(response) {
      $("#CampaignCodeFormContainer").html(response);
    }
  });
}

Once the customer has entered the discount code, it is not advisable to show an open input form anymore, as it is not possible to enter multiple discount codes. By reloading the form immediately after the submission using the mode: 'auto' attribute, you can display a preview of the entered code to ensure that the customer is aware that the code is in use.

If there is a product list on the same page as the input form, you can also reload the list with an AJAX call:

function getCampaignProducts(targetElement) {
  $.ajax({
    type: "GET",
    url: "/interface/ProductSet?campaign=n",
    success: function(response) {
      $(targetElement).html(response);
    }
  });
}

In the example, setting the campaign is done using the campaign attribute of the {ProductSet} tag. The retrieved products are printed to the element specified by selector targetElement.

Attribuutit

action:

Sets the destination for the discount code input field.

When the form is sent with a normal page request (not with JavaScript), the address to return to after processing the form is selected based on the value of the action attribute.

Allowed values:

  • (default value): this sends the data to the address /cart/
  • 'send': sends data to the address /checkout/

E.g. action:'checkout'

ajax:

Sets the tag to return either the whole page or only the content it produces as a response to an AJAX request when the form is sent by using JavaScript.

Allowed values:

  • true: returns only the content produced by the tag.
  • false: returns the whole page (default value).
label:

Defines the form title.

Sallitut arvot: merkkijono
mode:

Determines the type of content to be output.

Allowed values:

  • 'form': text field where the customer can enter the code
  • 'preview': prints a preview of the entered discount code and a button for deleting it
  • (default value): either the form, or the preview and the delete button, depending on the situation (recommended in most cases)

E.g. mode:'form'

notifications:

Determines whether the contents should be printed together with the notifications from the Coupon category.

Allowed values: true/false. Default value: true.

placeholder:

Defines a placeholder that appears in the form's text field when it is empty.

Sallitut arvot: merkkijono
before: / after:

With these two attributes, you can define content that is output before or after the content produced by the tag.

Allowed values:text that may contain HTML and Interface markup.

E.g.

  • before: '<p>This markup appears before the tag's own content.</p>'.
  • after: '<p>This appears after the tag's own content.</p>'

If the tag does not produce any content, the content of the before and after attributes will also not be displayed.

escape:

Adds escape characters to the content printed by the tag, making it easier to use the content in JavaScript code or JSON objects.

Sallitut arvot:
  • true: the attribute adds escape characters before quotation marks
  • false: the attribute does nothing
  • json: the attribute adds escape characters to the output, allowing the content to be used in JSON markup.
or:

Defines alternative content that is displayed if the tag itself does not produce any content.

Allowed values:text that may contain HTML and Interface markup.

E.g. or:'Content not found.'