问题描述
我应该获取购物车中产品的所有数据(产品名称和单个产品的属性)。作为参考,我有这段代码允许每个产品只显示一个属性。
您能帮我找到查看所有属性的解决方案吗? 商店里一共有10个属性
$taxonomy = 'pa_selezione-pezzi';
// Get an instance of the WC_Product Object (necessary if you don't have it)
// from a dynamic $product_id (or defined $product_id)
$product = wc_get_product($product_id);
// Iterating through the product attributes
foreach($product->get_attributes() as $attribute){
// targeting the defined attribute
if( $attribute->get_name() == $taxonomy ){
// Iterating through term IDs for this attribute (set for this product)
foreach($attribute->get_options() as $term_id){
// Get the term slug
$term_slug = get_term( $term_id,$taxonomy )->slug;
// Output
$confirmation = str_ireplace("{term_slug}",$term_slug,$confirmation);
}
}
}
解决方法
如果您要检索产品变体购物车项目中设置的产品属性,可以使用以下代码示例:
for (let i = 0; i < array2.length; i++) {
if (!array1.includes(array2[i])) {
array1.push(array2[i])
}
}
经过测试并有效