Skip to content

Instantly share code, notes, and snippets.

@crazyyy
Last active February 2, 2022 06:39
Show Gist options
  • Save crazyyy/47960fd60a52ec761732b9c13efac51b to your computer and use it in GitHub Desktop.
Save crazyyy/47960fd60a52ec761732b9c13efac51b to your computer and use it in GitHub Desktop.
#php #wp #acf wordpress loop posts and update ACF fields

PHP favorite snippets

Enable logging

declare(strict_types=1);

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

Write string to file

$log = date('Y-m-d H:i:s') . ' Запись в лог';
file_put_contents(__DIR__ . '/log.txt', $log . PHP_EOL, FILE_APPEND);

Write array to file

// https://prowebmastering.ru/php-logging.html
$array = [
  'foo' => 'bar',
  'data' => [1, 2]
];

$log = date('Y-m-d H:i:s') . ' ' . print_r($array, true);
file_put_contents(__DIR__ . '/log.txt', $log . PHP_EOL, FILE_APPEND);

Результат работы PHP скрипта

ob_start();
// Вывод заголовков браузера.
foreach (get_headers('https://yandex.ru') as $name => $value) {
  echo "$name: $value\n";
}
$log = date('Y-m-d H:i:s') . PHP_EOL . ob_get_clean() . PHP_EOL;
file_put_contents(__DIR__ . '/log.txt', $log, FILE_APPEND);
// another example
$path = realpath(__DIR__);
$logPath = $path  . '/runtime/';

try {
  $command = "php $path/handler.php  > {$logPath}process.log 2>&1 &";
  exec($command);
} catch (Exception $exception) {
  echo $exception->getMessage();
}

Write PHP errors to logfile

ini_set('error_log', __DIR__ . '/php-errors.log');
error_log('Запись ошибки в лог', 0);

// More
$logPath = __DIR__ . '/runtime';
$errorMsg = date('Y/m/d H:i:s') . ': Сообщение в лог' . PHP_EOL;

error_log($errorMsg, 3, $logPath . '/errors.log');

// Simple logging function
function logger($message)
{
  $logPath = __DIR__ . '/runtime';
  if (!is_dir($logPath)) {
    mkdir($logPath, 0777, true);
  }

  $errorMsg = date('Y/m/d H:i:s') . ": $message" . PHP_EOL;
  error_log($errorMsg, 3, $logPath . '/errors.log');
}
// Вызываем логер
logger('Ошибка получения данных в файле ' . __FILE__);

Use Monolog

https://github.com/Seldaek/monolog

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logPath = __DIR__ . '/runtime';
$logger = new Logger('warning');
$logger->pushHandler(new StreamHandler("$logPath/warning.log", Logger::WARNING));
$logger->warning('Ошибка!', ['name' => 'John']);

