【问题标题】:woocommerce variations - add post_meta to dropdownwoocommerce 变体 - 将 post_meta 添加到下拉列表
【发布时间】:2013-08-02 22:30:08
【问题描述】:

我目前正在与 Woocommerce 合作。以下代码当前将变体价格添加到产品页面的下拉列表中:

function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;

$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

$term_slug = ( !empty( $result ) ) ? $result[0] : $term;


$query = "SELECT postmeta.post_id AS product_id
            FROM {$wpdb->prefix}postmeta AS postmeta
                LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
            WHERE postmeta.meta_key LIKE '_wholesale_price%'
                AND postmeta.meta_value = '$term_slug'
                AND products.post_parent = $product->id";

$variation_id = $wpdb->get_col( $query );

$parent = wp_get_post_parent_id( $variation_id[0] );

if ( $parent > 0 ) {
    $_product = new WC_Product_Variation( $variation_id[0] );
    return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;

}

我想编辑以下行:

return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';

与其拉价格,我想显示已经保存的custom_meta。

我尝试将以上内容替换为:

return $term . ' (' . get_post_meta( get_the_ID(), '_wholesale_price', true ) . ')';

但它什么也没返回。有谁知道实现这一目标的正确方法?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    如果数据在循环之外,您需要先获取数据。试试这个:

    global $post;
    return $term . ' (' . get_post_meta($post->ID, '_wholesale_price', true) . ')';
    

    【讨论】:

    • 您好,这不适用于变体 ID。这将返回主帖子 ID。
    【解决方案2】:

    我设法通过执行以下操作来解决这个问题:

    return $term . ' (' .  $value['data']->variation_id, '_wholesale_price', true ) . ')';
    

    【讨论】:

    • 你知道$value变量是从哪里来的吗?它没有在您的代码中设置,也没有作为全局变量包含在内。
    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 2019-07-17
    • 2021-05-09
    • 2017-08-02
    • 1970-01-01
    • 2018-04-27
    • 2023-03-06
    相关资源
    最近更新 更多