dongsong4418 2017-11-06 10:55
浏览 87

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

I am writing some functions to display a custom field which value will show up after the subcategory titles on the product parent category page (e.g. http:website.com/catalog/parent-category).

The content I need for this custom field is html and shortcodes. So another function I am creating is the shortcode, which looks like this:

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

where I will need to show the link to a filtered-by-product-attributes product page. I want to be able to edit the label in shortcode attributes, but the slug is generated from product attributes' slugs.

This is what I am working with below, I haven't been able to make the shortcode works, and displaying the content below the sub product_cat titles. Any help pointing out the errors will be appreciated!!

// 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我的数据无法存进链表里
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端