Free Downloads WooCommerce Shortcodes

Below, you’ll find a detailed guide on how to add the Free Downloads WooCommerce Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Free Downloads WooCommerce Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Free Downloads WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
Free Downloads WooCommerce

"Free Downloads WooCommerce is a user-friendly WordPress plugin that enhances your e-commerce site by offering instant, hassle-free product downloads. Ideal for WooCommerce platforms."

★★★★☆ (74) Active Installs: 5000+ Tested with: 6.2.3 PHP Version: 7.4.0
Included Shortcodes:
  • [download_now]
  • [download_now_page]
  • [add_to_cart]

Free Downloads WooCommerce [download_now] Shortcode

The Download Now for WooCommerce shortcode is used to create a download button for a specific product. The shortcode accepts three attributes: ‘id’, ‘align’, and ‘text’. The ‘id’ specifies the product, ‘align’ sets the button alignment, and ‘text’ customizes the button text. If no ‘id’ is provided, it defaults to the current product. The shortcode returns a download button wrapped in a div, which can be aligned as specified.

Shortcode: [download_now]

Parameters

Here is a list of all possible download_now shortcode parameters and attributes:

  • id – The unique identifier of the product to download
  • align – Determines the alignment of the download button (left, center, right)
  • text – Customizes the text displayed on the download button

Examples and Usage

Basic example – Displays the download button for a specific product by referencing its ID.

[download_now id=1 /]

Advanced examples

Using the shortcode to display the download button for a specific product by referencing its ID and aligning it to the center.

[download_now id=1 align="center" /]

Using the shortcode to display the download button for a specific product by referencing its ID, aligning it to the right, and customizing the button text.

[download_now id=1 align="right" text="Custom Download Text" /]

Note: If no product ID is provided, the shortcode will automatically target the current product on the page. If no alignment is specified, the button will default to align left. If no text is provided, the button will display the default text.

PHP Function Code

In case you have difficulties debugging what causing issues with [download_now] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('download_now', 'somdn_single_shortcode');

Shortcode PHP function:

function somdn_single_shortcode($atts) {

  // Attributes
  $atts = shortcode_atts(
    array(
      'id' => '',
      'align' => 'left',
      'text' => ''
   ),
    $atts,
    'download_now'
  );
  
  $product_id = $atts['id'];
  $align = $atts['align'];
  $shortcode_text = $atts['text'];

  $product = '';

  if (empty($product_id)) {
    $product = somdn_get_product();
    $product_id = somdn_get_product_id($product);
  } else {
    $product = somdn_get_product($product_id);
  }

  // Bail if no product matches the productID
  if (!$product)
    return;

  $download_button = somdn_get_shortcode_product_content($product_id, $shortcode_text);

  // Bail if no download button returned
  if (!$download_button) {
    return;
  }

  $content = '<div class="somdn-shortcode-wrap ' . $align . '">' . $download_button . '</div>';
  return $content;

}

Code file location:

download-now-for-woocommerce/download-now-for-woocommerce/Includes/Legacy/somdn-shortcodes.php

Free Downloads WooCommerce [download_now_page] Shortcode

The Download Now for WooCommerce shortcode handles the download functionality for a single product page. It accepts two parameters: ‘id’ and ‘text’. The ‘id’ represents the product’s unique identifier, while ‘text’ is customizable download text. If no ‘id’ is provided, it attempts to fetch the product from the current context. The shortcode then generates a download link for the product, using the provided or default text. If it fails to generate a link, it simply returns nothing.

Shortcode: [download_now_page]

Parameters

Here is a list of all possible download_now_page shortcode parameters and attributes:

  • id – The unique identifier of the product to download
  • text – The custom text to display on the download button

Examples and Usage

Basic example – The shortcode displays the download now page for a specific product by referencing the product ID.

[download_now_page id=123 /]

Advanced examples

Using the shortcode to display the download now page for a specific product by referencing both ID and custom text. The text will be displayed on the download button.

[download_now_page id=123 text="Download this product now" /]

Using the shortcode without specifying an ID. In this case, the download now page for the product currently being viewed will be displayed. It also includes custom text for the download button.

