【问题标题】:Unable to return 'attribute' data in Woocomerce无法在 Woocommerce 中返回“属性”数据
【发布时间】:2018-01-22 14:11:21
【问题描述】:

pa_我正在尝试输入产品的属性数据,以便比较购物车中 items 之间的属性。但是,我无法访问数据。以下代码在 var_dump 命令中返回以下内容:

C:\wamp\www\wizstaginglocal\wp-content\plugins\code-sn-ps\php\sn-p-ops.php(426) : eval()'d code:17:boolean错误

也就是说什么都没有

代码:

add_action('woocommerce_before_cart', 'wiz_scale_woocommerce_before_cart');

function wiz_scale_woocommerce_before_cart() {
    foreach(WC() -> cart -> get_cart() as $cart_item_key => $cart_item) {
        // HERE the WC_Product object
        $product = $cart_item['data'];
        echo "My Product ID is {$cart_item['product_id']} \n";

        $attributes = $product -> get_attributes();
        foreach($attributes as $taxonomy => $value) {
            // The WP_Term object
            $term_obj = get_term_by('pa_1_scale', $value, $taxonomy);
            $term_name = $term_obj -> name;

        }
        echo '<pre>';
        var_dump($term_obj);
        echo '</pre>';
    }
}

在 $attributes 节目的 var_dump 中,对于相关的 slug:

数组 (size=3).... 'pa_1_scale' => 对象(WC_Product_Attribute)[1383] 受保护的“数据”=> 数组(大小=6) 'id' => int 1 '名称' => 字符串'pa_1_scale'(长度=10) '选项' => 数组(大小=1) ... '位置' => int 2 '可见' => 布尔真 '变体' => boolean false

我也意识到this post:是这个原因吗?

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    里面的属性数据是序列化的,我找不到提取方法。成功的解决方法是将 SQL 查询写入 WP 数据库。

    function mycustomfunc_retrieve_attribute_for product() {
    foreach(WC() -> cart -> get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['product_id'];   
    // perform wpdb search
    global $wpdb;
    $myatt = $wpdb->get_var(
        $wpdb->prepare(
        "SELECT DISTINCT
        t.name AS 'Term Name'
        FROM
        wiz_posts AS p
        INNER JOIN
        wiz_term_relationships AS tr ON p.id = tr.object_id
        INNER JOIN
        wiz_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
        INNER JOIN
        wiz_terms AS t ON t.term_id = tt.term_id
        WHERE
        p.post_type = 'product'
        AND
        p.ID = %d
        AND
        tt.taxonomy = 'mySlugName'
        ",
        $product
        )
        );
    
     echo "My Product ID is {$product} and the attribute is {$myatt} \n";
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多