如何为WooCommerce插件创建“单一”类别小部件

时间:2013-01-24 作者:user26485

Woocommerce开箱即用提供了许多小部件,但是用于提供产品搜索的类别小部件将所有类别分组到一个小部件中。

我想要一个小部件用于一个类别,即“按意义购物”和另一个小部件“按字母购物”等。

我抓取了widget-product\\u类别的widget代码。来自woocommerce插件的php。。。我想我可以简单地修改我在woocommerce插件中创建的“大小”类别的代码。

然而,我很快发现这超出了我的范围。

由于我需要使用“shop by xxx”搜索创建4个不同的小部件,如果有任何帮助,我将不胜感激。下面是woo小部件的代码,它在一个小部件中提供了所有类别。

有没有办法为“尺寸”的单个类别退货重新编码?

var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;
var $cat_ancestors;
var $current_cat;

/**
 * constructor
 *
 * @access public
 * @return void
 */
function WooCommerce_Widget_Product_Categories() {

    /* Widget variable settings. */
    $this->woo_widget_cssclass = \'widget_product_categories\';
    $this->woo_widget_description = __( \'A list or dropdown of product categories.\', \'woocommerce\' );
    $this->woo_widget_idbase = \'woocommerce_product_categories\';
    $this->woo_widget_name = __(\'WooCommerce Product Categories\', \'woocommerce\' );

    /* Widget settings. */
    $widget_ops = array( \'classname\' => $this->woo_widget_cssclass, \'description\' => $this->woo_widget_description );

    /* Create the widget. */
    $this->WP_Widget(\'product_categories\', $this->woo_widget_name, $widget_ops);
}


/**
 * widget function.
 *
 * @see WP_Widget
 * @access public
 * @param array $args
 * @param array $instance
 * @return void
 */
function widget( $args, $instance ) {
    extract( $args );

    $title = apply_filters(\'widget_title\', empty( $instance[\'title\'] ) ? __( \'Product Categories\', \'woocommerce\' ) : $instance[\'title\'], $instance, $this->id_base);
    $c = $instance[\'count\'] ? \'1\' : \'0\';
    $h = $instance[\'hierarchical\'] ? true : false;
    $s = (isset($instance[\'show_children_only\']) && $instance[\'show_children_only\']) ? \'1\' : \'0\';
    $d = $instance[\'dropdown\'] ? \'1\' : \'0\';
    $o = isset($instance[\'orderby\']) ? $instance[\'orderby\'] : \'order\';

    echo $before_widget;
    if ( $title ) echo $before_title . $title . $after_title;

    $cat_args = array(\'show_count\' => $c, \'hierarchical\' => $h, \'taxonomy\' => \'product_cat\');

    if ( $o == \'order\' ) {

        $cat_args[\'menu_order\'] = \'asc\';

    } else {

        $cat_args[\'orderby\'] = \'title\';

    }

    if ( $d ) {

        // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
        woocommerce_product_dropdown_categories( $c, $h, 0 );

        ?>
        <script type=\'text/javascript\'>
        /* <![CDATA[ */
            var dropdown = document.getElementById("dropdown_product_cat");
            function onCatChange() {
                if ( dropdown.options[dropdown.selectedIndex].value !==\'\' ) {
                    location.href = "<?php echo home_url(); ?>/?product_cat="+dropdown.options[dropdown.selectedIndex].value;
                }
            }
            dropdown.onchange = onCatChange;
        /* ]]> */
        </script>
        <?php

    } else {

        global $wp_query, $post, $woocommerce;

        $this->current_cat = false;
        $this->cat_ancestors = array();

        if ( is_tax(\'product_cat\') ) :

            $this->current_cat = $wp_query->queried_object;
            $this->cat_ancestors = get_ancestors( $this->current_cat->term_id, \'product_cat\' );

        elseif ( is_singular(\'product\') ) :

            $product_category = wp_get_post_terms( $post->ID, \'product_cat\' );

            if ($product_category) :
                $this->current_cat = end($product_category);
                $this->cat_ancestors = get_ancestors( $this->current_cat->term_id, \'product_cat\' );
            endif;

        endif;

        include_once( $woocommerce->plugin_path() . \'/classes/walkers/class-product-cat-list-walker.php\' );

        $cat_args[\'walker\']             = new WC_Product_Cat_List_Walker;
        $cat_args[\'title_li\']           = \'\';
        $cat_args[\'show_children_only\'] = ( isset( $instance[\'show_children_only\'] ) && $instance[\'show_children_only\'] ) ? 1 : 0;
        $cat_args[\'pad_counts\']         = 1;
        $cat_args[\'show_option_none\']   = __(\'No product categories exist.\', \'woocommerce\');
        $cat_args[\'current_category\']   = ( $this->current_cat ) ? $this->current_cat->term_id : \'\';
        $cat_args[\'current_category_ancestors\'] = $this->cat_ancestors;

        echo \'<ul class="product-categories">\';

        wp_list_categories( apply_filters( \'woocommerce_product_categories_widget_args\', $cat_args ) );

        echo \'</ul>\';

    }

    echo $after_widget;
}


/**
 * update function.
 *
 * @see WP_Widget->update
 * @access public
 * @param array $new_instance
 * @param array $old_instance
 * @return array
 */
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[\'title\'] = strip_tags($new_instance[\'title\']);
    $instance[\'orderby\'] = strip_tags($new_instance[\'orderby\']);
    $instance[\'count\'] = !empty($new_instance[\'count\']) ? 1 : 0;
    $instance[\'hierarchical\'] = !empty($new_instance[\'hierarchical\']) ? true : false;
    $instance[\'dropdown\'] = !empty($new_instance[\'dropdown\']) ? 1 : 0;
    $instance[\'show_children_only\'] = !empty($new_instance[\'show_children_only\']) ? 1 : 0;

    return $instance;
}


/**
 * form function.
 *
 * @see WP_Widget->form
 * @access public
 * @param array $instance
 * @return void
 */
function form( $instance ) {
    //Defaults
    $instance = wp_parse_args( (array) $instance, array( \'title\' => \'\') );
    $title = esc_attr( $instance[\'title\'] );
    $orderby = isset( $instance[\'orderby\'] ) ? $instance[\'orderby\'] : \'order\';
    $count = isset($instance[\'count\']) ? (bool) $instance[\'count\'] :false;
    $hierarchical = isset( $instance[\'hierarchical\'] ) ? (bool) $instance[\'hierarchical\'] : false;
    $dropdown = isset( $instance[\'dropdown\'] ) ? (bool) $instance[\'dropdown\'] : false;
    $show_children_only = isset( $instance[\'show_children_only\'] ) ? (bool) $instance[\'show_children_only\'] : false;
?>get\\u field\\u id(\'title\');?>“>获取字段id(\'title\');?>”name=“获取字段名称(\'title”);?>”type=“text”value=”/>

    <p><label for="<?php echo $this->get_field_id(\'orderby\'); ?>"><?php _e(\'Order by:\', \'woocommerce\') ?></label>
    <select id="<?php echo esc_attr( $this->get_field_id(\'orderby\') ); ?>" name="<?php echo esc_attr( $this->get_field_name(\'orderby\') ); ?>">
        <option value="order" <?php selected($orderby, \'order\'); ?>><?php _e(\'Category Order\', \'woocommerce\'); ?></option>
        <option value="name" <?php selected($orderby, \'name\'); ?>><?php _e(\'Name\', \'woocommerce\'); ?></option>
    </select></p>

    <p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id(\'dropdown\') ); ?>" name="<?php echo esc_attr( $this->get_field_name(\'dropdown\') ); ?>"<?php checked( $dropdown ); ?> />
    <label for="<?php echo $this->get_field_id(\'dropdown\'); ?>"><?php _e( \'Show as dropdown\', \'woocommerce\' ); ?></label><br />

    <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id(\'count\') ); ?>" name="<?php echo esc_attr( $this->get_field_name(\'count\') ); ?>"<?php checked( $count ); ?> />
    <label for="<?php echo $this->get_field_id(\'count\'); ?>"><?php _e( \'Show post counts\', \'woocommerce\' ); ?></label><br />

    <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id(\'hierarchical\') ); ?>" name="<?php echo esc_attr( $this->get_field_name(\'hierarchical\') ); ?>"<?php checked( $hierarchical ); ?> />
    <label for="<?php echo $this->get_field_id(\'hierarchical\'); ?>"><?php _e( \'Show hierarchy\', \'woocommerce\' ); ?></label><br/>

    <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id(\'show_children_only\') ); ?>" name="<?php echo esc_attr( $this->get_field_name(\'show_children_only\') ); ?>"<?php checked( $show_children_only ); ?> />
    <label for="<?php echo $this->get_field_id(\'show_children_only\'); ?>"><?php _e( \'Only show children for the current category\', \'woocommerce\' ); ?></label></p>
}

1 个回复
SO网友:Caroline Elisa

虽然有点晚了,但也许这对某人还是有帮助的。。。

如果您不介意硬编码父类别名称、slug和ID(例如“All”、“designer”、“38”),则可以创建下拉列表以跳转到指定ID的子类别:

<?php   
$terms = get_terms( \'product_cat\', \'child_of=38\' );
if ( $terms ) {
    print( \'<select name="product_cat" id="dropdown_product_cat">\' );
    print( \'<option value="designer" selected="selected">All</option>\' );
    foreach ( $terms as $term ) {
        printf( \'<option value="%s">%s</option>\', esc_attr( $term->slug ), esc_html( $term->name ) );
    }
    print( \'</select>\' );
} ?>
<script type="text/javascript">
/* <![CDATA[ */
        var product_cat_dropdown = document.getElementById("dropdown_product_cat");
        function onProductCatChange() {
                if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !==\'\' ) {
                        location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
                }
        }
        product_cat_dropdown.onchange = onProductCatChange;
/* ]]> */
</script>

结束

相关推荐

Google Map在Tab Plugins的第二个选项卡上无法使用

我正在使用postTabs plugin 和Comprehensive Google Map Plugin, 我的问题是,当我在第二个选项卡上有我的谷歌地图时,地图没有按预期加载。但是如果我把它移到第一个标签上,效果会非常好。。有没有办法让地图在第二个选项卡上工作?实际上,无论我使用哪个选项卡插件,地图都不会正确加载到第二个选项卡上。。欢迎提出任何建议。谢谢:)