Good Day, I have been struggling with the following I got a product name would like to get the product attibutes from product name like size,lenght, pitch
I have a product name Hex Head Bolts M24x100x2 8.8 MF The Size M24 The Lenght 100 The Pitch 2
Now I have to break appart the product name to get to the attributes so I use the following and it works sort of.
for this example the product name is Hex Head Bolts M24x100x2 8.8 MF
<?php
class ProductManage {
public function __construct() {
/** Step 1 (event). */
add_action( 'admin_menu', [ $this, 'product_manage_menu' ] );
}
/** Step 2 (add item). */
public function product_manage_menu() {
$page_title = 'Product Manage';
$menu_title = 'Product Manage';
$capability = 'manage_options'; // which menu.
$menu_slug = 'product-manage'; // unique identifier.
$callback = [ $this, 'product_manage_options' ];
$icon = 'dashicons-media-code';
$position = 21;
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback, $icon, $position );
}
/** Step 3 (page html). */
public function product_manage_options($size = array(1080,1080)) {
/*String and array Diameter function*/
function findDiameterStringFromArray($phrases, $string, &$position) {
// Reverse sort phrases according to length.
// This ensures that 'taxi' isn't found when 'taxi cab' exists in the string.
usort($phrases, create_function('$a,$b',
'$diff=strlen($b)-strlen($a);
return $diff<0?-1:($diff>0?1:0);'));
// Pad-out the string and convert it to lower-case
$string = ' '.strtolower($string).' ';
// Find the phrase
foreach ($phrases as $key => $value) {
if (($position = strpos($string, ' '.strtolower($value).' ')) !== FALSE) {
return $phrases[$key];
}
}
// Not found
return FALSE;
}
/*String and array function*/
function findStringFromArray($phrases, $string, &$position) {
// Reverse sort phrases according to length.
// This ensures that 'taxi' isn't found when 'taxi cab' exists in the string.
usort($phrases, create_function('$a,$b',
'$diff=strlen($b)-strlen($a);
return $diff<0?-1:($diff>0?1:0);'));
// Pad-out the string and convert it to lower-case
$string = ' '.strtolower($string).' ';
// Find the phrase
foreach ($phrases as $key => $value) {
if (($position = strpos($string, ' '.strtolower($value).' ')) !== FALSE) {
return $phrases[$key];
}
}
// Not found
return FALSE;
}
?>
<style>
.tableFixHead { overflow: auto; height: 700px; }
.tableFixHead thead th { position: sticky; top: 0; z-index: 1; }
/* Just common table stuff. Really. */
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 16px; }
th { background:#eee; }
</style>
<div class="tableFixHead">
<div class="wrap">
<h2>Product Manage</h2>
<br>
<div class="container">
<div class="row my-4">
<div class="col-6 row">
<table width="100%" border="1" cellspacing="2" cellpadding="2">
<tbody>
<thead>
<tr>
<th scope="col" style="width:30%">Name</th>
<th scope="col" style="width:10%">Sku</th>
<th scope="col" style="width:10%">Size</th>
<th scope="col" style="width:10%">Length</th>
<th scope="col" style="width:10%">material-grade</th>
<th scope="col" style="width:10%">plating</th>
<th scope="col" style="width:20%">Update</th>
</tr>
</thead>
<tbody>
<?php
// Fetch the products.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'date_query' => array ('after' => date('Y-m-d', strtotime('-20 days'))),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
echo '<tr>';
global $product;
$attr = $product->get_sku();
//echo $product;
//echo $product->get_id().' ';
//$product_name = preg_replace('/(?<!\ )[x]/', ' $0 ', $product->get_name());
$product_name = "Hex Head Bolts M24x100x2 8.8 MF";
echo '<td>'.$product_name.'</td>';
echo '<td>'.$product->get_sku().'</td>';
// echo '<td>'.$product->get_image( $size, $attr).'</td>';
$str = $product_name;
$mystring = $str;
//$lookfor = "x";
$main_size = array("M2","M2.5","M3","M4","M5","M6","M7","M8","M9","M10","M12","M14","M16","M17","M18","M19","M20","M22","M24","M25","M26","M27","M30","M33","M36","M39","M42","M45","M48","M52","M56","1.5","2","2.5","3","3.2","4","4.8","5","6","8","10","12.5","16.5","17","18","1","2","1/2","5/8","7/8","7/16","11/4","1/4","3/16","3/8","5/16","3/4","#10","#6","#4","#8","11/2","1/16","13","11/8","13/4","9/16","21/2","11/16","11/4","#1","#2","#3","#5","#7","#9","#11","#12","#13","#14","#15","#16","#17","#18","#19","#20","7","9","11","25","35","14","15","16","19","20","50","24","32","30","27","26","23","22","110","21","12","90","38","85","100","80","150","78","115","75","230","72","125","70","140","67","40","65","200","62","11.6","60","55","53","52","47","45","42","28","220","59","300","155","130","122","34","48","105","M13","M3.2","M1.6","M6.3","M3.5","7.5","3.5","4.5","5.5","6.5","8.5","9.5","19.5","17.5","10.5","6.3","13/8","M72","5.6","6.4");
$main_len = array("5","6","8","10","12","14","15","16","18","19","20","21","22","24","25","28","30","33","35","36","40","45","50","54","55","60","65","70","75","80","85","90","95","100","110","115","120","125","130","140","145","150","155","160","165","170","176","180","185","190","200","210","220","230","240","250","255","260","275","280","290","300","315","320","340","350","360","370","380","385","400","452","460","500","510","520","550","25M","5/16","5/8","3/4","1","1 1/4","1 1/2","1 1/2","1 3/4","2","2 1/2","3","3 1/2","305","310","365","375","425","440","330","420","480","430","2","225","105","32","13","1M","3M","470","4","7 1/2","2 3/4","7","9","2 1/4","3","5 1/2","4 1/2","4","1/2","175","27","450","135","1 3/4","270","390","1.5","20M","10M","5M","50M","15M","500M","40M","100M","6M","38","67","46","104","103","123","93.5","43","39","77","52","48","42","7/8","3/8","68","4 1/8","9.5","3 1/4","4 5/8","1 3/8","60M","63","8","152","6","114","5","112","30M","7","625","216","700","800","900","850","925","750","590","1350","1000","600","650","182","376","107","71","56","6.5","57","58","1.1M","2M","4","41","195","26","465","3 3/4","1220","890","4 1/4","1250","374","1280","1270","760","920");
$plating = array("BJ", "EG", "HDG", "PLN", "YP");
/*Checking size*/
$sizewordsAndPhrases = $main_size;
$sizesrch = $mystring;
if (($found = findDiameterStringFromArray($sizewordsAndPhrases, $sizesrch , $pos)) !== FALSE) {
//echo "'".$found."' was found in '".$srch."' at string position $pos.";
$second_size = $found;
}
else {
$second_size = "None";
}
/*Checking Lenght*/
$wordsAndPhrases = $main_len;
$srch = $mystring;
if (($found = findStringFromArray($wordsAndPhrases, $srch, $pos)) !== FALSE) {
//echo "'".$found."' was found in '".$srch."' at string position $pos.";
$second_len = $found;
}
else {
$second_len = "None";
}
/*Checking Plating*/
$wordsAndPhrases = $plating;
$srch = $mystring;
if (($found = findStringFromArray($wordsAndPhrases, $srch, $pos)) !== FALSE) {
//echo "'".$found."' was found in '".$srch."' at string position $pos.";
$second_plating = $found;
}
else {
$second_plating = "None";
}
if (empty($size)) {
//echo shop_attributes_in_loop();
echo '<td style="background-color:RED;color:white;">'.$second_size.'</td>';
//echo '<td style="background-color:RED;"> test </td>';
}else{
echo '<td>'.$product->get_attribute( 'pa_diameter' ).'</td>';
}
$length = $product->get_attribute( 'pa_length' );
if ($length == false) {
echo '<td style="background-color:RED;color:white;">'.$second_len.'</td>';
} else{
echo '<td>'.$product->get_attribute( 'pa_length' ).'</td>';
}
$material = $product->get_attribute( 'pa_material-grade' );
if ($material == false) {
echo '<td style="background-color:RED;color:white;"> test </td>';
} else{
echo '<td>'.$product->get_attribute( 'material-grade' ).'</td>';
}
$plating = $product->get_attribute( 'pa_plating' );
if ($plating == false) {
echo '<td style="background-color:RED;color:white;">'.$second_plating.'</td>';
} else{
echo '<td>'.$product->get_attribute( 'plating' ).'</td>';
}
echo '<td>'.$product->get_date_modified().'</td>';
echo '<td>test</td>';
echo '</tr>';
//echo "Date Modified (".$product->get_date_modified().") ";
endwhile;
wp_reset_query();
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
new ProductManage();
the out put from here is correct size = M24
but now I add lenght and that is where it goes down the drain then it brings up both size and lengh 100
what am i missing
