Jump to content

jarvis

Members
  • Posts

    543
  • Joined

  • Last visited

Everything posted by jarvis

  1. Is visitor a custom role? As a Wordpress user is generally only assigned one role, can you not simply do something like: #only show content to specific roles $user = wp_get_current_user(); if ( $user == 'subscriber' || $user == 'visitor' ){ // adsense code }
  2. If you create a loop for all your posts, you could then use 'get_page_by_title' within the loop. This will return the post ID. Once you have the post ID, you can then use 'update_post_meta' Obviously, it would depend what you're trying to update the custom field with. Are you not able to simply edit the post via the admin area?
  3. @Barand Yep, was being called recursively! #facePalm Thanks
  4. Hi @Barand I realised this when I went to post the example of data, so rectified it. My full code looks like this: <?php add_action( 'wp_ajax_nopriv_get_batch_orders_from_api', 'get_batch_orders_from_api' ); add_action( 'wp_ajax_get_batch_orders_from_api', 'get_batch_orders_from_api' ); function get_batch_orders_from_api() { // get yesterdays file $yesterdays_filename = "purchase_history_".date('Ymd',strtotime("-1 days"))."_001.csv"; $previous_data_file = get_stylesheet_directory() . '/files/'.$yesterdays_filename; // delete yesterdays file if(file_exists($previous_data_file)){ unlink( $previous_data_file ); echo 'The file ' . $previous_data_file . ' has been removed!<br/>'; } // create todays file $filename = "purchase_history_".date("Ymd")."_001.csv"; $order_data = get_stylesheet_directory() . '/files/'.$filename; if (file_exists($order_data)) { echo 'The file ' . $filename . ' already exists, data will now append the file<br/>'; } else { echo 'The file ' . $filename . ' does not exist and has been created!<br/>'; $headers = '"SKU",'; $headers .= '"Title",'; $headers .= '"First_Name",'; $headers .= '"Email",'; $headers .= '"Country",'; $headers .= '"PurchaseDate",'; $headers .= '"OrderRef",'; $headers .= '"CustomerRef",'; $headers .= '"Price",'; $headers .= '"Currency",'; $headers .= '"DeliveryDate",'; $headers .= '"SalesChannel"'; file_put_contents( $order_data, $headers . "\n", FILE_APPEND ); } $orders = []; // Should return an array of objects $results = wp_remote_retrieve_body( wp_remote_get('https://www.domain.com/batch-orders.php') ); // turn it into a PHP array from JSON string $results = json_decode( $results ); // Either the API is down or something else spooky happened. Just be done. if( ! is_array( $results ) || empty( $results ) ){ return false; } $orders[] = $results; foreach( $orders[0] as $order ){ $result = '"'.$order->sku. '",'; $result .= '"'.$order->title.'",'; $result .= '"'.$order->first_name.'",'; $result .= '"'.$order->email.'",'; $result .= '"'.$order->country.'",'; $result .= '"'.$order->purchase_date.'",'; $result .= $order->order_ref.','; $result .= $order->customer_ref.','; $result .= number_format($order->price, 2, '.', ''); $result .= '"'.$order->currency.'",'; $result .= '"'.$order->delivery_date.'",'; $result .= '"'.$order->sales_channel. "\n"; echo $result; file_put_contents( $order_data, $result, FILE_APPEND | LOCK_EX ); } $current_page = $current_page + 1; wp_remote_post( admin_url('admin-ajax.php?action=get_batch_orders_from_api'), [ 'blocking' => false, 'sslverify' => false, // we are sending this to ourselves, so trust it. 'body' => [ 'current_page' => $current_page ] ]); } If I check the output of batch-orders.php. this is the data: [{"sku":"JK115-W","title":"","first_name":"John Smith","email":"name@domain.com","country":"United Kingdom","purchase_date":"14\/02\/2020","order_ref":"683","customer_ref":"683","price":"34.99","currency":"GBP","delivery_date":"15\/02\/2021","sales_channel":"Online"}] The output of $result is the following: "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" But as mentioned, the CSV file generated just seems to loop the same line over and over, yet only appears once. Hopefully, that helps?
  5. Sorry! No, it's exactly the same. Whilst the output of the loop on the screen shows 1 result, the file seems to be on a continuous loop and will keep adding exactly the same information. It's never mixed, it doesn't alter or deviate. There are no breaks Which is why I'm stumped. If it outputs once on the screen and the file_put_contents is in the same loop, surely it should only write the same number of times it appears on the screen!?
  6. It's exactly the same line but just repeated many, many times. For example: "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online"
  7. Morning! I'm taking an array, looping through and placing the data into a CSV file. If I echo the foreach data, it only gets displayed once. If I open the CSV file, the same line is added numerous times. foreach( $orders[0] as $order ){ $result = '"'.$order->sku. '",'; $result .= '"'.$order->title.'"'; $result .= '"'.$order->first_name.'"'; $result .= '"'.$order->email.'"'; $result .= '"'.$order->country.'"'; $result .= '"'.$order->purchase_date.'"'; $result .= $order->order_ref; $result .= $order->customer_ref; $result .= number_format($order->price, 2, '.', ''); $result .= '"'.$order->currency.'"'; $result .= '"'.$order->delivery_date.'"'; $result .= '"'.$order->sales_channel.'"'; $result .= $order->member_id; $result .= $order->member_branch_id. "\n"; echo $result; file_put_contents( $order_data, $result, FILE_APPEND ); } Have I missed something? Surely if it's displayed once, it should only be added once to the CSV file? Thanks
  8. This can be ignored. I found an alternative solution Apologies!
  9. Hi All, I hope I've used the right/correct term for this. So I'm using: array_map(addQuotes, $line) Which calls this function: function addQuotes($line) { return "\"$line\""; } It works and adds quotes around everything I need. However, I need to exclude one column called SKU. Is it possible to use the above but skip this column? I know using array_map means you need to return the same number of columns, so my thought was: function addQuotes($line) { if( $line['SKU']){ return $line; } else { return "\"$line\""; } } Therefore not applying the quote treatment. Sadly, didn't work (not surprised as figured it was too obvious! The data is an array: Array ( [0] => Array ( [0] => SKU [1] => Name [2] => Product Category [3] => ImageURL [4] => Manufacturer [5] => Model [6] => MPN [7] => EAN [8] => MemberID ) [1] => Array ( [SKU] => CIHYG21SX [Name] => Beko CIHYG21SX 60cm Gas Hob - Stainless Steel [Product Category] => Gas Hobs [ImageURL] => https://www.domain.com/uploads/2021/Beko-CIHYG21SX.png [Manufacturer] => Beko [Model] => CIHYG21SX [MPN] => CIHYG21SX [EAN] => 8690842386756 [MemberID] => 456789 ) [2] => Array ( [SKU] => T26DS49N0 [Name] => Neff T26DS49N0 60cm Gas Hob - Stainless Steel [Product Category] => Gas Hobs [ImageURL] => https://www.domain.com/uploads/2018/Neff-T26DS49N0.jpg [Manufacturer] => Neff [Model] => T26DS49N0 [MPN] => T26DS49N0 [EAN] => 4242004203384 [MemberID] => 456789 ) ) I'm then using a loop to get the data and pass into my function (if that helps) No doubt something I've overlooked but any help is much appreciated!
  10. No, the file doesn't exist - which I get. I can't see how to add something to the file without it being repeated, as this is the issue I keep getting. I switched from file_put_contents to fopen and have achieved what I need. Why it doesn't work using file_put_contents I don't know $products[] = $results; $data = array(); if( $current_page == 1 ){ $data[] = array( 'id', 'name' ); } foreach( $products[0] as $product ){ $result .= $product->id. ','; $result .= '"'.$product->name.'"'; $data[] = array( 'id' => $product->id, 'name' => $product->name ); file_put_contents( $product_data, $result . "\n", FILE_APPEND | LOCK_EX ); $i++; } $file = fopen($product_data2,"a"); foreach ($data as $line) { fputcsv($file, $line); } fclose($file);
  11. Would it be when you declare the file? I'm clearly missing something because surely being recursive, it will just keep adding to the file
  12. Yes that's right. So the first iteration adds the header
  13. Hi All, I've a function that pulls data from a paginated API. The function is called recursively in order to work through each page and get the data. Within the function, I use file_put_contents to write the information to a CSV file. What I'd like to do, is add a first row/header to the CSV file with column names. I'm probably being daft but if I add the header information in, it gets repeated on each iteration of the loop. Here's my code: function get_from_api() { #$page_report = get_stylesheet_directory() . '/page-report.txt'; $filename = 'products.csv'; $product_data = get_stylesheet_directory() . '/'.$filename; $current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1; $products = []; // Should return an array of objects $results = wp_remote_retrieve_body( wp_remote_get('www.domain.com/batch.php?page=' . $current_page . '&per_page=10') ); // create a file to test the output $data = 'Current Page: '.$current_page; #file_put_contents( $page_report, $data . "\n\n", FILE_APPEND ); // turn it into a PHP array from JSON string $results = json_decode( $results ); // Either the API is down or something else spooky happened. Just be done. if( ! is_array( $results ) || empty( $results ) ){ return false; } #print_r($results); $products[] = $results; foreach( $products[0] as $product ){ $result .= $product->id. ','; $result .= $product->name; file_put_contents( $product_data, $result . "\n", FILE_APPEND ); } $current_page = $current_page + 1; wp_remote_post( admin_url('admin-ajax.php?action=get_from_api'), [ 'blocking' => false, 'sslverify' => false, // we are sending this to ourselves, so trust it. 'body' => [ 'current_page' => $current_page ] ]); } Any help would be great Thanks
  14. Sorted it with usort($productions, function($a1, $a2) { $v1 = strtotime($a1['year']); $v2 = strtotime($a2['year']); return $v2 - $v1; // $v2 - $v1 to reverse direction });
  15. Hi, I have a loop which creates an array: $productions[] = array( 'url'=>get_the_permalink(), 'year'=>$production_year->name, 'title'=>get_the_title() ); When I output the results, the year is in the wrong order: 2019 2018 2017 2016 2015 2014 2020 What I can't work out is how to order 2014 - 2020 I tried ksort(array, SORT_STRING) and ksort(array, SORT_NUMERIC) I also tried natsort But still have the same issue - am I missing something? Thanks
  16. @requinix Many thanks, that solved the issue! Have a great Christmas!
  17. Hi All, I hope someone can help! So I have a basic form which uses Select2. If someone uses the first field, it disables the remaining fields - this works fine. The issue is if you clear the selection in the first drop down, then pick a value from another drop down, it seems to select your choice but also adds 'Select' as an option. I've created a JS Bin to help show the issue: View the JS Bin Any help is appreciated Thanks
  18. Hi, Am hoping someone can please help. I've the following code: $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_method = explode(':', reset($chosen_methods) ); The first line returns the warning: "Warning: reset() expects parameter 1 to be array, null given in" I've tried the following: $chosen_method = ''; $chosen_method = NULL; $chosen_methods = array(); Sadly, none have worked. What am I missing? Thanks
  19. I see what you're saying but I'm still at a loss, I can see the docs but I cannot fathom how to do what I need "ajax": { "method" :"POST", "url" : '<?php echo admin_url('admin-ajax.php');?>', "data": function ( d ) { return $.extend( {action:action,tax:tax,id:id}, d, { "searchResultsHeader": $('.search-results-header').val() } ); } } ??
  20. Hi @requinix I tried to adjust example.on( 'xhr', function ( e, settings, json ) {// check is the response is not null and show the table if (json != null) { $('#example').removeClass("hide-me",""); jQuery(".search-results-header").html(data); } } ); I also tried adding d.search-results-header = $('#search-results-header').val(); Into the render part of the code I'll be honest, I'm struggling to make sense of it, hence turning to this forum for assistance. I'd previously read the link you sent and struggled to make sense of it. I also Google'd for examples, hence basing my code on someone else's example and adapting to my requirements. Less than ideal I know but got my code working. A lot of examples I found were using GET whereas I needed POST. Thanks
  21. Thanks @requinix When I previously tried ajaxifying the content, the search on the table didn't work. The code above worked and still enables the search functionality I just can't quite see how to do/integrate the other part
  22. @requinix I was going aiming to to insert it into that div. So table content goes to table and additional info to here. Or is it better to do it another way?
  23. Hi All, I use a jQuery datatable to display information from a search. The results are posted back via Ajax to a default table placeholder like so: <table id="example" class="table table-striped table-responsive hide-me" style="width:100%"> <thead> <tr> <th>Sponsor</th> <th>Nationality</th> <th>Sector</th> <th>Team Sponsored</th> <th>Sport</th> </tr> </thead> <tfoot> <tr> <th>Sponsor</th> <th>Nationality</th> <th>Sector</th> <th>Team Sponsored</th> <th>Sport</th> </tr> </tfoot> </table> This is handled by the following: $(document).on('click', '.taxonomy-sponsor-link', function(){ var action = 'get_sponsors_by_taxonomy'; var id = $(this).data('id'); var tax = $(this).data('tax'); //alert('action: '+action+' tax: '+tax+' id: '+id); //assing the datatable call to a variable let example = $('#example').DataTable({ "destroy": true, "responsive":{//this is usefull if you want to use a full responsive datatable just add the responsive css from dataTables.net "details": { renderer: function ( api, rowIdx, columns ) { var data = $.map( columns, function ( col, i ) { return col.hidden ? '<tr data-dt-row="'+col.rowIndex+'" data-dt-column="'+col.columnIndex+'">'+ '<td>'+col.title+':'+'</td> '+ '<td>'+col.data+'</td>'+ '</tr>' : ''; } ).join(''); return data ?$('<table/>').append( data ) :false; } } }, "autoWidth": false,//fix Bootstrap 4 glitch "ajax": { method :"POST", url : '<?php echo admin_url('admin-ajax.php');?>', data : {action:action,tax:tax,id:id,}, }, "columns": [ {"data": "sponsor"}, {"data": "nationality"}, {"data": "sector"}, {"data": "team_sponsored"}, {"data": "sport"} ], "columnDefs": [ { //"className": "dt-center", "targets": "_all" "className": "", "targets": "_all" } ] }); example.on( 'xhr', function ( e, settings, json ) {// check is the response is not null and show the table if (json != null) { $('#example').removeClass("hide-me",""); } } ); }); And my PHP gathers the data and passes it back via: echo json_encode($json); What I'd like to do, is also pass 1 other value from the results but not in the table. So wanted to use something like: <div id="search-results-header"></div> After an bit of searching, I can see you can alter the json_encode to be an array, so something like: echo json_encode(array( 'example' => $json, 'search-results-header' => $search )); However, I then can't work out whether: a) This is correct b) How I adjust the above call to output the table results AND the additional parameter. I wonder if someone would be kind enough to please assist? Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.