将 Woocommerce 产品字段的默认值设置为所有产品

我使用本教程为我的产品创建了一个自定义字段,但我需要为此字段设置默认值。我已经创建了数百种产品,我也需要更新此字段。然后我使用这个值来订购产品。

这是完整的代码:

// Display Fields

    add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

    // Save Fields

    add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

    function woocommerce_product_custom_fields()

    {

        global $woocommerce, $post;

        echo '<div class="product_custom_field">';

        // Custom Product Text Field

        woocommerce_wp_text_input(

            array(

                'id' => 'priority',

                'placeholder' => 'Priority of the product - values a>b',

                'label' => __('Priority of the product', 'woocommerce'),

                'desc_tip' => 'true',

                //'default' => '0',

            )

        );

        echo '</div>';

    }


    function woocommerce_product_custom_fields_save($post_id)

    {

        // Custom Product Text Field

        $woocommerce_custom_product_text_field = $_POST['priority'];

        if (!empty($woocommerce_custom_product_text_field))

            update_post_meta($post_id, 'priority', esc_attr($woocommerce_custom_product_text_field));

    }


    function cw_add_postmeta_ordering_args( $args_sort_cw ) {

      $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :

            apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

      switch( $cw_orderby_value ) {

           case 'priority':

                $args_sort_cw['orderby'] = 'meta_value';

                $args_sort_cw['order'] = 'asc';

                $args_sort_cw['meta_key'] = 'priority';

                break;

      }


我找不到可行的解决方案,所以我恳请您提供建议。我期望的是,每个现有产品都会设置此值,并且在创建新产品时将显示默认值。那么就应该按照这个值来排序。感谢您的帮助!


根据第一个答案进行编辑:数据库中的数据不会更新。在前端我只能看到我手动更改的产品,而看不到使用此功能添加 0 的产品。当我尝试将更改后的值更改回 0 时,它不会更新。实际上我只需要一个可以刷新所有产品的函数,以便将值存储到数据库中。


森林海
浏览 79回答 2
2回答

饮歌长啸

我要做的就是使用参数value中的参数woocommerce_wp_text_input。然后,您可以向其传递当前值或默认值(如果尚不存在)。还添加了新的保存例程woocommerce_admin_process_product_object// Display Fieldsadd_action('woocommerce_product_options_general_product_data', 'so_63588126_product_custom_fields');// Save Fieldsadd_action('woocommerce_admin_process_product_object', 'so_63588126_product_custom_fields_save');function so_63588126_product_custom_fields(){&nbsp; &nbsp; global $product_object;&nbsp; &nbsp; $value = $product_object->get_meta( 'weight', true );&nbsp; &nbsp; if ( ! $value ) {&nbsp; &nbsp; &nbsp; &nbsp; $value = 0;&nbsp; &nbsp; }&nbsp; &nbsp; echo '<div class="product_custom_field">';&nbsp; &nbsp; // Custom Product Text Field&nbsp; &nbsp; woocommerce_wp_text_input(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => 'weight',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'placeholder' => 'Weight of the product - values a>b',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __('Weight of the product', 'your-textdomain'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'desc_tip' => 'true',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $value&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; &nbsp; echo '</div>';}function so_63588126_product_custom_fields_save( $product ){&nbsp; &nbsp; // Custom Product Text Field&nbsp; &nbsp; $woocommerce_custom_product_text_field = $_POST['weight'];&nbsp; &nbsp; if ( ! empty( $_POST['weight'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $product->update_meta_data( 'weight', sanitize_text_field( wp_unslash( $_POST['weight'] ) ) );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $product->update_meta_data( 'weight', 0 );&nbsp; &nbsp; }}补充说明:我使用$product_object->update_meta_data()WooCommerce(自 3.0 起)更喜欢在对象上使用 CRUD(创建、读取、更新、删除)方法。update_post_meta()仍然有效,但如果/当 Woo 决定对产品和产品元使用自定义表时,CRUD 将是面向未来的。了解有关 CRUD 的更多信息:https ://docs.woocommerce.com/document/developing-using-woocommerce-crud-objects/关于文本域...这是自定义代码,因此为了正确翻译它,您需要使用您自己的独特文本域,而不是“woocommerce”。阅读有关文本域的更多信息:https ://developer.wordpress.org/themes/functionality/internationalization/#text-domain最后,sanitize_text_field()可能不是清理的正确选择,但我不知道您在那里存储了什么样的数据。intval()对于数字会更好...因此可以通过将参数设置为 args将type输入设置为数字输入类型。'number'woocommerce_wp_text_input()

阿波罗的战车

这是对我有用的解决方案。// Display Fieldsadd_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');// Save Fieldsadd_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');    function woocommerce_product_custom_fields()    {        global $woocommerce, $post, $product_object;        $value = $product_object->get_meta( 'priority', true );        if ( ! $value ) {            $value = 0;        }                echo '<div class="product_custom_field">';        // Custom Product Text Field        woocommerce_wp_text_input(            array(                'id' => 'priority',                'placeholder' => 'Priority - number 1>0',                'label' => __('Priority', 'woocommerce'),                'type' => 'number',                'desc_tip' => 'true',                'value' => $value            )        );        echo '</div>';    }    function woocommerce_product_custom_fields_save($post_id)    {        // Custom Product Text Field        if (array_key_exists('priority', $_POST)) {            update_post_meta($post_id, 'priority', intval($_POST['priority']));        }    }    function cw_add_postmeta_ordering_args( $args_sort_cw ) {      $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :            apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );      switch( $cw_orderby_value ) {           case 'priority':                $args_sort_cw['orderby'] = 'meta_value';                $args_sort_cw['order'] = 'desc';                $args_sort_cw['meta_key'] = 'priority';                break;      }      return $args_sort_cw;    }    add_filter( 'woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args' );    function cw_add_new_postmeta_orderby( $sortby ) {       $sortby['priority'] = __( 'Recommended', 'woocommerce' );       return $sortby;    }    add_filter( 'woocommerce_default_catalog_orderby_options', 'cw_add_new_postmeta_orderby' );    add_filter( 'woocommerce_catalog_orderby', 'cw_add_new_postmeta_orderby' );调整了该woocommerce_product_custom_fields_save()功能array_key_exists(),现在工作正常。
打开App,查看更多内容
随时随地看视频慕课网APP