【问题标题】:woocommerce product variation title in table表格中的 woocommerce 产品变体标题
【发布时间】:2016-12-22 00:49:49
【问题描述】:

我想在单个产品页面中列出我的产品变体及其尺寸,尺寸部分一切正常,但我无法显示变体名称。这是我的代码:

<?php 

    $variations = $product->get_available_variations();
    foreach ( $variations as $variable_array ){
    $variation = new WC_Product( $variable_array['variation_id'] );?>
    <tr>                                

        <td>
            <b>here I need the variation title</b>
        </td>                           
            <?php
                $magassag = $variation->height;
                ?>
                <?php if ($magassag != 0) {
                echo '<td class="cell_magas">' . $variation->height . ' cm' . '</td>'; }
                else{
                ?>
                <style type="text/css">#magassag-cim{display:none;}</style>
            <?php } ?>                      
            <?php
                $atmero = $variation->width;
                ?>
                <?php if ($atmero != 0) {
                echo '<td class="cell_szell">' . $variation->width . ' cm' . '</td>'; }
                else{
                ?>
                <style type="text/css">#szellesseg-cim{display:none;}</style>
            <?php } ?>      
            <?php
                $szellesseg = $variation->length;
                ?>
                <?php if ($szellesseg != 0) {
                echo '<td class="cell_latime">' . $variation->length . ' cm' . '</td>'; }
                else{
                ?>
                <style type="text/css">#szellesseg-cim{display:none;}</style>
            <?php } ?>          
        <td>
            <?php echo $variation->price;?>
        </td>
    </tr>

【问题讨论】:

    标签: php wordpress woocommerce product variations


    【解决方案1】:

    您可以使用$variation-&gt;get_formatted_name()$variation-&gt;get_title()

    另外,我 95% 确定您会想要使用类方法而不是直接访问对象属性,这会在 WooCommerce 2.7 中导致错误/警告。此外,它通常会通过过滤器运行,以防任何插件针对该值。 (例如:使用$variation-&gt;get_price() 而不是$variation-&gt;price

    【讨论】:

    • 您好,但不适用于 get_XXX。可以直接访问变体名称吗?还是需要从其他变体查询中访问类方法?
    • 使用 $variation->get_formatted_name() 和 $variation->get_title() 我得到“产品名称的变体 #number”,但我需要产品变体的标题。
    • 正如 Loic 提到的,没有变化的标题。如果建议的方法不能满足您的需求,请澄清。 get_XXX() 并不总是正确的方法名称,并且这是 WC2.7 中的一个重大变化,不一定是向后兼容的。
    • 另一个选项是$variation-&gt;get_name();,因为它将输出父标题后跟变体标题,即Red Shirt - Large 而不是Red Shirt - Large (variation_numer)尺寸:大 这是我在使用get_formatted_name() 时得到的 :)
    猜你喜欢
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2013-01-10
    • 1970-01-01
    • 2020-09-10
    • 2018-08-26
    相关资源
    最近更新 更多