2016-11-24 58 views
0

在那裏我想在我的訂單woocommerce中添加自定義元框。我已經建立,但在那裏我有問題,每當我試圖在這裏保存它並沒有改變我的代碼:如何保存添加元框woocommerce wordpress

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(); 

} 

我應該怎麼做呢?有人給我的解決方案,所以我的代碼可以像我想要的那樣工作?

當我使用:

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

error

如果我只用:

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

should

現在

enter image description here

回答

1

您錯過了選擇驅動程序後保存順序的選擇驅動程序的id的代碼,以保存順序。

我沒有測試過代碼,但希望它有幫助。

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); 
+0

oke,那是幫助我。但在那裏它給我一個litle麻煩,當我使用: 'echo「」。$ row ['post_title']。「」;' 當我使用該代碼時,我的選擇要顯示任何數據。 有你的解決方案嗎? –

+0

對不起,我無法理解:(你可以在while循環中使用var_dump($ row)並顯示$ row中的內容) 和顯示元框的訂單頁面的屏幕截圖 – Yamu

+0

看看現在是否適用 – Yamu

相關問題