Woocommerce - 编写短代码以显示按产品属性产品类别页面过滤的链接,并在产品类别页面上显示短代码

时间:2017-11-06 10:55:46

标签: php wordpress woocommerce

我正在编写一些函数来显示自定义字段,该字段将显示在产品父类别页面上的子类别标题之后(例如http:website.com/catalog/parent-category)。

此自定义字段所需的内容是html和短代码。所以我创建的另一个函数是短代码,如下所示:

    [product_attr slug="Straight Drill" label="Straight Drill"]

我需要显示指向按产品属性过滤的产品页面的链接。我希望能够在短代码属性中编辑标签,但是slug是从产品属性的slugs中生成的。

这就是我在下面使用的工具,我无法使短代码工作,并在子product_cat标题下显示内容。任何指出错误的帮助都将受到赞赏!!

// Display product attributes from a product category

global $product;
    $current_category = $wp_query->get_queried_object();
    $category_id = $current_category->term_id;
    $category_name = $current_category->name;
    $products = get_posts(array('product_cat' => $category_id));
    $term_link = get_term_link( $current_category );

    // Extended subscription function with subscription type variable
function create_attribute_shortcode( $atts ) {
    // Get product attributes
foreach ( $products as $product ) {

$attributes = $product->get_attributes();

if ( ! $attributes ) {
    return;
}
foreach ( $attributes as $attribute ) {
        $attribute_name = $attribute['name'];
        $attribute_slug = $attribute['slug'];

    extract( shortcode_atts( array(
        'slug'  => $attribute_slug,
        'label' => ''), $atts));

    $link= '<a href="'. esc_url( $term_link ). '/'. $slug .'">'. $label .'</a>';
    return $link;
}
}
add_shortcode( 'product-attr', 'create_attribute_shortcode' );

}
add_action('product_cat_add_form_fields', 'tft_taxonomy_add_new_meta_field', 10, 2);
add_action('product_cat_edit_form_fields', 'tft_taxonomy_edit_meta_field', 10, 2);

//Product Cat Create page
function tft_taxonomy_add_new_meta_field() {
    ?>
    <div class="form-field">
        <label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label>
        <textarea name="tft_meta_desc" id="tft_meta_desc"></textarea>
        <p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
    </div>
    <?php
}

//Product Cat Edit page
function tft_taxonomy_edit_meta_field($term) {
    //getting term ID
    $term_id = $term->term_id;
    // retrieve the existing value(s) for this meta field.
    $tft_meta_desc = get_term_meta($term_id, 'tft_meta_desc', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label></th>
        <td>
            <textarea name="tft_meta_desc" id="tft_meta_desc"><?php echo esc_attr($tft_meta_desc) ? esc_attr($tft_meta_desc) : ''; ?></textarea>
            <p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
        </td>
    </tr>
    <?php
}

add_action('edited_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
// Save extra taxonomy fields callback function.
function tft_save_taxonomy_custom_meta($term_id) {
    $tft_meta_desc = filter_input(INPUT_POST, 'tft_meta_desc');
    update_term_meta($term_id, 'tft_meta_desc', $tft_meta_desc);
}

// check if page is parent product cat
function has_term_have_children( $term_id = '', $taxonomy = 'product_cat' ) {
    // Check if we have a term value, if not, return false
    if ( !$term_id ) 
        return false;

    // Get term children
    $term_children = get_term_children( filter_var( $term_id, FILTER_VALIDATE_INT ), filter_var( $taxonomy, FILTER_SANITIZE_STRING ) );

    // Return false if we have an empty array or WP_Error object
    if ( empty( $term_children ) || is_wp_error( $term_children ) )
    return true;
    return false;
}
// Add product attribute lists to underneath product cats

    if( is_tax( 'product_cat' ) && has_term_have_children(get_queried_object_id()) ) {
    $args = array('child_of' => $termID, 'parent' => $termID);
    $termchildren = get_terms( 'product_cat', $args);
    add_action( 'woocommerce_after_shop_loop_item_title', 'tft_do_prod_attr_list', 1 );
function tft_do_prod_attr_list() {
    $tft_do_meta_desc = get_term_meta($termchildren->term_id, 'tft_meta_desc', true);
    $tft_display_meta_desc = do_shortcode($tft_do_meta_desc);
    echo '<div class="tft-meta-desc">';
    echo $tft_display_meta_desc;
    echo '</div>';
}
}

0 个答案:

没有答案