【问题标题】:How to save settings in wordpress product page?如何在 wordpress 产品页面中保存设置?
【发布时间】:2016-04-06 05:55:38
【问题描述】:

我已经尝试了以下代码:

      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初学者,谁能给出解决方案。

【问题讨论】:

    标签: mysql wordpress woocommerce


    【解决方案1】:

    您只需要使用 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 ) );
        }
    }
    

    【讨论】:

    • 如果我想存储在另一个表中应该怎么做?
    • 为什么要存储在另一个表中?我要问的原因是,以 wordpress 的原生方式保存元数据总是更好,这样就可以使用 wordpress/woocommerce 原生函数检索它,这超出了自定义范围。
    • 那么在插入后元数据后如何检索元值我是初学者,所以我不知道。
    • 通过使用get_post_meta在此处了解更多信息:developer.wordpress.org/reference/functions/get_post_meta
    • 我猜你正在使用 wordpress 和 woocommerce 组合开发一些电子商务门户。我希望以下 2 个链接可以帮助您了解更多信息。 Wordpress Function References AND WooCommer Action and Filter Hook Reference 您将能够在以上 2 个链接上找到大多数问题的解决方案。干杯,编码愉快。
    猜你喜欢
    • 2018-07-23
    • 2021-05-25
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多