0

I am currently putting together a WordPress website, with WooCommerce being the preferred platform for eCommerce functionality.

The individual would like to have a field, on the Product Page, where people can enter a custom word. This custom word would be applied to the Product on the page.

Furthermore, the first 16 letters, within this custom field, would be included in the product price. Therefore, the charge would only apply to any letters after the first 16 letters.

I am assuming this would require work within the functions.php file.

Any suggestions on the required coding or steps I would need to follow, in order to achieve this?

Craig
  • 1,872
  • 5
  • 23
  • 56
  • 1
    what code have you tired for this task? – happymacarts Apr 25 '17 at 20:45
  • To be honest, I am not entirely sure where to start with this particular function. If someone is able to point me in the right direction, then I could start to work on some code. – Craig Apr 25 '17 at 20:47
  • You could start [here](http://stackoverflow.com/questions/21650019/woocommerce-add-input-field-to-every-item-in-cart) or [here](https://sarkware.com/adding-custom-product-fields-to-woocommerce-without-using-plugins/). – helgatheviking Apr 25 '17 at 21:14

3 Answers3

1

I have never used WOOCOMERCE, and the question is asking for a php solution , but since this has to do with client input first i thought it may be better to use jquery on the client side to validate then pass the values to your php function to enter to your DB

you could run ajax calls on change too put that seemed a little too intensive and a PITA

$(document).ready(function() {
$("#basePrice").on('change', function(e){
   $('#word').keyup();
})
  $('#word').on('keyup', function(e) {
    length = $(this).val().length;
    $('#wordLength').html(length);
    if(length>=16){
      $('#wordLength').addClass('greater');
      total = parseFloat($('#basePrice').val()) + parseFloat((length - 16) * $('#priceperletter').val())  ;
      $('#total').val(total);
    }
    else {
      $('#wordLength').removeClass('greater');
      total = parseFloat($('#basePrice').val());
      $('#total').val(total);
    }
  })
})
label{
  display:block
}
.greater{
  border:1px solid red;
}
.totalLabel,.totalLabel * {
font-weight:700;
font-size:18px;
}
.totalLabel input{
  border:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Base Price<input id="basePrice" value="10"/></label>
<label>Price per Letter over 16<input id="priceperletter" value = ".25"/></label>
<label>Enter your string<input id="word" type="text" /> <span id="wordLength"></span></label>


<label class="totalLabel" >Total $<input id="total" readonly value="10"/></label>
happymacarts
  • 2,547
  • 1
  • 25
  • 33
  • Not entirely sure why someone would had down voted you answer. You have started to help my query. What I would like to do is to achieve something like what you have entered, although I would want it to manipulate the WooCommerce Product Price rather than create another price. Also, I would not want the base price or price per letter inputs to be visible. Any further advice, would be highly appreciated. – Craig May 09 '17 at 18:58
1

To count the character in string PHP strlen () function is used . For example:

<?php
echo strlen('YourString'); //outputs 10
?>
<?php
$string='YourString';
echo strlen($string); //outputs 10
?>

For more refer Crackpoint PHP strlen () tutorial

So if you want to compare you can use like

if (strlen($description) >= 16) ){
   // more then 16 characters
   // We need to charge the user
} else {
   // N

Your solution

<?php
   // If form is submitted
   if (isset($_POST['description']) && !empty($_POST['description'])) {
        // Form is submitted, check length
        $description = trim($_POST['description']);
        if (strlen($description) >= 16)) {
           // more then 16 characters
           // We need to charge the user
        } else {
           // Not more then 16 characters, continue.
        }
   }
   ?>
   <html>
         ... Your other code here ...
         <form method='POST' action="$_SERVER['PHP_SELF']">
              <input type='text' name='description' />
              <input type='submit' />
         </form>
   </html>
Tinesh Nehete
  • 31
  • 1
  • 4
0

Since this is a very generic question, and I can't comment only post an answer I'll just try and help out here.

if strlen($description >= 16) {
   // more then 16 characters
   // We need to charge the user
} else {
   // Not more then 16 characters, continue.
}

W3Schools: https://www.w3schools.com/php/func_string_strlen.asp PHP Site: http://php.net/manual/en/function.strlen.php


As per the comment below. Here is a mockup just for demonstration. I'm not sure how all of your site is setup, so I can't exactly make it work copy&paste for you. I'm not sure entirely how WooCommerce is setup in terms of payment.

   <?php
   // If form is submitted
   if (isset($_POST['description']) && !empty($_POST['description'])) {
        // Form is submitted, check length
        $description = trim($_POST['description']);
        if strlen($description >= 16) {
           // more then 16 characters
           // We need to charge the user
        } else {
           // Not more then 16 characters, continue.
        }
   }
   ?>
   <html>
         ... Your other code here ...
         <form method='POST' action="$_SERVER['PHP_SELF']">
              <input type='text' name='description' />
              <input type='submit' />
         </form>
   </html>

Again, this isn't ready for copy & paste. I know people on this forum are very particular and judgemental so please beware it's a mockup. You will need to ensure the input is secure and safe etc. I'm not sure how to directly hook into woocommerce so I apologize if this is just a waste of your time.

It's my first legitimate answer, be nice.

John C.
  • 45
  • 1
  • 5
  • Thanks for your answer. What additional information would I need to provide, in order to help obtain a more comprehensive answer? All I need is a Text Box on the Product Page, that is separate from the Product Description. The text box would allow someone to enter their own text. The individual would then pay per letter entered, minus the first 16 letters. – Craig Apr 25 '17 at 20:55
  • Hey Craig. I've updated my answer to possibly get you one step further. I hope I helped a little bit. Ideally, someone with Woocommerce experience will help you out. Good luck! – John C. Apr 25 '17 at 21:11
  • Thanks for your time @Unviewed. My feature has progressed a little bit. What I would like to do is to dynamically change the Product Price, depending on the amount of characters entered into the Custom Field. happymacarts is pretty close but rather than creating an additional Output box, any ideas how I would link such a code to directly manipulate the WooCommerce Product Output? – Craig May 09 '17 at 19:04