Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “显示”;库存;WooCommerce类别归档中的产品变体自定义属性值_Php_Wordpress_Woocommerce_Attributes_Product - Fatal编程技术网

Php “显示”;库存;WooCommerce类别归档中的产品变体自定义属性值

Php “显示”;库存;WooCommerce类别归档中的产品变体自定义属性值,php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,我在woocommerce有一家商店,我想在分类页面上显示可用的大小。我这样做了,但我不想让库存中的尺码显示出来。此时,分类页面上列出了分配给产品的所有尺寸。我只想显示那些有变化的股票 我试着给wc\u get\u product\u术语添加更多属性,但没有成功。这是当前的代码,运行良好,但不是我想要的: add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 ); function custo

我在woocommerce有一家商店,我想在分类页面上显示可用的大小。我这样做了,但我不想让库存中的尺码显示出来。此时,分类页面上列出了分配给产品的所有尺寸。我只想显示那些有变化的股票

我试着给wc\u get\u product\u术语添加更多属性,但没有成功。这是当前的代码,运行良好,但不是我想要的:

add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );

function custom_display_post_meta() {
    global $product;

    //Doing this for more types of sizes, so list only if it's necesarry
    $size = $product->get_attribute('pa_size');
    if( $size )
    {
        $values = wc_get_product_terms( $product->id, 'pa_size', array( 'fields' => 'names' ) );
        echo implode( ', ', $values );
    }
}

有可能吗?

您需要浏览变体库存数量,以便在WooCommerce类别存档页面中仅显示每个可变产品的库存大小,方法如下:

add_action( 'woocommerce_after_shop_loop_item', 'displaying_sizes_in_stock', 9 );
function displaying_sizes_in_stock() {
    global $product;

    // HERE, set your attribute taxonomy (once)
    $attr_taxonomy = 'pa_size';

    $size = $product->get_attribute( $attr_taxonomy );

    // Targeting variable products with size attribute (on product category archives)
    if( $product->is_type('variable') && $size && is_product_category() ){
        $variations = $product->get_available_variations( );

        // Product ID - compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        // Iterating Through all available variation for that product
        foreach($product->get_available_variations() as $values ){
            // Get an instance of WC_Product_Variation object
            $variation_obj = wc_get_product( $values['variation_id'] );
            // get the defined stock quantity
            $stock_quantity = $variation_obj->get_stock_quantity();
            if( ! $stock_quantity )
                $stock_quantity = $product->get_stock_quantity();
            // When product variation is in stock
            if( $stock_quantity > 0 && $stock_quantity ){
                // Get the variation attributtes
                $variation_attributes = $variation_obj->get_variation_attributes();
                // For "size" attribute only
                if( array_key_exists( "attribute_$attr_taxonomy", $variation_attributes) ){
                    // Get the term slug
                    $attr_slug = $variation_attributes["attribute_$attr_taxonomy"];
                    // Get an instance of WP_Term object
                    $term_obj = get_term_by( 'slug', $attr_slug, $attr_taxonomy );
                    // Get the attribute name
                    $attr_name[] = $term_obj->name;
                }
            }
        }
        // Output the "sizes" that have stock
        echo implode( ', ', array_unique( $attr_name ) ) . ' ';
    }
}
该代码位于活动子主题(或主题)的function.php文件或任何插件文件中


这段代码经过测试,适用于wooCommerce版本3+。

这正是我所需要的。我找的地方不对。非常感谢你!