Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Wordpress 基于变体以编程方式设置属性 在向产品属性添加变体方面需要一些帮助。_Wordpress_Woocommerce_Attributes_Product_Product Variations - Fatal编程技术网

Wordpress 基于变体以编程方式设置属性 在向产品属性添加变体方面需要一些帮助。

Wordpress 基于变体以编程方式设置属性 在向产品属性添加变体方面需要一些帮助。,wordpress,woocommerce,attributes,product,product-variations,Wordpress,Woocommerce,Attributes,Product,Product Variations,我创建了一个名为Data(pa_Data)的属性,该属性存储使用ACF添加的自定义字段中的术语,该字段是一个日期选择器 我想创建一个重复发生的事件,对于每个重复发生的事件,我想添加一个价格和股票的变化 所以这个过程是这样的: 在数组中添加所有ACF字段 使用此数组创建变体 向属性添加变体 问题是我不知道如何向属性(pa_数据)添加变体 下面是如何显示我的属性 以下是我希望被展示的方式 断然的! 解决方案: 问题在于WordPress钩子“post_updated”,它会将更改保存在数据库中,

我创建了一个名为Data(pa_Data)的属性,该属性存储使用ACF添加的自定义字段中的术语,该字段是一个日期选择器

我想创建一个重复发生的事件,对于每个重复发生的事件,我想添加一个价格和股票的变化

所以这个过程是这样的:

  • 在数组中添加所有ACF字段

  • 使用此数组创建变体

  • 向属性添加变体

  • 问题是我不知道如何向属性(pa_数据)添加变体

    下面是如何显示我的属性

    以下是我希望被展示的方式

    断然的! 解决方案:

    问题在于WordPress钩子“post_updated”,它会将更改保存在数据库中,但不会在admin中更改

    这是一个解决方案,适用于那些必须从RESTAPI更新帖子或只是发布更新的人

    这只是一个简单的演示

        add_action('woocommerce_update_product', 'ProductUpdate', 10, 3);
    
        function ProductUpdate($post_id) {
            // Insert Product Attributes
            function insert_product_attributes ($post_id, $variations) {
    
               $product = wc_get_product($post_id);
    
                // Set up an array to store the current attributes values.
               $values = array();
    
               foreach ($variations as $variation)  {
                   array_push($values, $variation['date']);          
               }
    
               wp_set_object_terms($post_id, $values, 'pa_data', true );
    
               $product_attributes_data = array('pa_data' => 
                    array(
                        'name'         => 'pa_data',
                        'position'     => '1',
                        'is_visible'   => '1',
                        'is_variation' => '1',
                        'is_taxonomy'  => '1',
                    );
               );
    
               update_post_meta($post_id, '_product_attributes', $product_attributes_data);  
            };
    
            function insert_product_variations ($post_id, $variations) {   
    
                foreach ($variations as $index => $variation) {
    
                $attribute_term = get_term_by('name', $variation['date'], 'pa_data');
    
                if ( $variation['date'] == $attribute_term ) {
                    return;
                }
    
                $variation_post = array( // Setup the post data for the variation
                    'post_title'  => 'Variation #'.$index.' of data for product#'. $post_id,
                    'post_name'   => 'product-'.$post_id.'-variation-'.$index,
                    'post_status' => 'publish',
                    'post_parent' => $post_id,
                    'post_type'   => 'product_variation',
                    'guid'        => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $index
                );
    
                $variation_post_id = wp_insert_post($variation_post); // Insert the variation
    
                // We need to insert the slug not the name into the variation post meta
                update_post_meta($variation_post_id, 'attribute_pa_data', $attribute_term->slug);
                update_post_meta($variation_post_id, '_price', $variation['price']);
                update_post_meta($variation_post_id, '_regular_price', $variation['price']);
            }
    
            function ProductSetVariations($post_id ) {
    
               // Get product object
               $product = wc_get_product($post_id);
    
               // Verify if the product is variable or simple product
               if ( !$product->is_type( 'variable' ) ) {
                   return;
               };
    
               $product_data = array(
                   'days' => array(
                       array(
                          'sku' => '123SKU',
                          'date' => '1 April 2020',
                          'price' => '10',
                          'stock' => '20'
                       ),
                       array(
                          'sku' => '456SKU',
                          'date' => '2 April 2020',
                          'price' => '10',
                          'stock' => '20'
                       ),
                       array(
                          'sku' => '789SKU',
                          'date' => '3 April 2020',
                          'price' => '10',
                          'stock' => '20'
                    )
                )
            );
    
            insert_product_attributes($post_id, $product_data['days']); // Insert variations passing the new post id & variations
            insert_product_variations($post_id, $product_data['days']); 
    
       }     
    };
    
        add_action('woocommerce_update_product', 'ProductUpdate', 10, 3);
    
        function ProductUpdate($post_id) {
            // Insert Product Attributes
            function insert_product_attributes ($post_id, $variations) {
    
               $product = wc_get_product($post_id);
    
                // Set up an array to store the current attributes values.
               $values = array();
    
               foreach ($variations as $variation)  {
                   array_push($values, $variation['date']);          
               }
    
               wp_set_object_terms($post_id, $values, 'pa_data', true );
    
               $product_attributes_data = array('pa_data' => 
                    array(
                        'name'         => 'pa_data',
                        'position'     => '1',
                        'is_visible'   => '1',
                        'is_variation' => '1',
                        'is_taxonomy'  => '1',
                    );
               );
    
               update_post_meta($post_id, '_product_attributes', $product_attributes_data);  
            };
    
            function insert_product_variations ($post_id, $variations) {   
    
                foreach ($variations as $index => $variation) {
    
                $attribute_term = get_term_by('name', $variation['date'], 'pa_data');
    
                if ( $variation['date'] == $attribute_term ) {
                    return;
                }
    
                $variation_post = array( // Setup the post data for the variation
                    'post_title'  => 'Variation #'.$index.' of data for product#'. $post_id,
                    'post_name'   => 'product-'.$post_id.'-variation-'.$index,
                    'post_status' => 'publish',
                    'post_parent' => $post_id,
                    'post_type'   => 'product_variation',
                    'guid'        => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $index
                );
    
                $variation_post_id = wp_insert_post($variation_post); // Insert the variation
    
                // We need to insert the slug not the name into the variation post meta
                update_post_meta($variation_post_id, 'attribute_pa_data', $attribute_term->slug);
                update_post_meta($variation_post_id, '_price', $variation['price']);
                update_post_meta($variation_post_id, '_regular_price', $variation['price']);
            }
    
            function ProductSetVariations($post_id ) {
    
               // Get product object
               $product = wc_get_product($post_id);
    
               // Verify if the product is variable or simple product
               if ( !$product->is_type( 'variable' ) ) {
                   return;
               };
    
               $product_data = array(
                   'days' => array(
                       array(
                          'sku' => '123SKU',
                          'date' => '1 April 2020',
                          'price' => '10',
                          'stock' => '20'
                       ),
                       array(
                          'sku' => '456SKU',
                          'date' => '2 April 2020',
                          'price' => '10',
                          'stock' => '20'
                       ),
                       array(
                          'sku' => '789SKU',
                          'date' => '3 April 2020',
                          'price' => '10',
                          'stock' => '20'
                    )
                )
            );
    
            insert_product_attributes($post_id, $product_data['days']); // Insert variations passing the new post id & variations
            insert_product_variations($post_id, $product_data['days']); 
    
       }     
    };