I need to get the value of an attribute for several Magento products. The value is the width. So I am currently using:
$product->getAttributeText('width');
Which returns the correct value of say "100mm". I now need to get rid of the "mm" off the end, so I have tried:
explode('m',trim($width));
But this just returns "Array". I am guessing I need to convert the width value to a string first, but I'm not sure how.
Incase if you get something other than mm. If I were you, would use preg_replace.
$widthAtt = $product->getAttributeText('width');
echo preg_replace("/[^0-9]/", "", $widthAtt);
What it does is, it removes any numeric values from $widthAtt.
Related
I'm developing a table with two columns, "State" and "Sales". To get the number in the sales column, I use the wc_price function, which works just fine to output a currency string:
CA $1,652.13
CO $515.80
...etc.
I have at least 50 rows, one for each state.
The very last row will output the total of all the dollar amounts from column 2.
I discovered that the actual result from wc_price isn't just a sting, it's a block of html:
<bdi>
<span class="woocommerce-Price-currencySymbol">$</span>
435.50
</bdi>
I tried to get to the actual number within the html, 435.50 using strip_tags:
$numeric = strip_tags(wc_price($total)); // result is a simple string, with the $ sign
echo str_replace("$","",$numeric) . "<br>"; // doesn't strip away the $ sign.
$tally = $tally + $numeric; // produces an error, non-numeric value.
The error is on the $tally = line. This doesn't work in any way. Sure, the html tags are stripped, and I'm left with a string: $435.50. In other words, I can't get rid of that danged '$' for some reason. I simply cannot parse, convert, or anything to that currency string. (int), intval($numeric), (float)$numeric, floatval($numeric), etc., none of these work. number_format doesn't work, either.
Warning: A non-numeric value encountered in C:\xampp\htdocs\mysite\wp-content\plugins\my-custom-plugin\my-custom-plugin.php on line 104
Add a floatval() around your str_replace and assign the variable.
$numeric = strip_tags($html);
$numeric = floatval(str_replace("$", "", $numeric));
See working example. It works with 5.6, 7.4 and 8.1.
I'm not sure of the result, but I think it's possible to have the price without the currecy reading from the documentation of wp_price:
$numeric = wp_price($total, array('currency'=>''));
However you can have some action that change the comportament of the wc_price function.
I have a field named map_box which contains coordinates (see the example below) and cannot be serve as string.
Previously, before Backpack, I use to cast the field to an array 'map_box' => 'array' and the result was the following: note that the coordinates are not string
[
-73.661,
45.589
],
Now, I am trying to achieve the same result, but I keep getting an array of strings. I have tried using the repeatable field with numbers field in it, but the numbers are string. I have also tried to cast the field, then use the text field but this returns an error (as the form is expecting a string and not an array).
My goal is to be able to edit this field in a CRUD controller while being able to serve them with the correct format in my API. Any ideas on how I could achieve the same result as above? I can also use the following format, if this one is possible:
{
lon: -73.661,
lat: 45.589
}
Thanks!
i am not sure if i understand you correctly ... what backpack has to do with this casting?
however if you have something like:
$value='"max_box":{lon: -73.661, lat: 45.589}';
or any string and you want to get float number positive or negative you can use this:
preg_match_all('/-?\d+\.\d+/', $value, $matches);
the $matches[0] will be an array of your numbers
I have a string that I get from sanitizing some values from an xml file.
$resolutions=strip_tags($resolutions,allow); // gives me '75 75 300 300 600 600'
Then I change the spaces to commas:
$resolutions = preg_replace('#\s+#',',',trim($resolutions)); // gives me '75,75,300,300,600,600'
Then I take that and want to extract min and max values like this
$resolution_min = min($resolutions);
on the page for display
echo $resolution_min; // still gives me '75,75,300,300,600,600'
However when I change the code where I want it displayed on the page
echo min(75,75,300,300,600,600); // it displays '75' as it should
I think, the issue is that the CSV values are being interpreted as a single text string and I need them read as CSV integer values.
Is there some way to force these to be read as numeric values by min and max?
I think part of my problem comes from this line or a line like it, jus befor an "explode" command
$list = "" . substr($scansupportedresolutions, $start,$length) . "";
I have never seen this kind of double double quote dot and dot double double quote and have no idea what it means
Is it simply saying the entire string in $list will be
".<CONTENT HERE>."
but specifically in double quotes? Whatever it is i seems crucial to the explode command.
From min:
If the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.
You are passing one parameter and it's a string. Pass an array:
$resolutions = explode(',', $resolutions);
$resolution_min = min($resolutions);
AbraCadaver is correct in his answer.
I have tried to improve your code. Take a look
<?php
// After Stripping tag
$resolutions = "75 75 300 300 600 600";
// No need to conver to Space to comma
$resolutionsArray = explode(" ", $resolutions);
$resolutionMin = min($resolutionsArray);
print($resolutionMin);
There is no gain by converting the "white Space" to "Comma". You can explode the string into array by setting "white Space" as delimiter.
Hope it helps you.
I am trying to use the following functionality:
[quote_elements.product.factor;ope=mul:quote_elements.qty]
But all I get is always 0.
if I use:
[quote_elements.product.factor;ope=mul:4]
it works fine and I get 4 times the factor number.
But this is not what I need. I need to multiply dynamically the factor with the quantity. this can be for each row different.
any tips what I am missing here?
Embedded TBS fields does not work in parameter ope.
That why the string « quote_elements.qty » is always converted to 0.
Parameter ope=mul can work only with fixed values.
In order to solve you problem, you can use a custom ondata function. It will enable you to add a calculated column in you record before to merge it.
PHP side:
function f_my_ondata($BlockName, &$CurrRec, $RecNum) {
$CurrRec['my_result'] = $CurrRec['product']['factore'] * $CurrRec['qty'];
}
Template side :
[quote_elements;block=...;ondata=f_my_ondata] // block definition
...
[quote_elements.my_result]
I'm using a wordpress plugin which redirects to a random post. It allows me to redirect to random posts based on tags, so the url might look like
example.com/?random&random_tag_id=100
If I wanted to find random posts which are tagged with tag ids 100 and 101, I would just do
example.com/?random&random_tag_id=100&random_tag_id=101
But I want to find random posts which are from EITHER ids 100 or 101. I know & is used for 'this + this', but would it be possible to make a 'this OR this' request via the URL?
Thanks for any help!
You can do something like this:
example.com/?random&random_tag_id=100,101,102,103
You'll then have to do something like this when you process those variables:
<?php
$random_tag_ids = explode(',', $_GET['random_tag_id']);
// $random_tag_ids now contains an array of your random tag ids
?>
You can pass arrays to query like this:
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
You'll get $_GET['random_tag_id'] == array(100, 101)
The problem with doing it that way is you'll overwrite the value in PHP. You need bracket notation to pass it correctly as an array
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
When you get to your PHP then you'll have
echo $_GET['random_tag_id'][0]; //Outputs 100
You could then pass into your query an imploded list
You're misunderstanding how GET parameters work.
In a Query String (?key=a&otherkey=b), the & is just a delimiter between the different key/var pairs. So the PHP equivalent of that string is this:
$_GET['key'] = 'a';
$_GET['otherkey'] = 'b';
If you use the same key twice in a query string, the latter will overwrite the former. So for the string ?key=a&key=b, $_GET['key'] will be equal to b.
As others have said, you can pass data via a comma separated list, (i.e. ?key=1,2,3,4), or via an array, (i.e. ?key[]=a&key[]=b).