2

需要一些帮助 WooCommerce 为产品属性添加变体。

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

我想创建一个重复的事件,并且对于每次重复,我想添加价格和库存的变化。

所以过程是这样的:

  1. 将所有 ACF 字段添加到数组中。

  2. 使用此数组创建变体。

  3. 为属性添加变体。

问题是我不知道如何向属性( pa_data )添加变体;

这是显示我的属性的方式 https://i.imgur.com/YmbDFlO.png

这是我想要的显示方式 https://i.imgur.com/oDNvuOD.png

$days = array
    array(
        'date' => 'April 02, 2020',
        'price' => '10',
    ),
    array(
        'date' => 'April 03, 2020',
        'price' => '20',
    ),
    array(
        'date' => 'April 04, 2020',
        'price' => '10',
    ),
);


// This method inserts new data into the post after the post is saved
add_action('post_updated', 'ProductUpdate', 10, 3);


 // Callback function after the post is saved
function ProductUpdate($post_id, $post_after, $post_before) {
    if ( $post_after && get_post_type($post_id) == 'product' ) {
        ProductSetVariations( $post_id,  $postDataVariations );
    }
};


function ProductSetVariations($productID, $days ) {

     // Get product object
    $product = wc_get_product($productID);

    foreach ($days as $day ) {

        $date = $day['date'];
        $price = $day['price'];

        $variationPost = array(
            'post_title' => $product->get_title(),
            'post_name' => 'product-' . $productID . '-variation',
            'post_status' => 'publish',
            'post_parent' => $productID,
            'post_type' => 'product_variation',
            'guid' =>  $product->get_permalink()
        );

        // Insert the new variation post;
        $variationID = wp_insert_post($variationPost);

        // Create the new post based on the id
        $variation = new WC_Product_Variation($variationID); 

        $taxonomy = 'pa_data';

        // If term dosent exsist in taxonomy than add it
        if ( !term_exists( $date, $taxonomy )) {
            wp_insert_term( $date, $taxonomy );
        };

        $term_slug = get_term_by('name', $date, $taxonomy)->slug; // Get the term slug  


        $post_term_names =  wp_get_post_terms( $productID, $taxonomy, array('fields' => 'names') );

        if( ! in_array( $date, $post_term_names ) ) {
             wp_set_post_terms( $productID, $date, $taxonomy, true );    
        };

        $term_slug = get_term_by('name', $date, $taxonomy)->slug; // Get the term slug  
        update_post_meta( $variationID, 'attribute_'.$taxonomy, $term_slug );    

        $variation->set_price($price);
        $variation->save(); // Save the data

    } 
};

解决!

解决方案:

问题出在 WordPress 挂钩“post_updated”上,它确实将更改保存在数据库中,但不会在管理员中更改。

这是解决方案,适用于必须从 rest api 更新帖子或仅发布更新的人。

这只是一个简单的演示。

    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']); 

   }     
};
4

0 回答 0