获取产品自定义属性Woocommerce

时间:2018-02-16 10:07:45

标签: wordpress woocommerce html-table attributes

我在functions.php中有一个产品自定义变体表模板,我试图获取产品自定义属性。我有两个(日期和地点)。 我找到了很多解决方案,但没有一个对我不起作用。 Woocommerce版本3.2.6。

下面的代码我得到

  

注意:只应通过引用传递变量。

我得到的类型' Null' $ date。

<td class="date">
    <?php 
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
        $date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
    ?>
</td>

这是我在function.php中的完整产品变体表代码

function woocommerce_variable_add_to_cart(){
    global $product, $post, $woocommerce;

    $attributes = $product->get_attributes();

    $variations = find_valid_variations();

    // Check if the special 'price_grid' meta is set, if it is, load the default template:
    if ( get_post_meta($post->ID, 'price_grid', true) ) {
        wp_enqueue_script( 'wc-add-to-cart-variation' );

        wc_get_template( 'single-product/add-to-cart/variable.php', array(
                'available_variations'  => $product->get_available_variations(),
                'attributes'            => $product->get_variation_attributes(),
                'selected_attributes'   => $product->get_variation_default_attributes()
            ) );
        return;
    }

    ?>
    <table class="variations variations-grid" cellspacing="0">
    <thead>
        <tr>
            <td> Date | Location </td>
            <td> Price </td>
            <td> Quantity </td>
            <td> Availability </td>
            <td> </td>
        </tr>
    </thead>
        <tbody>
            <?php
            foreach ($variations as $key => $value) {
                if( !$value['variation_is_visible'] ) continue;
            ?>
            <tr>
                <td class="date">
                    <?php 
                        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support
                        $date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
                    ?>
                </td>
                <td class="price">
                        <?php echo '<span>&pound;</span>' . $product->get_price(); ?>
                </td>
        <td class="quantity">
            <?php woocommerce_quantity_input(); ?>
        </td>
        <td class="stock">
            <?php if (!$value['is_in_stock'] ) { ?>
                  <p class="stock out-of-stock"><?php _e( 'Places Not Available', 'woocommerce' ); ?></p>
                        <?php } else { ?>
             <p class="stock in-stock"><?php _e( 'Places Available', 'woocommerce' ); ?></p>
                </td>
                <td class="add-to-cart">
                    <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
                        <?php
                        if(!empty($value['attributes'])){
                            foreach ($value['attributes'] as $attr_key => $attr_value) {
                            ?>
                            <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
                            <?php
                            }
                        }
                        ?>
                        <button type="submit" class="single_add_to_cart_button button alt"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
                        <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
                        <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
                        <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
                    </form>
                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
    <?php
}


function find_valid_variations() {
    global $product;

    $variations = $product->get_available_variations();
    $attributes = $product->get_attributes();
    $new_variants = array();

    foreach( $variations as $variation ) {

        // Peruse the attributes.

        // 1. If both are explicitly set, this is a valid variation
        // 2. If one is not set, that means any, and we must 'create' the rest.

        $valid = true; // so far
        foreach( $attributes as $slug => $args ) {
            if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
                // Exists

            } else {
                // Not exists, create
                $valid = false; // it contains 'anys'

             foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
                    $attribute = trim( $attribute );
                    $new_variant = $variation;
                    $new_variant['attributes']["attribute_$slug"] = $attribute;
                    $new_variants[] = $new_variant;
                }

            }
        }

        // This contains ALL set attributes, and is itself a 'valid' variation.
        if( $valid )
            $new_variants[] = $variation;

    }

    return $new_variants;
}

更新

我得到了这段代码,但我在一个标签中得到了两个属性值(日期位置)。 如何分离属性?

<td>
    <?php 
        foreach($value['attributes'] as $key => $val ) {
            $val = str_replace(array('-','_'), ' ', $val);
            printf( '<span class="attr attr-%s">%s</span>', $key, ucwords($val) );
        }
    ?>
</td>

更新

最后我得到了日期和位置,但有额外的警告,通知和单个字符。

enter image description here

代码

<?php 
        foreach($value as $date ) {
                printf($date['attribute_date']);
        }
?>

** 已解决 **

也许不是最好的方法,但它正在发挥作用。

<td class="date">
        <?php
                $i = 0;
                foreach($value['attributes'] as $key => $val ) {
                        if($i == 0 ) {
                                echo $val;
                        }
                $i++;
                }
        ?>
</td>

<td class="location">
        <?php
                $i = 0;
                foreach($value['attributes'] as $key => $val ) {
                        if($i !== 0) {
                                echo $val;
                        }
                $i++;
                }
        ?>
</td>

2 个答案:

答案 0 :(得分:2)

使用方法get_attributes()和函数wc_get_product_terms()描述here

<?php
// Get the attributes
$attributes = $product->get_attributes();
// Start the loop
foreach ( $attributes as $attribute ) : ?>

<?php
// Check and output, adopted from /templates/single-product/product-attributes.php
    if ( $attribute['is_taxonomy'] ) {
        $values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
        echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
    } else {
        // Convert pipes to commas and display values
        $values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
        echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
    }
?>

<?php endforeach; ?>

如果查看模板\templates\single-product\product-attributes.php,您可以看到如何获取各个属性名称。

if ( $attribute->is_taxonomy() ) {
    $attribute_taxonomy = $attribute->get_taxonomy_object();
    $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );

    foreach ( $attribute_values as $attribute_value ) {
        $value_name = esc_html( $attribute_value->name );

        if ( $attribute_taxonomy->attribute_public ) {
            $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
        } else {
            $values[] = $value_name;
        }
    }
}

答案 1 :(得分:0)

以下是获取特定自定义属性的方法:

// Get the custom attribute
$svd_attribute = array_shift( wc_get_product_terms( $product->id, 'your_custom_attribute', array( 'fields' => 'names' ) ) );
// Display if String
if ( is_string( $svd_attribute ) ) {
    echo wp_kses_post( "$svd_attribute" );
}

只需替换“ your_custom_attribute”

从事WooCommerce:3.8.0