如何在wordpress产品页面中保存设置?

时间:2016-04-06 05:55:38

标签: mysql wordpress woocommerce

enter image description here

我试过以下代码:

      if(isset($_POST['save']))
            {
                   global $wpdb,$product;
                    $id = $product->id;

                    $custommsg = sanitize_text_field( $_POST['customstock-msg'] );
                    $customprocessingtime = sanitize_text_field( $_POST['customstock-Processing-time'] );
                    $customstockquantity = sanitize_text_field( $_POST['customstock-quantity'] );
                    $customstockcatlogpage = sanitize_text_field( $_POST['customstock-catlogpage'] );
                    $customstockinstockdate = sanitize_text_field( $_POST['customstock-instockdate'] );
                    $customstockinstockdate = date("Y-m-d", strtotime($customstockinstockdate) );

 $wpdb->insert('wp_woocommerce_specific_product_settings', array(
                                                                    'custom_msg' => $custommsg,
                                                                    'order_processing_time'  => $customprocessingtime,
                                                                    'exp_instock_date' => $customstockinstockdate, 
                                                                    'show_stockstatus_quantity' => $customstockquantity,
                                                                    'showon_catlog' => $customstockcatlogpage,
                                                                    'specific_product_id' =>  $id
                                                                ));
            }

我是wordpress的初学者,任何人都可以提供解决方案。

1 个答案:

答案 0 :(得分:2)

您只需要使用woocommerce的动作挂钩woocommerce_process_product_meta

以下是应该进入functions.php文件的代码。您可能希望根据需要重命名自定义元字段。 (在update_post_meta声明中)。

add_action( 'woocommerce_process_product_meta', 'wp_woo_save_product_custom_meta' );
function wp_woo_save_product_custom_meta( $post_id ){
    $custommsg = sanitize_text_field( $_POST['customstock-msg'] );
    if( !empty( $custommsg ) ){
        update_post_meta( $post_id, 'customstock_msg', $custommsg ) );
    }
    $customprocessingtime = sanitize_text_field( $_POST['customstock-Processing-time'] );
    if( !empty( $customprocessingtime ) ){
        update_post_meta( $post_id, 'customstock_Processing_time', $customprocessingtime ) );
    }
    $customstockquantity = sanitize_text_field( $_POST['customstock-quantity'] );
    if( !empty( $customstockquantity ) ){
        update_post_meta( $post_id, 'customstock_quantity', $customstockquantity ) );
    }
    $customstockcatlogpage = sanitize_text_field( $_POST['customstock-catlogpage'] );
    if( !empty( $customstockcatlogpage ) ){
        update_post_meta( $post_id, 'customstock_catlogpage', $customstockcatlogpage ) );
    }
    $customstockinstockdate = sanitize_text_field( $_POST['customstock-instockdate'] );
    $customstockinstockdate = date("Y-m-d", strtotime($customstockinstockdate) );
    if( !empty( $customstockinstockdate ) ){
        update_post_meta( $post_id, 'customstock_instockdate', $customstockinstockdate ) );
    }
}