Woocommerce如何从function.php获取自定义字段值

时间:2019-03-14 13:00:56

标签: php wordpress woocommerce

我正在尝试使用以下代码通过functions.php获取自定义字段值:

function discount_base_quantity(){
    global $woocommerce;
    global $post;

    $product  = $woocommerce->cart->get_cart();

    foreach ($product as $key => $value) {
        $product_id = wc_get_product($value['data']->get_id());
    $field = get_post_meta($product_id,'option_a');
    echo $field; }}

但是它没有用。我想念什么?还有另一种方法吗?

1 个答案:

答案 0 :(得分:1)

我发现了问题。实际上,我没有在get_post_meta函数中传递产品ID。所以这段代码对我来说很有效:

        function discount_base_quantity(){
        global $woocommerce;

        $product  = $woocommerce->cart->get_cart();

        foreach ($product as $key => $value) {
                $id = $value['product_id'];
                //echo $product_id;
                $field = get_post_meta($id,'option_a', true);
                echo $field;
}
    }