从woocommerce 3的一个孩子那里获得分组的产品链接 [英] Get the grouped product link from one of it's childs in woocommerce 3

查看:86
本文介绍了从woocommerce 3的一个孩子那里获得分组的产品链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce 2.3中,单个产品的post_parent是分组产品的一部分.因此可以通过以下方式链接它们:

With woocommerce 2.3 there was post_parent for single products what was the part of grouped product. So it was possible to link them by:

function parent_permalink_button() {
   global $post; 
   if( $post->post_parent != 0 ){ 
       $permalink = get_permalink($post->post_parent);
       echo '<a class="button" href="'.$permalink.'">Link to Parent</a>';
   }
}

使用woocommerce 3.0.0更新情况已更改.实际上,现在正好相反.分组产品具有_children.

with woocommerce 3.0.0 update situation changed. Actually it is opposite now. Grouped product has its _children.

如何创建从单个产品到其分组的链接?它可以是更多分组产品的一部分,因此可以有多个链接(但我的店不是这种情况)

How can I create the link from single product to its grouped? It can be a part of more grouped product so there can be multiple links (but it is not the case of my shop)

感谢Michal

推荐答案

可以通过以下方式为WooCommerce 3+构建该功能:
(带有可选的$post_id参数)

It's possible to build that function for WooCommerce 3+ this way:
(with an optional $post_id argument)

/**
 * Get a button linked to the parent grouped product.
 *
 * @param string (optional): The children product ID (of a grouped product)
 * @output button html
 */
function parent_permalink_button( $post_id = 0 ){
    global $post, $wpdb;

    if( $post_id == 0 )
        $post_id = $post->ID;

    $parent_grouped_id = 0;

    // The SQL query
    $results = $wpdb->get_results( "
        SELECT pm.meta_value as child_ids, pm.post_id
        FROM {$wpdb->prefix}postmeta as pm
        INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
        INNER JOIN {$wpdb->prefix}term_relationships as tr ON pm.post_id = tr.object_id
        INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
        WHERE p.post_type LIKE 'product'
        AND p.post_status LIKE 'publish'
        AND t.slug LIKE 'grouped'
        AND pm.meta_key LIKE '_children'
        ORDER BY p.ID
    " );

    // Retreiving the parent grouped product ID
    foreach( $results as $result ){
        foreach( maybe_unserialize( $result->child_ids ) as $child_id )
            if( $child_id == $post_id ){
                $parent_grouped_id = $result->post_id;
                break;
            }
        if( $parent_grouped_id != 0 ) break;
    }
    if( $parent_grouped_id != 0 ){
        echo '<a class="button" href="'.get_permalink( $parent_grouped_id ).'">Link to Parent</a>';
    } 
    // Optional empty button link when no grouped parent is found
    else {
        echo '<a class="button" style="color:grey">No Parent found</a>';
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

在WooCommerce 3+中经过测试并可以工作

Tested and works in WooCommerce 3+

用法:(2例)

1)例如,不直接在产品模板中使用可选参数$post_id:

1) Without using the optional argument $post_id for example directly in the product templates:

parent_permalink_button();

2)随处使用该函数,定义其参数$post_id:

2) Using the function everywhere, defining the it's argument $post_id:

$product_id = 37; // the product ID is defined here or dynamically…
parent_permalink_button( $product_id );

这篇关于从woocommerce 3的一个孩子那里获得分组的产品链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