Year End Sale -2o% Off On All Products - Coupon: FMADEC2017

Top 5 Programming Questions and Their Answers on WooCommerce Dynamic Pricing Extension - Part 1

WooCommerce dynamic pricing extension is a powerful plugin for any eCommerce store. It enables store owners to provide special discount offers based on various factors. Dynamic product pricing is useful when you need to provide discounts on the product based on the quantity of products, cart subtotal, and items a shopper is purchasing from your online store. If store owners want to improve their business then they must provide special discounted prices. Here in this section, we would like to cover answers of the top 5 frequently asked questions regarding WooCommerce dynamic pricing extension.

Important Note:

These are the answers provided by the community and best rated by the community as well. 

Question No 1:

The user is getting the dynamic price in a variable and wants this variable to a hooked function in the woocommerce_before_calculate_totals hook in the cart. But it is not working, how to resolve this issue?

Here is the user code

  $add=200; //I want to pass this variable in add_action hook
  add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price');
  function add_custom_total_price($cart_object) {
     global $woocommerce;
     $custom_price =$add; // This is my custom price
    foreach ( $cart_object->cart_contents as $key => $value ) {
       $value['data']->price = $custom_price;
    }
 } 

Answer

To achieve this task, just define $custom_price as global in the function,

Here is the solution:

$custom_price = 200; 

add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10, 2 );
function add_custom_total_price( $cart_object ) {
    global $woocommerce, $custom_price;

    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
    }
} 

Question No 2:

The user has a simple form with input type number, He wants to add total price dynamically whenever someone changes the number. He wants to do this without a button click event

Form Code:

<form>
<p class='ticket'>VIP ticket ($200):</p><input type="number" name="vip" min="0" max="20">
</form>

<p>Total price: </p>

Answer

Just change your HTML a little

<p class='ticket'>VIP ticket ($200):</p><input type="number" name="vip" id="vip" min="0" max="20" />

<p>Total price: $<span id="total">0</span></p>

Use <span id=”total”></span> to change total price easily and add id=”vip” to number input.

var ticketValue = 200;

$("#vip").on("change", function() {
    $("#total").html($(this).val() * ticketValue);
});

Question No 3:

A User has a Woocommerce Plugin on a WordPress site, he wants to display a specific product price throughout the site with more than one price. The user wants to show product with both price options. How to retrieve two prices values and display them?

Answer

In your function.php file just add the code given below.

function so_28073705( $product_id ) {

    $wc_product_variable  =  new WC_Product_Variable( $product_id );

    $variation_price_html  =  $wc_product_variable->get_price_html( );

    return $variation_price_html;

}

The function:

<?php echo so_28073705( <product_id> ); ?>

Returns this value:

<span class="amount">$low-price</span><span class="amount">$high-price</span>

Question No 4:

The user wants to show different prices for 3 different styles. For example if the user select style 1 prices are: 1$, 2$, 3$, Style 3: 4$, 5$, 6$ Style 3: 7$, 8$ and 9$. How to show these prices, depending on which style is chosen by the user?

Here is the Code:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
  <input type="hidden" name="cmd" value="_xclick">
  <input type="hidden" name="business" value="testtest@hotmail.com">
  <input type="hidden" name="lc" value="BM">
  <input type="hidden" name="button_subtype" value="services">
  <input type="hidden" name="no_note" value="0">
  <input type="hidden" name="currency_code" value="USD">
  <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
  <table>
    <tr>
      <td>
        <input type="hidden" name="on1" value="Which style?">Which style?</td>
    </tr>
    <tr>
      <td>
        <select name="os1">
          <option value="Style 1">Style 1</option>
          <option value="Style 2">Style 2</option>
          <option value="Style 2">Style 2</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>
        <input type="hidden" name="on0" value="How many?">How many?</td>
    </tr>
    <tr>
      <td>
        <select name="os0">
          <option value="10 -">10 - $1.00 USD</option>
          <option value="100 -">100 - $2.00 USD</option>
          <option value="1000 -">1000 - $3.00 USD</option>
        </select>
      </td>
    </tr>
  </table>
  <input type="hidden" name="currency_code" value="USD">
  <input type="hidden" name="option_select0" value="10 -">
  <input type="hidden" name="option_amount0" value="1.00">
  <input type="hidden" name="option_select1" value="100 -">
  <input type="hidden" name="option_amount1" value="2.00">
  <input type="hidden" name="option_select2" value="1000 -">
  <input type="hidden" name="option_amount2" value="3.00">
  <input type="hidden" name="option_index" value="0">
  <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

Answer

You can hook the change event in jQuery and can easily update the prices accordingly. The script added below updates the hidden field and change the text of the second dropdown. You have to include the following line in the <head> section or anywhere in the <body>.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Script:

   $('select[name=os1]').change(function() {
 var style = $( this ).val();
  var price1, price2, price3;
  
  if(style == "Style 1"){
    price1 = "1.00";
    price2 = "2.00";
    price3 = "3.00";
  } else if(style == "Style 2"){
    price1 = "4.00";
    price2 = "5.00";
    price3 = "6.00";
  } else if (style == "Style 3"){
    price1 = "7.00";
    price2 = "8.00";
    price3 = "9.00";
  }
  
  $('select[name=os0] option[value=10]').text("10 - $" + price1 + " USD");
  $('select[name=os0] option[value=100]').text("100 - $" + price2 + " USD");
  $('select[name=os0] option[value=1000]').text("1000 - $" + price3 + " USD");
  $("input[name=option_amount0]").val(price1);
  $("input[name=option_amount1]").val(price2);
  $("input[name=option_amount2]").val(price3);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
      <input type="hidden" name="cmd" value="_xclick">
      <input type="hidden" name="business" value="testtest@hotmail.com">
      <input type="hidden" name="lc" value="BM">
      <input type="hidden" name="button_subtype" value="services">
      <input type="hidden" name="no_note" value="0">
      <input type="hidden" name="currency_code" value="USD">
      <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
      <table>
        <tr>
          <td>
            <input type="hidden" name="on1" value="Which style?">Which style?</td>
        </tr>
        <tr>
          <td>
            <select name="os1">
              <option value="Style 1">Style 1</option>
              <option value="Style 2">Style 2</option>
              <option value="Style 3">Style 3</option>
            </select>
          </td>
        </tr>
        <tr>
          <td>
            <input type="hidden" name="on0" value="How many?">How many?</td>
        </tr>
        <tr>
          <td>
            <select name="os0">
              <option value="10">10 - $1.00 USD</option>
              <option value="100">100 - $2.00 USD</option>
              <option value="1000">1000 - $3.00 USD</option>
            </select>
          </td>
        </tr>
      </table>
  <label id="output"></label>
      <input type="hidden" name="currency_code" value="USD">
      <input type="hidden" name="option_select0" value="10">
      <input type="hidden" name="option_amount0" value="1.00">
      <input type="hidden" name="option_select1" value="100">
      <input type="hidden" name="option_amount1" value="2.00">
      <input type="hidden" name="option_select2" value="1000">
      <input type="hidden" name="option_amount2" value="3.00">
      <input type="hidden" name="option_index" value="0">
      <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
      <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>

Values of os0 changed from 100 - to 100 and so on.

Question No 5:

The user wants the price of products to change dynamically according to the order quantity.  Is there any module/plugin doing it in WooCommerce?

Answer

There are lots of such plugins available (free and paid) which you can install on your store to change product prices according to requirements. I would like to recommend Dynamic Pricing extension for WooCommerce by FMEAddons.

Download WooCommerce Dynamic Pricing Extension by FMEAddons

woocom-dynamic-pricing-rules

Leave a Reply