Trouble getting file url of file attachment in cloneable group

Support MB Group Trouble getting file url of file attachment in cloneable groupResolved

Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #18828
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please take a look at my screen record.

    The fields do not have any value but after clicking Publish, the table still shows as well as the button download above. Remove attribute state or set to false then refresh and update the product again to see the result.

    #18841
    sgtsciencesgtscience
    Participant

    That did it! was not updating the products as you mentioned. Thank you very much for your help!

    Steve

    #19232
    sgtsciencesgtscience
    Participant

    Hi There I have another follow up to this.

    For the special colors fields I made I had to make an option for dual color using a conditional selection box like this:

    add_filter( 'rwmb_meta_boxes', 'prefix_register_special_colours' );
    function prefix_register_special_colours( $meta_boxes ) {
        $meta_boxes[] = array(
            'id' => 'Special_Colours',
            'title' => esc_html__( 'Special Order Colours' ),
            'post_types' => array( 'product' ),
            'context' => 'advanced',
            'priority' => 'default',
            'autosave' => 'true',
            'fields' => array(
                array(
                    'id'   => 'has_special_colours',
                    'name' => 'Special Order Colours',
                    'desc'  => 'Select if this product has special order colours',
                    'type' => 'checkbox',
                    'std'  => 0,
                ),
                array(
                    'id' => 'special_colours',
                    'type' => 'group',
                    'group_title' => 'Colour {#}',
                    'collapsible' => true,
                    'save_state' => false,
                    'clone'  => true,
                    'sort_clone' => true,
                    'hidden' => array( 'has_special_colours', '!=', '1' ),
                    'fields' => array(
                        array(
                            'id'   => 'colour_name',
                            'type' => 'text',
                            'name' => 'Colour Name',
                        ),
                        array(
                            'id' => 'special_colour',
                            'type' => 'color',
                            'name' => esc_html__( 'Special Colour' ),
                            'desc' => esc_html__( 'Select a colour to appear on the product page as a special order colour' ),
                        ),
                        array(
                            'id'    => 'is_dual',
                            'name'  => 'Is Dual Colour?',
                            'desc'  => 'Pick if dual colour',
                            'type'  => 'select',
                            'options' => array(
                                'No'         => 'No',
                                'Yes'        => 'Yes',
                            ),
                        ),
                        
                        array(
                            'id' => 'special_colour2',
                            'type' => 'color',
                            'hidden' => array( 'is_dual', '!=', 'Yes' ),
                            'name' => esc_html__( 'Special Colour2' ),
                            'desc' => esc_html__( 'Select a colour to appear on the product page as a special order dual colour' ),
                        ),
                    ),
                ),
            ),
        );
        return $meta_boxes;
    }

    and to display it I am using this:

    add_action( 'woocommerce_before_add_to_cart_button', 'product_special_colours', 5 );
    function product_special_colours() {
        $has_special_colours = rwmb_meta( 'has_special_colours' );
        $special_colours = rwmb_meta( 'special_colours' );
        if ( ! empty( $special_colours ) ) {
            echo '<table class="variations" cellspacing="0">
                <tbody>
                    <tr>
                        <td class="label">
                            <label for="so_color">Special Order Colours</label>
                        </td>
                        <td class="value woo-variation-items-wrapper">
                            <ul class="variable-items-wrapper color-variable-wrapper">';
                                foreach ( $special_colours as $special_colour ) {
                                    $is_dual = isset ( $special_colour['is_dual'] ) ? $special_colour['is_dual'] : '';
                                    if ($is_dual != "Yes") {
                                        $colour_name = isset( $special_colour['colour_name'] ) ? $special_colour['colour_name'] : '';
                                        if ( isset( $special_colour['special_colour'] ) ) {
                                            $colour = isset( $special_colour['special_colour'] ) ? $special_colour['special_colour'] : '';
                                            echo '<li class="special_colour" style="background-color: ' . $colour . ' ">
                                                <span class="tooltip">' . $colour_name . '</span>
                                            </li>';
                                        }
                                    }
                                    else {
                                        $colour_name = isset( $special_colour['colour_name'] ) ? $special_colour['colour_name'] : '';
                                        if ( isset( $special_colour['special_colour'] ) ) {
                                            $colour = isset( $special_colour['special_colour'] ) ? $special_colour['special_colour'] : '';
                                            $colour2 = isset( $special_colour['special_colour2'] ) ? $special_colour['special_colour2'] : '';
                                            echo '<li class="special_colour" style="background: linear-gradient(-45deg, ' . $colour . ' 0%, ' . $colour . ' 50%, ' . $colour2 . ' 50%, ' . $colour2 . ' 100%);">
                                                <span class="tooltip">' . $colour_name . '</span>
                                            </li>';
                                        }
                                    }
                                }
                            echo '</ul>
                        </td>
                        <td>
                            <p>To order one of our special special order colours please <a href="/contact/">contact us</a>. <br> Please note that shipping of special orders can take up to 12 weeks.</p>
                        </td>
                    </tr>
                </tbody>
            </table>';
    
        } 
    }

    When I use the selection to choose whether the color is dual color or not it works perfect, the issue is when I delete information from all the fields if I want to display no special colors on the frontend, when the product is saved the selection field defualts to the first option "no" so the stuff displays on the frontend since it can't pass if ( ! empty( $special_colours ) ) {

    You can see I setup a conditional checkbox on whether or not to show the fields, and when I run if ( ! empty( $has_special_colours ) ) { it works. I'm thinking that I can also just use a checkbox instead of a select field for "is_dual" selector since I think an empty checkbox will register as empty, but there may be a case when I want to use a select box with multiple selections. I'm just wondering what is the best solution here, since I feel that cleaning out all the fields and only displaying when there is stuff in the fields is the cleanest option.

    This also leads me to ask, if I were to remove all the code that registers the fields, what happens to the data? What happens to the data when I change the id name of a field? It is still around under the old id? How to I clean out the data if I want to remove fields or change and id? If there is any documentation on this that would be great,

    Thanks again for all your help,

    Steve

    #19238
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you change the field ID, after saving the new value, WordPress generates the new meta key that corresponds with the field ID and saves it in the new row(s) in the database. The old ID (meta key) still there and called Orphan metadata, you can follow these guides to know how to delete it and clean up the database
    https://www.fixrunner.com/how-to-easily-clean-up-your-wordpress-database/
    https://www.wpbeginner.com/plugins/how-to-clean-up-your-wordpress-database-for-improved-performance/

    #19258
    sgtsciencesgtscience
    Participant

    ok thanks for the information, do you have a suggestion on the best way to not show any of the color stuff when there is no information? In the fields? I there a way to set a select field as empty?

    Steve

    #19265
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The select field must have not-empty options to choose from. In this case, I think you should use the field checkbox, just on/off (true/false).

    #19283
    sgtsciencesgtscience
    Participant

    Ok thank you very much for your advice!

    Steve

Viewing 7 posts - 16 through 22 (of 22 total)
  • You must be logged in to reply to this topic.