Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如何获取产品变化值_Php_Arrays_Wordpress_Foreach_Woocommerce - Fatal编程技术网

Php 如何获取产品变化值

Php 如何获取产品变化值,php,arrays,wordpress,foreach,woocommerce,Php,Arrays,Wordpress,Foreach,Woocommerce,我希望能够列出变量选项值。例如,我有一个12英寸、14英寸和16英寸的灯笼。我希望能够得到这些值。我一直在尝试使用foreach循环来获取这些值,但我需要一些帮助。这是我的密码 function test_func(){ global $woocommerce, $product, $post; // test if product is variable if( $product->is_type( 'variable' ) ){ $availab

我希望能够列出变量选项值。例如,我有一个12英寸、14英寸和16英寸的灯笼。我希望能够得到这些值。我一直在尝试使用foreach循环来获取这些值,但我需要一些帮助。这是我的密码

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    if( $product->is_type( 'variable' ) ){
        $available_variations = $product->get_available_variations();
        // var_dump($available_variations);

        foreach( $available_variations as $key => $value ){ 
            var_dump( $value['attributes'] ) ;
        }
    }
}
这就是输出:

array(1) { ["attribute_pa_size"]=> string(4) "12in" } array(1) { ["attribute_pa_size"]=> string(4) "14in" } array(1) { ["attribute_pa_size"]=> string(4) "16in" }
正如你所看到的,我想要的值在那里,但我不知道如何让它们响应它们

以下是我使用var_dump()$available_variations时得到的结果

“[“允许延期交货”]=>bool(false)[“维度”]=>array(3){[“长度”]=>string(4)”11.8”[“宽度”=>string(4)“11.8”[“高度”]=>string(4)”11.8”[“维度”=>string(21)“11.8 x 11.8 x 11.8英寸”[“显示价格”=>float(3.2)[“显示常规价格”=>float(3.2)”[“图像”=>string(18)”title[>string(18)”jpr-671“>[“字幕”]=>string(0)”“[“url”=>string(59)”“[“alt”=>string(0)”“[“src”=>string(67)”[“srcset”=>string(445)”600w、150w、300w、768w、1024w、100w”[“大小”=>string(31)”(最大宽度:600px)100vw、600px”[“full\u src”=>string(59)”“[“full\u src\u w”=>int(2000)[“full\u-src”=>string(2000)]“图库缩略图建筑工地工地的缩略图建筑工地工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的整体(100)门汀(100)[“拇指拇指建筑工地的工地工地工地工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的缩略图建筑工地的整体(100)==>int 100(100)门汀(100)[“拇指拇指拇指拇指建筑工地工地的拇指(拇指(拇指)钢钢钢钢钢钢钢的[[8 8 8 8 8.8 8 8 8 8.8 8 8 8][===>字符串(67=>字符串(67)”=>字符串(67)”>字符串(67)”>字符串(67)”””””““““““[拇指(67)”””””””””””””””[[[拇指拇指拇指拇指拇指拇指拇指拇指拇指拇指拇指拇指L(对)[”是否单独出售“]=>string(2)“否”[“是虚拟的”]=>bool(假)[“最大数量”]=>int(17)[“最小数量”]=>int(1)[“价格html”]=>string(145)”

“[“sku”]=>string(5)“6712R”[“variation_description”]=>string(0)”“[“variation_id”]=>int(1462)[“variation_is_active”]=>bool(true)[“variation_is_visible”]=>bool(true)[“weight”]=>string(0)”[“weight_html”=>string(3)“N/A”}[1]=>array(24){“attributes”=>array(1){“attributes)=>array(1){“attributes)][pa大小”=>string(attributes)}>14html)[]=>字符串(51)”


这只适用于一种产品,每种变体有一种,但这让您了解了它的工作原理。我也愿意尝试其他方法以获得相同的结果,因此如果您知道一种方法,请告诉我。谢谢您需要对产品属性使用第二个foreach循环:

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    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;

                // Testing output
                echo '<p>' . $attr_label_name . ': ' . $term_name . '</p>';
            }
        }
    }
}
功能测试_func(){
全球$woocmerce、$product、$post;
//测试产品是否可变
如果($product->is_类型('variable')){
//循环查看可用的产品变化数据
foreach($product->get\u available\u variations()作为$key=>$variation){
//循环浏览此变体的产品属性
foreach($variation['attributes']作为$attribute=>$term\u slug){
//获取分类法slug
$taxonomy=str_replace('attribute_','''$attribute);
//获取属性标签名称
$attr\u label\u name=wc\u attribute\u label($taxonomy);
//显示属性标签名称
$term\u name=get\u term\u by('slug',$term\u slug,$taxonomy)->name;
//测试输出
回显“”.$attr_label_name.”:“.$term_name.”

”; } } } }
如何获取产品变体说明,我指的是变体中的说明,而不是主要产品说明