Search code examples
phpwordpresswoocommerceadvanced-custom-fieldsshortcode

Woocommerce how to convert a function fires in a hook to a shortcode


I have custom fields added to WooCommerce single product page using functions.php:

add_action( 'woocommerce_after_single_product_summary', 'auction_information_field', 4 );

However, I am having problems with the position placement of the block when using Divi Builder.

Because when the divi builder is activated, it's placed outside before Divi builder area. Yet works fine when I use the default standard editor.

So I'm interested in solving this problem by turning the add_action function for the fields into a shortcode instead. So the short code can stay in functions.php, and I can place the shortcode in the divi builder module to have it in the correct location.

Although I'm unsure how to covert into a shortcode.

Any advice would be much appreciated.

            <div class="et_pb_row property_page_row">
            <div class="property-content">
                <h2>Auction Details</h2>
                <div class="property-overview">
                    <ul>
                        <li>
                            Auction Status
                            <strong><?php
                                $type = $product->get_auction_status();
                                switch ( $type ) {
                                    case 'non-started':
                                        echo esc_attr__( 'Not Started', 'yith-auctions-for-woocommerce' );
                                        break;
                                    case 'started':
                                        echo esc_attr__( 'Started', 'yith-auctions-for-woocommerce' );
                                        break;
                                    case 'finished':
                                        echo esc_attr__( 'Finished', 'yith-auctions-for-woocommerce' ) ;
                                        break;
                                }
                                ?>
                            </strong>
                        </li>
                        <li>
                            Auction Type <strong><?php echo $product->get_auction_type(); ?></strong>
                        </li>
                        <li>
                            Auction Start Date
                            <strong><?php
                                $dateinic = $product->get_start_date();
                                if ( $dateinic ) {
                                
                                    $format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
                                    $format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
                                
                                    $format = $format_date . ' ' . $format_time;
                                
                                    $date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateinic ), $format );
                                    echo $date;
                                }
                                ?>
                            </strong>
                        </li>
                        <li>
                            Auction End Date
                            <strong><?php
                                $dateclose = $product->get_end_date();
                                
                                if ( $dateclose ) {
                                
                                    $format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
                                    $format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
                                    
                                    $format = $format_date . ' ' . $format_time;
                                    
                                    $date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateclose ), $format );
                                    echo $date;
                                }
                                
                                ?>
                            </strong>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    <?php
    }

Solution

  • You can use add_shortcode. and you can use like [auction_information_field]. Try the below code.

    function auction_information_field_callback() {
    
        if( is_singular( 'product' ) ){
    
            global $product;
    
            ob_start();
    
            if ( 'auction' === $product->get_type() ) { ?>
                <div class="et_pb_row property_page_row">
                    <div class="property-content">
                        <h2>Auction Details</h2>
                        <div class="property-overview">
                            <ul>
                                <li>
                                    Auction Status
                                    <strong><?php
                                        $type = $product->get_auction_status();
                                        switch ( $type ) {
                                            case 'non-started':
                                                echo esc_attr__( 'Not Started', 'yith-auctions-for-woocommerce' );
                                                break;
                                            case 'started':
                                                echo esc_attr__( 'Started', 'yith-auctions-for-woocommerce' );
                                                break;
                                            case 'finished':
                                                echo esc_attr__( 'Finished', 'yith-auctions-for-woocommerce' ) ;
                                                break;
                                        }
                                        ?>
                                    </strong>
                                </li>
                                <li>
                                    Auction Type <strong><?php echo $product->get_auction_type(); ?></strong>
                                </li>
                                <li>
                                    Auction Start Date
                                    <strong><?php
                                        $dateinic = $product->get_start_date();
                                        if ( $dateinic ) {
                                        
                                            $format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
                                            $format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
                                        
                                            $format = $format_date . ' ' . $format_time;
                                        
                                            $date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateinic ), $format );
                                            echo $date;
                                        }
                                        ?>
                                    </strong>
                                </li>
                                <li>
                                    Auction End Date
                                    <strong><?php
                                        $dateclose = $product->get_end_date();
                                        
                                        if ( $dateclose ) {
                                        
                                            $format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
                                            $format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
                                            
                                            $format = $format_date . ' ' . $format_time;
                                            
                                            $date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateclose ), $format );
                                            echo $date;
                                        }
                                        
                                        ?>
                                    </strong>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            <?php
            }
    
            $html = ob_get_clean();
            return $html;
    
        }   
            
    }
    
    add_shortcode( 'auction_information_field', 'auction_information_field_callback' );