0

I found and customized function to make a custom order of products on FE by using custom meta. We have one problem. We can see default data in BE of every product but it becomes visible on FE only after manually updating the product. We have thousands of products so it is not possible to reupdate every single product manually. Problem is that function saves data to DB only after manual update of products. Does anybody has an idea of how to customize the function to save default data to DB by "default"? You can find our function below.

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_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( '_vaha_produktu', true );
        if ( ! $value ) {
            $value = 0;
        }
        
        echo '<div class="product_custom_field">';
        woocommerce_wp_text_input(
            array(
                'id' => '_vaha_produktu',
                'placeholder' => 'Váha produktu',
                'label' => __('Váha produktu', 'woocommerce'),
                'desc_tip' => 'true',
                'value' => $value
            )
        );
        echo '</div>';
    }

    function woocommerce_product_custom_fields_save($post_id)
    {
        // Custom Product Text Field
        $woocommerce_custom_product_text_field = $_POST['_vaha_produktu'];
        if (!empty($woocommerce_custom_product_text_field)) {
            update_post_meta($post_id, '_vaha_produktu', esc_attr($woocommerce_custom_product_text_field));
        }
        else {
            $woocommerce_custom_product_text_field = '0';
            update_post_meta($post_id, '_vaha_produktu', 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 '_vaha_produktu':
                $args_sort_cw['orderby'] = 'meta_value';
                $args_sort_cw['order'] = 'desc';
                $args_sort_cw['meta_key'] = '_vaha_produktu';
                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['_vaha_produktu'] = __( 'Doporučené řazení', '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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.