首页 > 解决方案 > WooCommerce:比较变体属性值

问题描述

我正在销售成对出现的产品。因此,当下订单时,通常它包含两个变体:“顶部”和“底部”。

通常两种变体都包含相同的属性值,除了一个属性。

我想比较这两种变体,以查看除一个之外的所有属性值是否相同。

我正在尝试使用以下代码片段来做到这一点:

if( $product->is_type( 'variable' ) ){
    // Loop through available product variation data
    foreach ( $product->get_available_variations() as $key => $variation ) {
        // Loop through the product attributes for this variation
        foreach ($variation['attributes'] as $attribute => $term_slug ) {
            // Get the taxonomy slug
            $taxonmomy = str_replace( 'attribute_', '', $attribute );

            // Get the attribute label name
            $attr_label_name = wc_attribute_label( $taxonmomy );

            // Display attribute labe name
            $term_name = get_term_by( 'slug', $term_slug, $taxonmomy )->name;

标签: phpwordpresswoocommerce

解决方案


推荐阅读