Change Shortcode wrapper in Woocommerce

南笙酒味 提交于 2019-11-29 08:14:37

You can create your own shortcode, just a clone of the default one, but with that change, so paste this in your functions.php:

function custom_recent_products_FX($atts) {
    global $woocommerce_loop, $woocommerce;

    extract(shortcode_atts(array(
        'per_page'  => '12',
        'columns'   => '4',
        'orderby' => 'date',
        'order' => 'desc'
    ), $atts));

    $meta_query = $woocommerce->query->get_meta_query();

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => $per_page,
        'orderby' => $orderby,
        'order' => $order,
        'meta_query' => $meta_query
    );

    ob_start();

    $products = new WP_Query( $args );

    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php woocommerce_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    <?php endif;

    wp_reset_postdata();

    return '<div class="MY_CUSTOM_CLASS">' . ob_get_clean() . '</div>';
 }
 add_shortcode('custom_recent_products','custom_recent_products_FX');

Notice at the end of that function the "MY_CUSTOM_CLASS", change that for your needs.

This will create a new shortcode, almost same than the "recent_products" one but with that only change.

So to output this, just use on template:

 echo do_shortcode('[custom_recent_products per_page="3"]');

Or in your posts:

 [custom_recent_products per_page="3"]

I don´t know if this is the best approach, but for what i can see, the class "woocommerce" is returned directly on the recent_products shortcode function as html, so i can´t imagine how to filter or hook this by anotherway.

Hope that helps and sorry my bad english :)

you can use following trick to over right wrapper function :
Here i have created a shortcode for checkout and shortcode and change wrapper
shortcode will be [overright_checkout_of_shortcode] 
function shortcode_handler($atts) {
    $classget=new WC_Shortcodes();
    $wrapper = array(
            'class'  => '',
            'before' => '<div id="accordion" class="panel-group faq_group checkout-group" role="tablist" aria-multiselectable="true" >',
            'after'  => '</div>'
        );
  return $classget->shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ,$wrapper);
 }
add_shortcode('overright_checkout_of_shortcode','shortcode_handler');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!