php - How to get default variation ID (woocommerce)

admin2025-01-08  3

I need to create an additional "Add to Cart" button on the product page. In a simple product there is no problem with this. It is more difficult in a product with variants. To create such a button I need to know the ID of the default variant.

$product->get_default_attributes();

returns only the attribute name and value, no ID.

How to get the ID of the default variant in php code?

I need to create an additional "Add to Cart" button on the product page. In a simple product there is no problem with this. It is more difficult in a product with variants. To create such a button I need to know the ID of the default variant.

$product->get_default_attributes();

returns only the attribute name and value, no ID.

How to get the ID of the default variant in php code?

Share Improve this question asked Jul 16, 2023 at 17:21 patryk siutapatryk siuta 311 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0
<?php

/**
 * Get default product variation
 *
 * note: this method check for exact match.
 *
 * @param mixed  $product_id WC_Product|WP_Post|int|bool $product Product instance, post instance, numeric or false to use global $post.
 * @param string $return Optional. The format to return the results in. Can be 'id' to return id of variation or 'object' for the product variation object. Default 'id'.
 *
 * @return int|WC_Product_Variation return 0 if not found.
 */
function wc_get_default_variation( $product_id = false, $return = 'id' ) {
    // do not use wc_get_product() to bypass some limits
    $product = WC()->product_factory->get_product( $product_id );

    if ( empty( $product ) || ! $product instanceof WC_Product_Variable ) {
        return 0;
    }

    if ( $product->has_child() ) {
        $attributes = $product->get_default_attributes();

        if ( ! empty( $attributes ) ) {
            $check_count      = true;
            $attributes_count = count( $attributes );

            // get in-stock (if enabled in wc options) and visible variations
            $variations = $product->get_available_variations( 'objects' );

            foreach ( $variations as $variation ) {
                $variation_attributes = $variation->get_attributes();

                // check count for first time
                // if not match, it means that user do not set default value for some variation attrs
                if ( $check_count && $attributes_count !== count( $variation_attributes ) ) {
                    break;
                }

                // no need to check count anymore
                $check_count = false;

                // remove 'any' attrs (empty values)
                $variation_attributes = array_filter( $variation_attributes );

                // add 'any' attrs with default value
                $variation_attributes = wp_parse_args( $variation_attributes, $attributes );

                // check is default
                if ( $variation_attributes == $attributes ) {
                    if ( $return === 'id' ) {
                        return $variation->get_id();
                    }

                    return $variation;
                }
            }
        }
    }

    return 0;
}

https://gist.github.com/AkramiPro/c8ddb83d253714730ac26d3a64e7941e

Try this code

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
    $available_variations = $product->get_available_variations();
    $selectedPrice = '';
    $dump = '';

    foreach ( $available_variations as $variation )
    {
        // $dump = $dump . '<pre>' . var_export($variation['attributes'], true) . '</pre>';

        $isDefVariation=false;
        foreach($product->get_default_attributes() as $key=>$val){
            // $dump = $dump . '<pre>' . var_export($key, true) . '</pre>';
            // $dump = $dump . '<pre>' . var_export($val, true) . '</pre>';
            if($variation['attributes']['attribute_'.$key]==$val){
                $isDefVariation=true;
            }   
        }
        if($isDefVariation){
            $price = $variation['display_price'];         
        }
    }
    $selectedPrice = wc_price($price);

//  $dump = $dump . '<pre>' . var_export($available_variations, true) . '</pre>';

    return $selectedPrice . $dump;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269815a1380.html

最新回复(0)