WooCommerce 挂钩中的产品变体和父变量产品对象

Product variation and parent variable product objects in WooCommerce hook

我为变体开发了自定义交货时间信息。但出于某种原因,我无法获取变体的父产品 ID 以在我的函数中使用它。我尝试了以下 - 如果我将 $id 直接设置为 51237,我的代码可以工作,但是使用 $post 来获取 id,它不会。 任何帮助表示赞赏!

// here I am loading special infos for each variation

add_filter( 'woocommerce_available_variation', 'load_variation_products_fields', 1 );
function load_variation_products_fields( $variations ) {
    $variations['variation_cmpzDeliveryTime'] = get_delivery_time( $variations[ 'variation_id' ] ) ;
    return $variations;
}


function get_delivery_time ( $product_id) {
    $product_obj = wc_get_product( $product_id );
    

    // here I want the post ID (so the parent product ID if the product type is variation)
    global $post;
    $id = $post->ID; 

    var_dump ($id); // this outputs "int(51237)" for each variation - that seems to be right!

    switch($product_obj->get_type())    {
        case 'variation':
            if (Helpers::get_product_multi_inventory_status($product_id) == 'no' ) {
                $_pf = new WC_Product_Factory(); 
                $_product = $_pf->get_product($product_id);
                $stock_status = $_product->get_stock_status();
                if ( wc_gzd_get_product( $product_id )->get_delivery_time_html() == '' )    { 
                    if ( ($stock_status == 'instock')   )   {
                        // $id is ignored - if I put here 51237, it works!
                        return set_delivery_text('instock', $id); 
                    } else  {
                        return set_delivery_text('outofstock');
                    }
                }
            }
            elseif (Helpers::get_product_multi_inventory_status($product_id) == 'yes' ) {
                return get_delivery_time_multi_inventory($product_id);
            }
            break;
    }
}

woocommerce_available_variation 中挂接的函数中缺少一些参数......您需要将其替换为:

// here I am loading special infos for each variation
add_filter( 'woocommerce_available_variation', 'load_variation_products_fields', 10, 3 );
function load_variation_products_fields( $variation_data, $product, $variation ) {
    $variation_data['variation_cmpzDeliveryTime'] = get_delivery_time( $variation_data, $product, $variation ) ;
    return $variation_data;
}

现在您可以看到有 3 个参数,其中 $product 是父变量产品对象,$variation 是产品变体对象。

所以你要改变你也必须改变get_delivery_time()函数代码,比如:

function get_delivery_time ( $variation_data, $product, $variation ) {
    $variation_id      = $variations['variation_id']; // or $variation->get_id();
    $is_in_stock       = $variation['is_in_stock']; // is variation "in stock"
    $parent_product_id = $product->get_id(); // The parent variable product Id
    // var_dump($parent_product_id);

    if ( Helpers::get_product_multi_inventory_status($variation_id) == 'no' ) { 
        if ( wc_gzd_get_product( $variation_id )->get_delivery_time_html() == '' )    { 
            if ( $is_in_stock ) {
                return set_delivery_text('instock', $parent_product_id); 
            } else  {
                return set_delivery_text('outofstock');
            }
        }
    } else {
        return get_delivery_time_multi_inventory($variation_id);
    }
}

它应该会更好用。