pa_我正试图哄骗产品'属性数据,以便比较购物车中 items 之间的属性。但是,我无法访问数据。以下代码在 var_dump 命令中返回以下内容:
C:\ wamp \ www \ wizstaginglocal \ wp-content \ plugins \ code-snippets \ php \ snippet-ops.php(426):eval()' d code:17:boolean false
换句话说没什么
代码:
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:
数组(大小= 3)....&#39; pa_1_scale&#39; =&GT; 对象(WC_Product_Attribute)[1383] 受保护的数据&#39; =&GT; 数组(大小= 6) &#39; ID&#39; =&GT; int 1 &#39;名称&#39; =&GT;字符串&#39; pa_1_scale&#39; (长度= 10) &#39;选项&#39; =&GT; 数组(大小= 1) ... &#39;位置&#39; =&GT; int 2 &#39;可见&#39; =&GT;布尔值为true &#39;变异&#39; =&GT;布尔值假
我也意识到this post:这是什么原因?
答案 0 :(得分:0)
属性数据是序列化的,我找不到提取它的方法。成功的工作是将一个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";
}