0

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?

2 Answers 2

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

-1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.