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

Description

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

Depending on the situation, the tag can 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.

Syntax

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

Scope

Required scope: globaali

Tags with global scope can be used on any template and in 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 makes it possible to send the discount code form to the server without the need to reload the page.

You can use the Interface markup below to create a basis for implementing an asynchronous discount code form:

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

By leaving the mode attribute with the default value 'auto', the tag is set to print either the code input form or the preview of the entered code, depending on the situation.

The following JavaScript code sends the entered code to the online store's server. The example makes use of the jQuery 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 on(), and by using the preventDefault() function, you prevent the form from being automatically submitted when the button is pressed:

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

Upon a successful call, the reloadCampaignCodeForm() function will reload the input form:

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 at once. By reloading the form immediately after the submission using the attribute mode: 'auto', you can display a preview of the entered code to ensure that the customer is clearly 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/Products?campaign=n",
    success: function(response) {
      $(targetElement).html(response);
    }
  });
}

In the example, setting the campaign is done with the {Products} tag's campaign attribute. 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 site 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:

  • 'cart' (default value): sends the data to the address /cart/
  • 'checkout': data is sent 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 (default value): returns the entire page
label:

Defines the form title.

Allowed values: 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
  • 'auto' (default value): prints, as necessary, either the form, or the preview and the delete button (recommended in most cases)

E.g. mode:'form'

notifications:

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

Allowed values: true/false. The default value is true.

placeholder:

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

Allowed values: merkkijono
after/before:

By using the after and before attributes you can define content that is output either after or before the tag's output.

Allowed values:The HTML and Interface markup

E.g.

  • before: '<p>Tämä merkkaus näkyy ennen tagin omaa sisältöä.</p>'
  • after: '<p>Tämä taas näkyy tagin oman sisällön jälkeen.</p>'

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

escape:

Adds an escape character before quotation marks in the output.

The attribute makes it easier to process the tag-produced content when using JavaScript.

Allowed values:true/false. The default value is false.

or:

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

Allowed values:The HTML and Interface markup

E.g. or:'Sisältöä ei löytynyt.'