How to change the position of headers in the table wordpress - php

Now I have a post_type created, called products
This shows me a table in the following order:
Title
Category
Date
Price
Mileage
As I can change the position as follows:
Title
Category
Price
Mileage
Date
???
I thank you help me, thanks

Add this to functions.php
add_filter('manage_productos_posts_columns', 'column_order_move_1');
function column_order_move_1($columns) {
$n_columns = array();
$move = 'Price'; // what to move
$before = 'Date'; // move before this
foreach($columns as $key => $value) {
if ($key==$before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
add_filter('manage_productos_posts_columns', 'column_order_move_2');
function column_order_move_2($columns) {
$n_columns = array();
$move = 'Mileage'; // what to move
$before = 'Date'; // move before this
foreach($columns as $key => $value) {
if ($key==$before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
It's not the best solution.

Related

PHP Shopware Rest API - How to sum specific Keyvalues of Arrays in an Array

I want to calculate and show the turnover of some webshops(Plattform:Shopware), filtered by month and without canceled orders.
I got the connection to the webshop, I already got all orders, sorted them by month, filtered out the canceled orders. Now I pushed all relevant orders into a new array called $october. In $october, every single order is an array itself with many keys and values. I just need to summarize all "invoiceAmount" values together so i can show the turnover of this month. I tried many variations(commented), the best i get is "0" from the initialization of $sum... but im also very new to php. please help me out :-)
API Resources Orders
<?php
include_once ('api.php');
$url = xxxxx;
$user = xxxxx;
$key = xxxxx;
$client = new ApiClient($url,$user,$key);
$orders = $client->get('orders');
$october = [];
echo "<h1>===================month Array==============</h1>";
foreach ($orders as $order) {
foreach ($order as $field) {
if ($field["orderStatusId"]!==4) {
$mystring = $field[orderTime];
$findme = '-10-';
$pos = strpos($mystring, $findme);
if ($pos !=null) {
print_r($field);
array_push($october,$field);
}
}
}
}
echo "<script>";
echo 'console.log('.json_encode($october).')';
echo "</script>";
print_r($october);
echo("<h1>===================Sum of month==============</h1>");
$sumOctober = 0;
foreach ($october as $field["invoiceAmount"]=>$value) {
$sumOctober+= $value;
}
/*
$sumOctober = 0;
foreach ($october as $order) {
foreach ($order as $field["invoiceAmount"]=>$value) {
$sumOctober+= $value;
}
}
*/
echo($sumOctober);
/*
$arr = $october;
$sum = 0;
foreach ($arr as $order){
foreach ($order as $field["invoiceAmount"]=>$value) {
$sum += $value;
}
}
*/
/*
$arr = $october($orders($key));
$sum = 0;
array_walk_recursive($arr, function($val, $key) use (&$sum) {
$sum += $val;
});
var_dump($sum);
echo "<h1>Die Summe Oktober ist:</h1>";
echo ($sum);
*/
?>
This totally helped, dont know why the guy deleted his answer right away...but thanks!
$sumOctober = 0;
foreach ($october as $oct) {
$sumOctober+= $oct["invoiceAmount"];
}

How to access into the description of a submenu?

Well, I'm trying to get the description of a menu item and of a sub menu with the code attached below, but I'm not able to get it.
I'm trying to get the description of "About us" and the description of "Our board staff":
For the menu item (About us) is working good but for some reason the sub menu (Our board and staff) doesn't contain the information description and it just have ID, URL and Title, I already tried a var_dump() of the sub menu object (as you can see it in the code below) but it doesn't has it.
function get_menu_section_description($sectionUrl){
$menu = wp_get_menu_array("menu");
$desc = "";
foreach ($menu as $key => $item){
$arr = $item['url'];
// var_dump($item);
if ($sectionUrl == $arr[0]) {
$desc = $item['description'];
}
if(sizeof($item['children']) > 0){
foreach ($item['children'] as $key => $children){
// var_dump($children);
$arr2 = $children['url'];
if ($sectionUrl == $arr2) {
$desc = $children['description'];
}
}
} } return $desc; }
Anyone know why doesn't have the description item, how to activate it or a possible solution for that? Thanks in advance.
Since WordPress 3.0, you don't need a custom walker anymore!
There is the walker_nav_menu_start_el filter, see https://developer.wordpress.org/reference/hooks/walker_nav_menu_start_el/
Example
function add_menu_description($item_output, $item, $depth, $args) {
if (strlen($item->description) > 0 ) {
// append description after link
$item_output .= sprintf('<span class="description">%s</span>', esc_html($item->description));
// insert description as last item *in* link ($input_output ends with "</a>{$args->after}")
//$item_output = substr($item_output, 0, -strlen("</a>{$args->after}")) . sprintf('<span class="description">%s</span >', esc_html($item->description)) . "</a>{$args->after}";
}
return $item_output;
}
add_filter('walker_nav_menu_start_el', 'add_menu_description', 10, 4);
I found a solution few days a go, so may it help someone, the problem I had was was the function to call the menu wp_get_menu_array(), there I had to add the description in the sub menu, just that:
function wp_get_menu_array($current_menu) {
$array_menu = wp_get_nav_menu_items($current_menu);
$menu = array();
foreach ($array_menu as $m) {
if (empty($m->menu_item_parent)) {
$menu[$m->ID] = array();
$menu[$m->ID]['ID'] = $m->ID;
$menu[$m->ID]['title'] = $m->title;
$menu[$m->ID]['url'] = $m->url;
$menu[$m->ID]['classes'] = $m->classes;
$menu[$m->ID]['description'] = $m->description;
$menu[$m->ID]['children'] = array();
}
}
$submenu = array();
foreach ($array_menu as $m) {
if ($m->menu_item_parent) {
$submenu[$m->ID] = array();
$submenu[$m->ID]['ID'] = $m->ID;
$submenu[$m->ID]['title'] = $m->title;
$submenu[$m->ID]['url'] = $m->url;
$submenu[$m->ID]['description'] = $m->description; //Line added;
$menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID];
}
}
return $menu;
}

How to create custom CSV files using PHP?

In the $csv_array_attributes[0] represents every column for my csv file. sku is the product id. In $csv_array_attributes[0] represents all of the product ids for my csv(which is the rows value form column sku). In the query below I extract all of the products ids, attributes names and attributes values. My problem is that I have to compute my $csv array in order to assign for each attribute name(columns) the attribute values. In $final_string somehow I want to memorize all of the attributes values for a products (see the commented part). Thx in advance for the help. Im really stuck with this :(
$csv_array_attributes[0] = "sku%%".$header;
//GETTING ATTRIBUTES VALUES
$i = 1;
foreach($xml->children() as $content){
$id_produs = $content->ProductCode;
$csv_array_attributes[$i] = $id_produs;
$i++;
}
$select = mysql_query("SELECT id_prod.id_produs, GROUP_CONCAT('^^',nume_attr) AS nume_at, GROUP_CONCAT('^^',val_attr) AS val_at FROM attributes
INNER JOIN id_prod
ON id_prod.id_id_produs = attributes.id_produs
GROUP BY id_prod.id_produs");
$i = 0; $id_prod = array();
while ($row = mysql_fetch_array($select)){
$id_produs = $row['id_produs'];
$nume_attr = explode(",^^",substr($row['nume_at'],2));
$val_attr = explode(",^^",substr($row['val_attr'],2));
$id_prod[$id_produs] = $nume_attr;
$i++;
}
// var_dump(count($id_prod));
$nume = explode("%%", $csv_array_attributes[0]);
$csv[0] = $csv_array_attributes[0];
$i = 1;
foreach ($id_prod as $key => $value) {
// comment part
//$final_string = "";
//if (attr_name has attribute_value for product i ){
// $final_string.= attribute_value."%%";
//}else{
// $final_string.= "%%";
//}
//end comment part
$csv[$i] = $csv_array_attributes[$i]."%%".$final_string;
$i++;
}
//var_dump($csv_array_attributes[0])
//CREARE CSV
$file = fopen("attributes.csv","w+");
foreach ($csv as $line){
fputcsv($file,explode('%%',$line),"^","`");
}
To see the current result please click HERE
Actually I only can give you a hint how I would create a array for csv.
// header
$csv[0][0] = "one";
$csv[0][1] = "two";
$csv[0][2] = "three";
$csv[0][3] = "four";
$csv[0][4] = "five";
// body
$csv[1][0] = "some";
$csv[1][1] = "values";
$csv[1][2] = "that";
$csv[1][3] = "could";
$csv[1][4] = "be";
$csv[2][0] = "here";
$csv[2][1] = "in";
$csv[2][2] = "this";
$csv[2][3] = "little";
$csv[2][4] = "array";
This is only a sample to view the array setup.
If you have set up the header, lets say by a for you can fill the 2 demensional array.
If this is set up you can use a foreach to build your CSV
Sample
$seperator = ";";
$ln = "\r\n";
$csv_output = "";
foreach($csv as $c){
foreach($c as $value){
$csv_output .= $value . $seperator;
}
$csv_output . $ln;
}
csv output
one;two;three;four;five;
some;values;that;could;be;
here;in;this;little;array;
a row;with an;;empty;field;
empty fields are no problem since you seperate them all with a ;

Compare 2 text files and get internal id

I tried comparing 2 text files with data separated by -, in one case one file gets all data and in another case only has the data for change with the same id in this case this file it's called db_tmp.txt
The structure in both files it's this :
File txt ( the first it´s the id ) db/text.txt
1a34-Mark Jhonson
1a56-Jhon Smith
1a78-Mary Walter
The file for comparing, has for example the data for change, same id but different content - db_tmp.txt
1a56-Tom Tom
I created a function for comparing both files to detect if the same id and change exists:
<?php
$cron_file = file("db_tmp.txt");
$cron_compare = file("db/test.txt");
function cron($file_temp, $file_target)
{
for ($fte = 0; $fte < sizeof($file_temp); $fte++) {
$exp_id_tmp = explode("-", $file_temp[$fte]);
$cr_temp[] = "" . $exp_id_tmp[0] . "";
}
for ($ftt = 0; $ftt < sizeof($file_target); $ftt++) {
$exp_id_targ = explode("-", $file_target[$ftt]);
$cr_target[] = "" . $exp_id_targ[0] . "";
}
$diff = array_diff($cr_target, $cr_temp);
$it = 0;
foreach ($diff as $diff2 => $key) {
echo $diff2;
echo "--- $key";
print "<br>";
}
}
cron($cron_file, $cron_compare);
?>
If the same id exists in tmp, i must detect the entry in the other file and change to the value of the tmp, i try but can't get this to work, the function works but not for everything, if anyone can help me, that would be perfect, because i don´t know how continue this and save.
If you want to match according to id, a simple foreach would suffice, then just check during the loop if they have the same key. Consider this example:
// sample data from file('db_text.txt');
$contents_from_text = array('1a34-Mark Jhonson','1a56-Jhon Smith', '1a87-Mary Walter');
// reformat
$text = array();
foreach($contents_from_text as $element) {
list($key, $value) = explode('-', $element);
$text[$key] = $value;
}
$tmp = array();
// sample data from file('db_tmp.txt');
$contents_from_tmp = array('1a56-Tom Tom', '1a32-Magic Johnson', '1a23-Michael Jordan', '1a34-Larry Bird');
foreach($contents_from_tmp as $element) {
list($key, $value) = explode('-', $element);
$tmp[$key] = $value;
}
// compare
foreach($tmp as $key => $value) {
foreach($text as $index => $element) {
if($key == $index) {
$tmp[$key] = $element;
}
}
}
$contents_from_tmp = $tmp;
print_r($contents_from_tmp);
Sample Output

rename attribute value in foreach if multiple values exist same php magento

Hi i have a script which fetch all product data from magento , there is one problem occur which is there are some products whose name is same but sku is different , i want to append product name who has the same value other who has the unique value should not append...
<?php
#ob_start();
#session_start();
ini_set('display_errors', 1);
include '../../../../app/Mage.php';
umask(0);
Mage::app('default');
function empty_pk($data){
if($data!=''){return $data;}
else {return " ";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);
//echo'<pre>';
//print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$i=0;
foreach ($collection as $product_all) {
//echo $product_all->getId().'<br/>';
if($i==10) break;
$id = $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($id);
//echo'<pre>';
$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';
$description = $neew->getdescription();
$short_description = $neew->getshort_description();
$sku = $neew->getsku();
$size_fit = $neew->getsize_fit();
$style_ideas = $neew->getstyle_ideas();
$name = $neew->getname();
how can i do this
You can use temporary array for keeping names' counts and then filter by it:
function empty_pk($data){
if($data!=''){return $data;}
else {return " ";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);
$names = array();
foreach ($collection as $product_all) {
$name = $product_all->getName();
if (array_key_exists($name)) {
$names[$name]++;
} else {
$names[$name] = 1;
}
}
//echo'<pre>';
//print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$i=0;
foreach ($collection as $product_all) {
//echo $product_all->getId().'<br/>';
if($i==10) break;
$id = $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($id);
//echo'<pre>';
// Exit if unique.
if ($names[ $neew->getName() ] == 1) {
break;
}
$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';

Categories