We are preparing a template for factory QC. The template is automatically filled with all the order information and it will then be printed for their use.
This is a part of the template where the order meta items are listed, whenever we try to get the order items we get all details, we need to extract them each one by one
<table width="100%" border="0" cellpadding="7" cellspacing="0" style="text-transform: uppercase; font-size: 12px;">
<?php foreach ( $this->get_order_items() as $item_id => $item ) ?>
<tr>
<td width="35%">
Order No:
</td>
<td width="65%" style="font-weight: bold; ">
#F1-<?php $this->order_number(); ?>
</td>
</tr>
<tr>
<td>
Model:
</td>
<td style="font-weight: bold; ">
<?php echo $item['meta']; ?> //order items
</td>
</tr>
<tr>
<td>
Design:
</td>
<td style="font-weight: bold; ">
<?php echo $item['name']; ?> // product name
</td>
</tr>
</table>
The $item['meta'] gives us the following
MODEL: A4
YEAR: 2022
MAKE: Audi
COLOR: BLACK
LINE COLOR: ORANGE
We only need the model for the model row, we will put the color information and year in another table lets say. How can we extract the model in this case
<?php $item_meta = $item['meta']; ?>
<?php foreach ($item_meta as $meta_id => $meta) ?>
<?php echo $meta['Model']; ?>
We tried to use this to go through the elements one by one and get the value by meta key but it is always empty.
Edit:
We were able to get the value for Model using
wc_get_order_item_meta( $item['item_id'], 'Model' );
You just need to put the 2-d array structure there as you can access the model from the items just like that.
$item['meta']['Model']
I am trying to generate the pdf through my wordpress plugin but it is repeating the same error.What would be solution for the problem?
I have already checked the empty space before and after php tag.
require_once(plugin_dir_path(__FILE__) . '/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('Our Code World');
$pdf->SetTitle('First printed pdf!');
// set default header data
$pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont('helvetica');
// set margins
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 10);
//set font
$pdf->SetFont('helvetica', '', 12);
$pdf->AddPage();
/*
* content run from here
*/
$content = '';
$content .= '
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th width="5%">ID</th>
<th width="30%">Name</th>
</tr>
';
$content .= '
<tr>
<td>2574</td>
<td>Rafiq</td>
</tr>
';
$content .= '</table>';
$pdf->writeHTML($content);
ob_end_clean();
$pdf->Output("sample.pdf", "I");
I also faced similar issue and I solved that by removing all attributes of table tags. TCPDF supports only limited attributes, try this
$content = '';
$content .= '<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>';
$content .= '<tr>
<td>2574</td>
<td>Rafiq</td>
</tr>';
$content .= '</table>';
include 'includes/session.php';
$range = $_POST['date_range'];
$ex = explode(' - ', $range);
$from = date('Y-m-d', strtotime($ex[0]));
$to = date('Y-m-d', strtotime($ex[1]));
$sql = "SELECT *, SUM(amount) as total_amount FROM deductions";
$query = $conn->query($sql);
$drow = $query->fetch_assoc();
$deduction = $drow['total_amount'];
$from_title = date('M d, Y', strtotime($ex[0]));
$to_title = date('M d, Y', strtotime($ex[1]));
require_once('../tcpdf/tcpdf.php');
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle('Payslip: '.$from_title.' - '.$to_title);
$pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont('helvetica');
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(TRUE, 10);
$pdf->SetFont('helvetica', '', 11);
$pdf->AddPage();
$contents = '';
$sql = "SELECT *, SUM(num_hr) AS total_hr, attendance.employee_id AS empid, employees.employee_id AS employee FROM attendance LEFT JOIN employees ON employees.id=attendance.employee_id LEFT JOIN position ON position.id=employees.position_id WHERE date BETWEEN '$from' AND '$to' GROUP BY attendance.employee_id ORDER BY employees.lastname ASC, employees.firstname ASC";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$empid = $row['empid'];
$casql = "SELECT *, SUM(amount) AS cashamount FROM cashadvance WHERE employee_id='$empid' AND date_advance BETWEEN '$from' AND '$to'";
$caquery = $conn->query($casql);
$carow = $caquery->fetch_assoc();
$cashadvance = $carow['cashamount'];
$gross = $row['rate'] * $row['total_hr'];
$total_deduction = $deduction + $cashadvance;
$net = $gross - $total_deduction;
$contents .= '
<h2 align="center">TechSoft IT Solutions</h2>
<h4 align="center">'.$from_title." - ".$to_title.'</h4>
<table cellspacing="0" cellpadding="3">
<tr>
<td width="25%" align="right">Employee Name: </td>
<td width="25%"><b>'.$row['firstname']." ".$row['lastname'].'</b></td>
<td width="25%" align="right">Rate per Hour: </td>
<td width="25%" align="right">'.number_format($row['rate'], 2).'</td>
</tr>
<tr>
<td width="25%" align="right">Employee ID: </td>
<td width="25%">'.$row['employee'].'</td>
<td width="25%" align="right">Total Hours: </td>
<td width="25%" align="right">'.number_format($row['total_hr'], 2).'</td>
</tr>
<tr>
<td></td>
<td></td>
<td width="25%" align="right"><b>Gross Pay: </b></td>
<td width="25%" align="right"><b>'.number_format(($row['rate']*$row['total_hr']), 2).'</b></td>
</tr>
<tr>
<td></td>
<td></td>
<td width="25%" align="right">Deduction: </td>
<td width="25%" align="right">'.number_format($deduction, 2).'</td>
</tr>
<tr>
<td></td>
<td></td>
<td width="25%" align="right">Cash Advance: </td>
<td width="25%" align="right">'.number_format($cashadvance, 2).'</td>
</tr>
<tr>
<td></td>
<td></td>
<td width="25%" align="right"><b>Total Deduction:</b></td>
<td width="25%" align="right"><b>'.number_format($total_deduction, 2).'</b></td>
</tr>
<tr>
<td></td>
<td></td>
<td width="25%" align="right"><b>Net Pay:</b></td>
<td width="25%" align="right"><b>'.number_format($net, 2).'</b></td>
</tr>
</table>
<br><hr>
';
}
$pdf->writeHTML($contents);
$pdf->Output('payslip1.pdf', 'I');
I'm sorry if this is an old question but I really can't find answer for this. I am new to wordpress plugin development. How can I do a somewhat like a callback before a form is submitted using WP Options API? In a native PHP, we can just say as
if (isset($_POST['submit'])) {
$options = $_POST['zt_ftp_settings'];
var_dump($options);
}
However, I am using the WP Options API. It saves the data correctly, I just want to do some more after saving the data.
Here is what I did,
<form method="post" action="options.php">
<?php
settings_fields('zt_ftp_settings_options' );
do_settings_sections( 'zt_ftp_settings_options' );
$option = get_option('zt_ftp_settings');
?>
<h2>FTP Settings</h2>
<p>Please provide your ftp credentials.</p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">FTP Hostname</th>
<td>
<input name="zt_ftp_settings[zt_ftp_host_name]" type="text" id="zt_ftp_host_name" value="<?php echo $option['zt_ftp_host_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Username</th>
<td>
<input name="zt_ftp_settings[zt_ftp_user_name]" type="text" id="zt_ftp_user_name" value="<?php echo $option['zt_ftp_user_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Password</th>
<td>
<input name="zt_ftp_settings[zt_ftp_password]" type="password" id="zt_ftp_password" value="<?php echo $option['zt_ftp_password']?>" class="regular-text">
</td>
</tr>
</tbody>
</table>
<?php submit_button( 'Save Settings' ); ?>
Thanks.
Use WP AJAX functionality.
Use below JS,
$("#submit").click(function(){
var url = window.location.href;
jQuery.post(
ajaxurl,
{
'action': 'delete_search',
'tid': tid,
},
function(response){
location.reload();
});
});
Replace your submit button ID and use your form values.
Use below code in function.php
function prefix_ajax_delete_search() {
global $wpdb;
$user_id = get_current_user_id();
$wpdb->delete('sf_save_search', array('id' => $_POST['tid']) );
wp_die();
}
add_action( 'wp_ajax_delete_search', 'prefix_ajax_delete_search' );
As per your requirement change the function name and variables
i have built several diffrent templates for my WP site each calling diffrent metas.
ok, im looking for a way to add a meta-box that will only display if the correct page template is selected.
say i have a homepage.php template, this template calls some specific meta info that only the homepage uses, a call to action of example, so i need the meta-box to appear once the proper page template is selected and disappear if a different page template is selected.
any help is appreciated.
get_page_template will return the template you are looking at. You can switch on this to display the content you want.
ok i finally found a way around it guys and here it is:
<?php
function my_admin() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php'){
add_meta_box( 'product_info',
'Informacion de Producto',
'display_product_info_meta_box',
'ex_producto', 'normal', 'high'
);
}
}
function display_product_info_meta_box( $product_info ) {
$product_price = floatval( get_post_meta( $product_info->ID, 'product_price', true ) );
$product_code = esc_html( get_post_meta( $product_info->ID, 'product_code', true ) );
?>
<table>
<tr style="width: 100%">
<td >Precio Producto</td>
<td><input type="text" size="80" name="product_info_product_price" value="<?php echo $product_price; ?>" /></td>
</tr>
<tr style="width: 100%">
<td >Codigo de Producto</td>
<td><input type="text" size="80" name="product_info_product_code" value="<?php echo $product_code; ?>" /></td>
</tr>
</table>
<?php
}
add_action( 'admin_init', 'my_admin' );
?>
so... yeap i hope that dose someone out there some good :)
I have created a menu to manage user records for wordpress admin. While listing the record I have used check box to select and unselect the record as in Pages menu.
I have to select whole address or name on checkbox click. But here what happens is that when I click on name it selects whole checkbox including name and address. I do not have an idea how the check box works.
Here is the code
<?php
foreach($record as $record_row)
{
$name = $record_row->name;
$address = $record_row->name;
?>
<tr>
<th class="check-column" scope="row">
<input name="names[]" class="one" type="checkbox" value="<?php echo $record_row->ID; ?>">
</th>
<th class="check-column" scope="row">
<input name="address[]" class="two" type="checkbox" id="address[]" value="<?php echo $record_row->ID; ?>">
</th>
<td class="column-icon media-icon" width="30%"><?php echo $record_row->name; ?></td>
<td class="post-title page-title column-title"><?php echo $record_row->name; ?></td>
</tr>
<?php
}?>
Can anyone help me. Thanks in advance.
Get rid of the id attribute on the checkbox - it's invalid syntax for starters, and every id in your document must be unique!