[download_now_page text="Get your download now" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [download_now_page] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('download_now_page', 'somdn_single_shortcode_page');

Shortcode PHP function:

function somdn_single_shortcode_page($atts) {

  // Attributes
  $atts = shortcode_atts(
    array(
      'id' => '',
      'text' => ''
   ),
    $atts,
    'download_now_page'
  );
  
  $product_id = $atts['id'];
  $text = $atts['text'];

  if (!$product_id) {

    $product = somdn_get_product();

    if (!$product) {
      return;
    }

  } else {

    $product = somdn_get_product($product_id);

  }

  if (!$product) {
    return;
  }

  $args = apply_filters('somdn_single_shortcode_page_args', array(
    'product' => $product,
    'echo' => false,
    'shortcode_text' => $text,
    'product_page_short' => true
 ));

  $content = somdn_product_page($args);

  //$content = somdn_product_page(false, $product, false, false, false, $text, true); // False $shortcode to replicate product page
  // function args = ($archive = false, $product = '', $echo = true, $archive_enabled = false, $shortcode = false, $shortcode_text = '', $product_page_short = false)
  
  if (!$content) {
    return;
  }

  return $content;

}

Code file location:

download-now-for-woocommerce/download-now-for-woocommerce/Includes/Legacy/somdn-shortcodes.php

Free Downloads WooCommerce [add_to_cart] Shortcode

The Download Now for WooCommerce shortcode, ‘add_to_cart’, facilitates the display of a single product’s price and cart button. This customizable shortcode accepts attributes like ‘id’, ‘class’, ‘quantity’, ‘sku’, ‘style’, and ‘show_price’. It fetches product data based on the ‘id’ or ‘sku’ provided. If the product is valid, it triggers the download page. If not, it displays the product price and add to cart button. This shortcode is versatile and enhances user experience.

Shortcode: [add_to_cart]

Parameters

Here is a list of all possible add_to_cart shortcode parameters and attributes:

  • id – A unique identifier for the WooCommerce product
  • class – Additional CSS classes to apply to the product
  • quantity – The default quantity for the product in the cart
  • sku – The stock keeping unit identifier for the product
  • style – Inline CSS styling to apply to the product
  • show_price – Controls whether the product’s price is displayed

Examples and Usage

Basic example – Display a single product with its price and add to cart button using the product’s ID.

[add_to_cart id="123" /]

Advanced examples

Display a single product with a custom CSS class and a specific quantity set to 3. The product is selected using its SKU.

[add_to_cart sku="ABC123" class="my-custom-class" quantity="3" /]

Display a single product without the price. The product is selected using its ID and a custom style is applied to the product display.

[add_to_cart id="123" show_price="false" style="border:2px solid #000; padding: 20px;" /]

Display a single product with a custom CSS class, a specific quantity set to 5, and without the price. The product is selected using its SKU.

[add_to_cart sku="ABC123" class="my-custom-class" quantity="5" show_price="false" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [add_to_cart] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('add_to_cart', 'somdn_custom_woo_add_to_cart_shortcode');// Add custom action

Shortcode PHP function:

function somdn_custom_woo_add_to_cart_shortcode($atts) {

  /**
   * Display a single product price + cart button.
   *
   * @version 1.0
   * @since  3.2.0
   * @param array $atts Attributes.
   * @return string
   */
  global $post;

  if (empty($atts)) {
    return '';
  }

  $atts = shortcode_atts(array(
    'id'         => '',
    'class'      => '',
    'quantity'   => '1',
    'sku'        => '',
    'style'      => 'border:4px solid #ccc; padding: 12px;',
    'show_price' => 'true',
 ), $atts, 'product_add_to_cart');

  if (!empty($atts['id'])) {
    $product_data = get_post($atts['id']);
  } elseif (!empty($atts['sku'])) {
    $product_id   = wc_get_product_id_by_sku($atts['sku']);
    $product_data = get_post($product_id);
  } else {
    return '';
  }

  $product = is_object($product_data) && in_array($product_data->post_type, array('product', 'product_variation'), true) ? wc_setup_product_data($product_data) : false;

  if (!$product) {
    return '';
  }

  $genoptions = get_option('somdn_gen_settings');
  $hide_readmore = (isset($genoptions['somdn_hide_readmore_button_archive']) && $genoptions['somdn_hide_readmore_button_archive']) ? true : false ;

  ob_start();

  $product_id = somdn_get_product_id($product);

  if (somdn_is_product_valid($product_id)) {

    $download_page_args = apply_filters('somdn_custom_woo_add_to_cart_shortcode_args', array(
      'archive'=> true,
      'product' => $product,
      'echo' => true
   ));
    do_action('somdn_archive_product_page', $download_page_args);

  } else {

    echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr($atts['class']) . '" style="' . (empty($atts['style']) ? '' : esc_attr($atts['style'])) . '">';

    if (wc_string_to_bool($atts['show_price'])) {
      // @codingStandardsIgnoreStart
      echo $product->get_price_html();
      // @codingStandardsIgnoreEnd
    }

    woocommerce_template_loop_add_to_cart(array(
      'quantity' => $atts['quantity'],
   ));

    echo '</p>';

  }

  // Restore Product global in case this is shown inside a product post.
  wc_setup_product_data($post);

  return ob_get_clean();
}

Code file location:

download-now-for-woocommerce/download-now-for-woocommerce/woo-files/somdn-woo-functions.php

Conclusion

Now that you’ve learned how to embed the Free Downloads WooCommerce Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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