<style>
.section-article table {
width: 100%;
max-width: 100%;
table-layout: fixed;
}
.pack-items {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-content: stretch;
align-items: flex-start;
padding-top: 5px;
padding-bottom: 5px;
}
.pack-items--title {
width: 100% !important;
margin-bottom: 5px;
padding-bottom: 5px;
font-size: 80% !important;
border-bottom: 1px dashed rgba(199, 97, 97, 0.4);
}
.pack-items--item {
width: 33%;
display: block;
text-align: center;
}
th:nth-child(1),
td:nth-child(1) {
width: 15% !important;
}
th:nth-child(2),
td:nth-child(2),
th:nth-child(3),
td:nth-child(3) {
width: 7.5% !important;
}
th:nth-child(4),
td:nth-child(4),
th:nth-child(5),
td:nth-child(5) {
width: 25% !important;
}
th:nth-child(6),
td:nth-child(6) {
width: 10% !important;
}
tr:nth-child(even) {
background: #CCC
}
tr:nth-child(odd) {
background: #FFF
}
</style>
<?php
function rand_float($st_num=0,$end_num=1,$mul=1000000) {
if ($st_num>$end_num) return false;
return mt_rand($st_num*$mul,$end_num*$mul)/$mul;
}
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 150,
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
'ignore_sticky_posts' => true,
'order' => 'ASC',
'orderby' => 'title',
);
$the_query = new WP_Query( $query_args );
echo '<table>';
echo '<tr>';
echo '<th class="col-1">';
echo 'Title';
echo '</th>';
echo '<th class="col-2">';
echo 'Pricing Old';
echo '</th>';
echo '<th class="col-3">';
echo 'Pricing New';
echo '</th>';
echo '<th class="col-4">';
echo 'Items';
echo '</th>';
echo '<th class="col-5">';
echo 'Updated';
echo '</th>';
echo '<th class="col-6">';
echo 'New Pricing';
echo '</th>';
echo '</tr>';
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); // start loop
echo '<tr>';
$price = get_field('price');
$old_price = get_field('old_price');
$sale_in_percentage = get_field('sale_in_percentage');
echo '<td>';
the_title();
echo '</td>';
echo '<td>';
echo $price;
echo '<br>';
echo $old_price;
echo '<br>';
echo '<br>';
if ($sale_in_percentage) {
echo '<strong>%</strong> ';
echo $sale_in_percentage;
}
echo '</td>';
echo '<td>';
$price = get_field('price');
$price_sale = get_field('price_sale');
echo $price;
echo '<br>';
echo '<span>';
if ($price_sale && $price_sale <= $price) {
echo '<span style="color: red;">';
echo $price_sale;
echo '</span>';
} else {
echo '<span>';
echo $price_sale;
echo '</span>';
}
echo '</span>';
echo '<br>';
if ($sale_in_percentage) {
echo '<strong>%</strong> ';
echo $sale_in_percentage;
}
echo '</td>';
echo '<td>';
echo '<span class="pack-items">';
echo '<span class="pack-items--item">Price</span>';
echo '<span class="pack-items--item">Sale</span>';
echo '<span class="pack-items--item"> * </span>';
echo '</span>';
if( have_rows('item_packages_and_quantity_variants') ):
echo '<span class="pack-items">';
echo '<span class="pack-items--item pack-items--title">Standart</span>';
echo '</span>';
$default_counter = 0;
while ( have_rows('item_packages_and_quantity_variants') ) : the_row();
$price = get_sub_field('package_costs');
$old_price = get_sub_field('costs_old');
$default_package = get_sub_field('default_package');
$price = get_sub_field('package_costs');
$old_price = strlen(get_sub_field('costs_old')) > 0 ? get_sub_field('costs_old') : false;
$price_new = get_sub_field('price') ? get_sub_field('price') : '123';
$price_sale = strlen(get_sub_field('price_sale')) > 0 ? get_sub_field('price_sale') : false;
$default_package = get_sub_field('default_package');
$temp_new_price = '';
$temp_new_sale = '';
if (abs(($price-$price_new)/$price_new) < 0.00001) {
// echo "same";
$isIdentical = true;
} else {
$isIdentical = false;
}
if (!$isIdentical) {
$isNotAFloat = (bool)random_int(0, 1);
echo '<span style="color: red;">another</span>';
$temp_new_price = rand_float(0.1, 600, 1000000);
$temp_new_price = abs($temp_new_price);
if ($isNotAFloat) {
$temp_new_price = (int) $temp_new_price;
}
$temp_new_price = abs($temp_new_price);
if (is_float($temp_new_price)) {
$temp_new_price = number_format($temp_new_price, 2);
}
$temp_new_sale = false;
if ($old_price) {
$temp_new_sale = rand_float(0.1, 100, 1000000);
$temp_new_sale = abs($temp_new_sale);
if ($isNotAFloat) {
$temp_new_sale = (int) $temp_new_sale;
}
$temp_new_sale = $temp_new_price - $temp_new_sale;
if (is_float($temp_new_sale)) {
$temp_new_sale = number_format($temp_new_sale, 2);
}
$temp_new_sale = abs($temp_new_sale);
$temp_new_sale = strval( $temp_new_sale );
update_sub_field('costs_old', $temp_new_sale, get_the_ID());
update_sub_field('price_sale', $temp_new_sale, get_the_ID());
}
$temp_new_price = abs($temp_new_price);
$temp_new_price = strval( $temp_new_price );
update_sub_field('costs_old', $temp_new_price, get_the_ID());
update_sub_field('price_sale', $temp_new_price, get_the_ID());
} else {
// echo "same ";
}
$price = get_sub_field('package_costs');
$old_price = strlen(get_sub_field('costs_old')) > 0 ? get_sub_field('costs_old') : false;
$price_new = get_sub_field('price');
$price_sale = strlen(get_sub_field('price_sale')) > 0 ? get_sub_field('price_sale') : false;
echo '<span class="pack-items">';
echo '<span class="pack-items--item">';
echo $price . ' (' . $price_new . ' - ' . $temp_new_price . ' - ) ';
echo '</span>';
if ($old_price) {
echo '<span class="pack-items--item">';
if ($old_price >= $price) {
update_sub_field('costs_old', $temp_new_sale, get_the_ID());
$old_price = strlen(get_sub_field('costs_old')) > 0 ? get_sub_field('costs_old') : false;
echo '<span style="color: red;">';
echo $old_price . ' (' . $price_sale . ' - ' . $temp_new_sale . ') ';
echo '</span>';
} else {
echo '<span style="color: #dde3e7;">';
echo $old_price . ' (' . $price_sale . ' - ' . $temp_new_sale . ') ';
echo '</span>';
}
echo '</span>';
}
echo '<span class="pack-items--item">';
if ($default_package) {
if ($default_counter <= 1) {
echo '<span style="color: red;">'; echo 'Default';
update_sub_field('default_package', 0, get_the_ID());
}
echo '<span>'; echo 'Default';
echo ' (' . $default_counter . ')';
echo '</span>';
$default_counter++;
}
echo '</span>';
echo '</span>';
endwhile;
endif;
if( have_rows('dosage_option') ):
$default_counter = 0;
while ( have_rows('dosage_option') ) : the_row();
if( have_rows('qty_and_cost_variants_for_this_dose') ):
echo '<span class="pack-items">
<span class="pack-items--item pack-items--title">Dosage</span>
</span>';
while( have_rows('qty_and_cost_variants_for_this_dose') ): the_row();
$price = get_sub_field('costs');
$old_price = strlen(get_sub_field('costs_old')) > 0 ? get_sub_field('costs_old') : false;
$price_new = get_sub_field('price') ? get_sub_field('price') : '123';
$price_sale = strlen(get_sub_field('price_sale')) > 0 ? get_sub_field('price_sale') : false;
$default_package = get_sub_field('default_package');
$temp_new_price = '';
$temp_new_sale = '';
if (abs(($price-$price_new)/$price_new) < 0.00001) {
// echo "same";
$isIdentical = true;
} else {
$isIdentical = false;
}
if (!$isIdentical) {
$isNotAFloat = (bool)random_int(0, 1);
echo '<span style="color: red;">another</span>';
$temp_new_price = rand_float(0.1, 600, 1000000);
$temp_new_price = abs($temp_new_price);
if ($isNotAFloat) {
$temp_new_price = (int) $temp_new_price;
}
$temp_new_price = abs($temp_new_price);
if (is_float($temp_new_price)) {
$temp_new_price = number_format($temp_new_price, 2);
}
$temp_new_sale = false;
if ($old_price) {
$temp_new_sale = rand_float(0.1, 100, 1000000);
$temp_new_sale = abs($temp_new_sale);
if ($isNotAFloat) {
$temp_new_sale = (int) $temp_new_sale;
}
$temp_new_sale = $temp_new_price - $temp_new_sale;
if (is_float($temp_new_sale)) {
$temp_new_sale = number_format($temp_new_sale, 2);
}
$temp_new_sale = abs($temp_new_sale);
$temp_new_sale = strval( $temp_new_sale );
update_sub_field('costs', $temp_new_sale, get_the_ID());
update_sub_field('price', $temp_new_sale, get_the_ID());
}
$temp_new_price = abs($temp_new_price);
$temp_new_price = strval( $temp_new_price );
update_sub_field('costs_old', $temp_new_price, get_the_ID());
update_sub_field('price_sale', $temp_new_price, get_the_ID());
} else {
// echo "same ";
}
$price = get_sub_field('costs');
$old_price = strlen(get_sub_field('costs_old')) > 0 ? get_sub_field('costs_old') : false;
$price_new = get_sub_field('price');
$price_sale = strlen(get_sub_field('price_sale')) > 0 ? get_sub_field('price_sale') : false;
echo '<span class="pack-items">';
echo '<span class="pack-items--item">';
echo $price . ' (' . $price_new . ' - ' . $temp_new_price . ' - ) ';
echo '</span>';
if ($old_price) {
echo '<span class="pack-items--item">';
if ($old_price <= $price) {
update_sub_field('price_sale', $temp_new_sale, get_the_ID());
$old_price = strlen(get_sub_field('price_sale')) > 0 ? get_sub_field('price_sale') : false;
echo '<span style="color: red;">';
echo $old_price . ' (' . $price_sale . ' - ' . $temp_new_sale . ') ';
echo '</span>';
} else {
echo '<span style="color: #dde3e7;">';
echo $old_price . ' (' . $price_sale . ' - ' . $temp_new_sale . ') ';
echo '</span>';
}
echo '</span>';
}
echo '<span class="pack-items--item">';
if ($default_package) {
if ($default_counter >= 1) {
echo '<span style="color: red;">'; echo 'Default';
update_sub_field('default_package', 0, get_the_ID());
}
echo '<span>'; echo 'Default';
echo ' (' . $default_counter . ')';
echo '</span>';
$default_counter++;
}
echo '</span>';
echo '</span>';
endwhile;
endif;
endwhile;
endif;
echo '</td>';
echo '<td class="second result">';
echo '<span class="pack-items">';
echo '<span class="pack-items--item">Price</span>';
echo '<span class="pack-items--item">Sale</span>';
echo '<span class="pack-items--item"> * </span>';
echo '</span>';
$product_price = false;
$product_price_sale = false;
if( have_rows('item_packages_and_quantity_variants') ):
echo '<span class="pack-items">';
echo '<span class="pack-items--item pack-items--title">Standart</span>';
echo '</span>';
$default_counter = 0;
$i = 0;
$product_price = false;
$product_price_sale = false;
while ( have_rows('item_packages_and_quantity_variants') ) : the_row();
$price = get_sub_field('price');
$price_sale = get_sub_field('price_sale');
$default_package = get_sub_field('default_package');
if ($i === 0) {
$product_price = $price;
$product_price_sale = false;
if (get_sub_field('price_sale')) {
$product_price_sale = get_sub_field('price_sale');
}
}
echo '<span class="pack-items">';
echo '<span class="pack-items--item">';
echo $price;
echo '</span>';
echo '<span class="pack-items--item">';
if ($price_sale && $price_sale >= $price) {
echo '<span style="color: red;">';
echo $price_sale;
echo '</span>';
} else {
echo '<span style="color: #dde3e7;">';
echo $price_sale;
echo '</span>';
}
echo '</span>';
echo '<span class="pack-items--item">';
if ($default_package) {
if ($default_counter <= 1) {
echo '<span style="color: red;">';
echo 'Default';
// update_sub_field('default_package', false, get_the_ID());
} else {
echo '<span>';
echo 'Default';
$product_price = $price;
$product_price_sale = false;
if (get_sub_field('price_sale')) {
$product_price_sale = get_sub_field('price_sale');
}
}
echo ' (' . $default_counter . ')';
echo '</span>';
$default_counter++;
}
echo '</span>';
echo '</span>';
$i++;
endwhile;
endif;
if( have_rows('dosage_option') ):
$i = 0;
$default_counter = 0;
$product_price = false;
$product_price_sale = false;
while ( have_rows('dosage_option') ) : the_row();
if( have_rows('qty_and_cost_variants_for_this_dose') ):
echo '<span class="pack-items">';
echo '<span class="pack-items--item pack-items--title">Dosage</span>';
echo '</span>';
while( have_rows('qty_and_cost_variants_for_this_dose') ): the_row();
$price = get_sub_field('price');
$price_sale = get_sub_field('price_sale');
$default_package = get_sub_field('default_package');
if ($i === 0) {
$product_price = $price;
$product_price_sale = false;
if (get_sub_field('price_sale')) {
$product_price_sale = get_sub_field('price_sale');
}
}
echo '<span class="pack-items">';
echo '<span class="pack-items--item">';
echo $price;
echo '</span>';
echo '<span class="pack-items--item">';
if ($price_sale && $price_sale >= $price) {
echo '<span style="color: red;">';
echo $price_sale;
echo '</span>';
} else {
echo '<span style="color: #dde3e7;">';
echo $price_sale;
echo '</span>';
}
echo '</span>';
echo '<span class="pack-items--item">';
if ($default_package) {
if ($default_counter <= 1) {
echo '<span style="color: red;">';
echo 'Default';
// update_sub_field('default_package', false, get_the_ID());
} else {
echo '<span>';
echo 'Default';
$product_price = $price;
$product_price_sale = false;
if (get_sub_field('price_sale')) {
$product_price_sale = get_sub_field('price_sale');
}
}
echo ' (' . $default_counter . ')';
echo '</span>';
$default_counter++;
}
echo '</span>';
echo '</span>';
$i++;
endwhile;
endif;
endwhile;
endif;
echo '</td>';
echo '<td>';
update_field('old_price', false, get_the_ID());
if ($product_price_sale) {
update_field('price', $product_price_sale, get_the_ID());
update_field('old_price', $product_price, get_the_ID());
update_field('price_sale', $product_price, get_the_ID());
}
echo $product_price;
echo '<br>';
echo $product_price_sale;
echo '</td>';
echo '</tr>';
endwhile; // end loop
endif;
echo '</table>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment