Adam Projo Adam Projo - 10 days ago 6
PHP Question

How to Save Add meta box woocommerce wordpress

In there i want to add custom meta box in my order woocommerce. i've already create it but in there i have problem whenever i try to save its doesnt change here my code :



add_action( 'add_meta_boxes', 'add_meta_boxes' );

function add_meta_boxes()
{
add_meta_box(
'woocommerce-order-my-custom',
__( 'Order Driver' ),
'order_my_custom',
'shop_order',
'side',
'default'
);
}
function order_my_custom()
{
$servername = "localhost";
$username = "root";
$password = "no_password";
$dbname = "testtable";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM wp_posts WHERE post_type='drivers'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

echo "<select style='width:100%;' >";
echo "<option> --- Select ---</option>";
while($row = $result->fetch_assoc()) {
echo "<option value=".$row['ID'].">".$row['post_title']."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();

}




what should i do now ? have someone give me solution so my code can work like what i want ?

when i use :

echo "<option".($selected=='')?'':'selected'." value=".$row['ID'].">".$row['post_title']."</option>";


error

if i only use :

echo "<option value=".$row['ID'].">".$row['post_title']."</option>";


should

now :

enter image description here

Answer

You are missing the code to save the selected driver's id in order meta while saving the order after selecting driver.

I've not tested the code but hope it helps.

add_action( 'add_meta_boxes', 'add_meta_boxes' );

function add_meta_boxes()
{
    add_meta_box( 
        'woocommerce-order-my-custom', 
        __( 'Order Driver' ), 
        'order_my_custom', 
        'shop_order', 
        'side', 
        'default' 
    );
}
function order_my_custom()
{ 
  global $woocommerce, $post;
  $order_id  =$post->ID;
  $servername = "localhost";
  $username = "root";
  $password = "no_password";
  $dbname = "testtable";

  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  } 

  $sql = "SELECT * FROM wp_posts WHERE post_type='drivers'";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    ob_start();?>
   <select name='order_driver' style='width:100%;' >
    <option value=""> --- Select ---</option>
    <?php
    while($row = $result->fetch_assoc()) {
        $order_driver = get_post_meta($order_id,'order_driver',true);
    ?>
    <option <?php echo ($row['ID']==$order_driver)?'selected':''?> value="<?php echo $row['ID']?>"><?php echo $row['post_title']?></option>

  <?php  } ?>
   </select>
  <?php } else {?>
   <option value="">0 Results</option>
    <?php }
    echo ob_get_clean();
$conn->close();

} 

function save_drivers_meta( $post_id, $post, $update ) {

    $post_type = get_post_type($post_id);

    /*
     * Make sure we are updating post meta only for shop orders
     */
    if ( "shop_order" != $post_type ) return;

    // - Update the post's order_driver value if it's set.

    if ( isset( $_POST['order_driver'] ) ) {
        update_post_meta( $post_id, 'order_driver', sanitize_text_field( $_POST['order_driver'] ) );
    }
}
add_action( 'save_post', 'save_drivers_meta', 10, 3 );
Comments