Home > Mobile >  Woocommerce hook to shortcode
Woocommerce hook to shortcode

Time:10-30

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 solviing 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.

function property_overview_field_shortcode( ) {
  return <<<PROPERTYINFO
         
<div >
<div >
    <h2>Property Overview</h2>             
    <div >
        <ul>

<?php $price = get_field_object('guide_price'); ?>
<?php
    if( ! empty( $price ) ) { ?>
        <li>
            <?php echo $price['label']; ?>:
            <strong><?php echo $price['value']; ?></strong>
        </li>
<?php } ?>

<?php $list = get_field_object('listing_status'); ?>
<?php
    if( ! empty( $list ) ) { ?>
        <li>
            <?php echo $list['label']; ?>:
            <strong><?php echo $list['value']; ?></strong>
        </li>
<?php } ?>

<?php $property_cat = get_field_object('property_category'); ?>
<?php
    if( ! empty( $property_cat ) ) { ?>
        <li>
            <?php echo $property_cat['label']; ?>:
            <strong><?php echo $property_cat['value']; ?></strong>
        </li>
<?php } ?>

<?php $location = get_field_object('location'); ?>
<?php
    if( ! empty( $location ) ) { ?>
        <li>
            <?php echo $location['label']; ?>:
            <strong><?php echo $location['value']; ?></strong>
        </li>
<?php } ?>

<?php $property_type = get_field_object('property_type'); ?>
<?php
    if( ! empty( $property_type ) ) { ?>
        <li>
            <?php echo $property_type['label']; ?>:
            <strong><?php echo $property_type['value']; ?></strong>
        </li>
<?php } ?>

<?php $city = get_field_object('city'); ?>
<?php
    if( ! empty( $city ) ) { ?>
        <li>
            <?php echo $city['label']; ?>:
            <strong><?php echo $city['value']; ?></strong>
        </li>
<?php } ?>

<?php $bedroom = get_field_object('bedroom'); ?>
<?php
    if( ! empty( $bedroom ) ) { ?>
        <li>
            <?php echo $bedroom['label']; ?>:
            <strong><?php echo $bedroom['value']; ?></strong>
        </li>
<?php } ?>
        </ul>
    </div>
</div>
</div>
    <?php 
} 

PROPERTYINFO;
}
add_shortcode( 'PROPERTYINFO', 'property_overview_field_shortcode' );

CodePudding user response:

As explained in the WordPress Shortcode API in the codex: https://codex.wordpress.org/Shortcode_API

If the shortcode produces a lot of HTML then ob_start can be used to capture output and convert it to a string as follows:-

function my_shortcode() {
    ob_start();
    // Here you can put your additional HTML or PHP code.
    ?> <HTML> <here> ... <?php
    return ob_get_clean();
}
add_shortcode('my-shortcode', 'my_shortcode');

If you don't use output buffering, then the code will typically show up before the content on the page.

CodePudding user response:

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' );
  • Related