How to truncate end of SKU in Magento on frontend? - php

What I'm seeking to do is display the SKU of a product on the frontend of my Magento cart, which is working accurately with this code:
<?php echo $this->htmlEscape($_product->getSku()) ?>
However, what I need to do in addition to this is truncate the last character returned when the SKU is loaded on the front end. How can I achieve this with PHP, what code can I use to accomplish this, and how would I insert it into this string to truncate the last character from the output?
Thanks for any help in advance! :]

To truncate the last character in a string call substr with -1 for the second parameter.
<?php echo $this->htmlEscape(substr($_product->getSku(), 0, -1)) ?>

Related

Multiplication evaluates with asterisk at the end when creating an .xls with PHP

The title might be a little confusing, but I've got a spreadsheet generator, and in this case it's used to generate a spreadsheet to upload to Amazon, and I'm working on converting the price of the items from USD to CAD.
In my file, I store the price of my items into $price_amazon, then I multiply it by whatever the user enters and gets stored into itself. Like so;
$conversion = $row['rate'];
$price_amazon = $row['price'];
$price_amazon = $price_amazon*$conversion
#Evaluates to:
#19.99*1.24
Here's the issue, once the spreadsheet loads up, my price fields will have the correct value, in this case 24.7876 but with an asterisk at the end, so 24.7876*. All of the cells for prices have an asterisk at the end, and I'm not sure on what is going on exactly. The image below shows exactly what I'm talking about, taken directly from the spreadsheet.
Then just remove the asterix at the end of the string and convert the value to a float:
$price_amazon = (float) substr($row["price"], 0, -1);
or even just a type conversation to a float removes the asterix from the end:
$price_amazon = (float) $row["price"];
Figured out the problem. User error! Further down my script, where I'm echoing all the variables into their necessary cells, I fat-fingered the asterisk key attempting to do God-knows-what. Here's what it looked like
echo "<table>\n";
echo "<tr>
<td>$price_amazon*</td>
As you can see, such a very easy mistake to make and hard to find, but this was definitely a case of user error. I appreciate everyone taking the time to help resolve my issue when in the end, the issue was me!

PHPExcel limited lenght in list

It seems that PHPExcel TYPE_LIST has a limited length.
$formula = $objPHPExcel->getActiveSheet()->getCell('A2')->getDataValidation();
$formula->setType(PHPExcel_Cell_DataValidation::TYPE_LIST);
$formula->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION);
$formula->setFormula1($countriesList);
The following string works in the dropdown list:
Afghanistan,Albania,Algeria,American
Samoa,Andorra,Angola,Anguilla,Antarctica,Antigua and
Barbuda,Argentina,Armenia,Aruba,Australia,Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bermuda,Bhutan,Bolivia,Bosnia
and Herze
Althought you may have notice that the last word "Herze" is not correct. The correct word is "Herezegovina". If I try to send all the text into the dropdown it stops working.
Afghanistan,Albania,Algeria,American
Samoa,Andorra,Angola,Anguilla,Antarctica,Antigua and
Barbuda,Argentina,Armenia,Aruba,Australia,Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bermuda,Bhutan,Bolivia,Bosnia
and Herzegovina
It seems that the maximum length is 255 and I would like someone to confirm me this? and if there's other option to show all the items as dropdown?
You can use a LOOKUP formula instead of a list

How do I truncate this result?

this displays the name of a group, but sometimes the names of the groups are really long and break the layout. How can I limit the result to maybe 20 characters? Thank you!
<?php echo $group->getName();?>
There is a php function for it. It's substr(). Your example code would look like:
<?php $length = 20; ?>
<?php echo substr($group->getName(), 0, $length);?>
If you want, you can add some extra features like check the length and if it's longer than $length than cut it and add "..." for users to know, the text was cut.
I found this to work. Sorry this question seems to have annoyed some of you, but I want others to know how I fixed it.
<?php echo substr($group->getName(),0,25);?>

Create a dynamic "Free Shipping" Progress bar for Woocommerece widget in Wordpress shortcode

I am trying to use the Progress Bar plugin in wordpress to create a widget that shows a progress bar under the shopping cart of how much more they need to spend in order to get free shipping.
I need to first take the value of the cart:
global $woocommerce;
$price = $woocommerce->cart->get_cart_total();
Secondly Strip the dollar sign and round to nearest integer
?
Thirdly Output the value into the shortcode, so it displays properly into the HTML. As it is now, it outputs this for the width instead of a number.<span class="red" style="width: .$cartnumber%;">
I need this to output a plain number so it will work with the width. Below is the code so far.
<?php
global $woocommerce;
$total = $woocommerce->cart->get_cart_total();
$cartnumber = str_replace("$", "", $total);
echo do_shortcode('[wppb progress='.$cartnumber.' option=red text="Free Shipping"]');
?>
I have enabled PHP execution within widgets for
I have encountered this before, and below is what I have used last.
http://www.php.net/manual/en/class.numberformatter.php
Before I am using preg_match using [0-9\,\s.]+ pattern. Then I have replaced all special characters and letters to blank to get the integer. (not a good solution)
Example code taken from http://www.pagecolumn.com/tool/pregtest.htm
<?php
$ptn = "/[0-9\,\s\.]+/";
$str = "$9,988,776.65 in the United States";
preg_match($ptn, $str, $matches);
print_r($matches);
?>
Output
Array
(
[0] => 9,988,776.65
)
After I realised that it was not a good solution, I revised it to simpler solution using the code below. from: Remove non-numeric characters (except periods and commas) from a string
$number = "$9,988,776.65";
echo preg_replace("/[^0-9]/", "", $number);
Output
998877665
if you did not remove the thousands and decimal symbol it will get the number format of the currency.
Regarding the progress bar, you need to convert the totals to 100%. I assume that your formula is similar below:
(price/free_shipping_minimum)*100
if result goes over 100 then set it to 100%. if you don't want decimals in your percentage cast it to int or use php's round/ceil/floor function.
http://www.php.net/manual/en/function.round.php
I hope this helps a lot.
Why you don't use
WC()->cart->subtotaĺ
Return a float, so no need to remove $ sign

Different results between print_r($something) and writing the same $something in database

I am parseing a page and saving the retrived data in mysql db. Everything is ok except the price of the product. After extracting price,when i use print_r($price) i get the actual value but while saving the same $price in my database, i get only a part of it.
for example:-
while using print_r($something); //output is 2 458
while saving in database $something, the saved value is only 458.
I think that the problem is due to space between 2 and 4. I can understand that this is a very simple question for most of you, but right now i am not able to solve it.
Thanks a lot ahead for support!
MySQL is pretty permissive about what data you can insert into its fields. In this case you are trying to insert a string that contains two numbers into a numeric field, it's doing its best to extract a single number from that data but is getting it wrong.
All you need to do is remove the space(s) before you insert:
$something = str_replace(' ', '', $something);
or using a regular expression you could remove any non-numeric character:
$something = preg_replace("'[^0-9]'", '', $something);

Categories