【问题标题】:Setting a default value for a Woocommerce product field to all products为所有产品设置 Woocommerce 产品字段的默认值
【发布时间】:2020-08-28 18:50:40
【问题描述】:

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

这是整个代码:

// 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;
      }
      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' );

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

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

【问题讨论】:

  • 我回答了,但很好奇为什么需要自定义重量字段?这已经是 Woo 提供的一个领域了?
  • 我的意思不是说它更像是体重优先级。变量命名是我的错。

标签: php wordpress woocommerce


【解决方案1】:

我要做的是在woocommerce_wp_text_input 参数中使用value 参数。然后,您可以将当前值或默认值传递给它(如果尚不存在)。

还通过woocommerce_admin_process_product_object添加了新的保存程序

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'so_63588126_product_custom_fields');
// Save Fields
add_action('woocommerce_admin_process_product_object', 'so_63588126_product_custom_fields_save');


function so_63588126_product_custom_fields()
{
    global $product_object;

    $value = $product_object->get_meta( 'weight', true );

    if ( ! $value ) {
        $value = 0;
    }

    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(
        array(
            'id' => 'weight',
            'placeholder' => 'Weight of the product - values a>b',
            'label' => __('Weight of the product', 'your-textdomain'),
            'desc_tip' => 'true',
            'value' => $value 
        )
    );
    echo '</div>';
}

function so_63588126_product_custom_fields_save( $product )
{
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['weight'];
    if ( ! empty( $_POST['weight'] ) ) {
        $product->update_meta_data( 'weight', sanitize_text_field( wp_unslash( $_POST['weight'] ) ) );
    } else {
        $product->update_meta_data( 'weight', 0 );
    }

}

补充说明:

我使用$product_object-&gt;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() 对数字更好......通过在 woocommerce_wp_text_input() 参数中将 type 参数设置为 'number' 将输入设置为数字输入类型也是如此。

【讨论】:

  • 非常感谢您的回答,但您能帮我解决发生的另一个问题吗?
  • 关于不重置回 0,我猜 0 值没有通过 ! empty(),所以我对代码进行了调整以将“权重”保存为 0这个案例。
【解决方案2】:

这是对我有用的解决方案。

// 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, $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' );

我用array_key_exists() 调整了woocommerce_product_custom_fields_save() 函数,现在它工作正常。 感谢 helgatheviking 的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多