从 WooCommerce 购物车中获取变体属性标签名称和价值产品

时间:2021-02-20 05:37:36

标签: php woocommerce cart custom-taxonomy taxonomy-terms

我应该获取购物车中产品的所有数据(产品名称和单个产品的属性)。作为参考,我有这段代码允许每个产品只显示一个属性。

您能帮我找到查看所有属性的解决方案吗? 商店里一共有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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您要检索产品变体购物车项目中设置的产品属性,可以使用以下代码示例:

for (let i = 0; i < array2.length; i++) {
    if (!array1.includes(array2[i])) {
        array1.push(array2[i])
    }
}

经过测试并有效