【问题标题】:Echo specific Product Attributes and Meta Data in WooCommerce Cart在 WooCommerce 购物车中回显特定的产品属性和元数据
【发布时间】:2016-12-13 06:52:48
【问题描述】:

更新(与作者 cmets 相关):

我想自定义 WooCommerce cart.php 以使用 Essential Grid premium plugin 在产品页面上显示一些可以正常工作的元数据。

我想显示一些产品属性字段以及我使用Essential Grid 插件的元字段创建器创建的一些自定义元字段。

为了测试,我使用 'Height' 属性(所以 'pa_height')和自定义字段 'Age''eg-age-cal'

目前,我已尝试使用以下方法:

<?php echo get_post_meta($product_id, 'pa_height', true );?>

还有:

<?php echo get_post_meta($product_id, 'eg-age-cal', true );?>

但这些似乎不起作用。

我已经设法让代码使用:

<?php echo get_post_meta($product_id, '_regular_price', true );?>

所以我知道代码正在运行。

我只是需要帮助解决,如何从这些自定义属性和自定义字段中获取值。

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    更新(与 WC 3+ 兼容)

    在您在下面的评论中解释之后,我发现您正在使用Essential Grid premium plugin(一个商业插件)来创建一些与您的 wooCommerce 产品相关的自定义字段和属性。

    在这一点上,我无能为力,因为我以前从未使用过这个插件,而且我不知道这个插件中的数据存储在数据库中的什么位置。

    我认为你不能使用常用的 WordPress/WooCommerce 函数来获取这些数据,这就是你不会像往常一样使用get_post_meta() 获取任何数据的原因......

    获得帮助的最佳方式是:
    - 搜索/探索您的数据库中的自定义字段数据。
    - 在 Essential Grid 插件作者支持线程中搜索/询问。


    我原来的回答:

    对于您的产品中定义的属性,使用带有 $product_id 变量的 get_post_meta() 函数,您需要这样使用它获取您想要的数据(这是一个值数组):

    // getting the defined product attributes
    $product_attr = get_post_meta( $product_id, '_product_attributes' );
    
    // displaying the array of values (just to test and to see output)
    echo var_dump( $product_attr );
    

    你也可以使用函数get_attributes()(更推荐),这样:

    // Creating an object instance of the product
    $_product = new WC_Product( $product_id );
    
    // getting the defined product attributes
    $product_attr = $_product->get_attributes();
    
    // displaying the array of values (just to test and to see output)
    echo var_dump( $product_attr );
    

    所有代码都经过测试并且可以运行。

    现在购物车数据已在 Cookie 和会话中设置,您需要使用 WC()-&gt;cart 语法来获取购物车数据和商品

    所以你可以使用这种代码来获取购物车中的物品(产品):

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if(!empty($product)){
    
            // getting the defined product attributes
            $product_attr = $_product->get_attributes();
    
            // displaying the attributes array of values (just to test and to see output)
            echo var_dump( $product_attr ) . '<br>';
        }
    }
    

    这将显示 CART 中每个产品的属性值数组。


    一个基于this thread的解决方案,在同一代码sn-p中使用wc_get_product_terms()来获取你的属性:

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if(!empty($product)){
    
            // compatibility with WC +3
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
            // Getting "height" product attribute
            $myAttribute = array_shift( wc_get_product_terms( $product_id, 'pa_height', array( 'fields' => 'names' ) ) );
            echo $myAttribute . '<br>';
        }
    }
    

    参考文献:

    【讨论】:

    • 太棒了,谢谢!我现在将检查此代码,看看它是否是我需要的:)。抱歉我的回复延迟,才重新登录
    • @StevenWright 没问题。如果您能告诉我您是如何获得此自定义字段以及如何将其保存到数据库的...谢谢
    • 对不起,我对你上面的代码有点困惑(我只是一个 PHP 初学者) - 我添加了你提供的代码的 sn-ps,但是,两个 var_dump 都输出完全相同的数组, 字段没有实际值(例如,高度为 123)。对于自定义字段,我们最终不得不使用“Essential Grid”插件创建它并使用他们的元字段创建器,这样它就可以很好地与他们的插件配合使用。感谢您到目前为止的帮助:)
    • @StevenWright 更新了您的问题和我的答案...由于您使用的是商业插件,因此很难获得帮助在这里,因为只有少数人拥有和使用这个商业插件。请参阅我的更新答案以获取解决方案。
    • wc_get_product_terms() 和没有商业插件的自定义属性有同样的问题。某些具有逗号分隔属性值的产品返回空值。 $product->get_attributes() 和 foreach 循环有效。是这个函数的一个错误。
    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 2020-07-13
    • 2019-05-19
    • 2018-09-05
    • 2017-12-10
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多