تابع ووکامرسی wc_get_product_variation_attributes دریافت ویژگی متغیر محصول

تابع ووکامرسی wc_get_product_variation_attributes دریافت ویژگی متغیر محصول

تابع ووکامرسی wc_get_product_variation_attributes – دریافت ویژگی های متغیر محصول به صورت مستقل

Syntax – سینتکس

(array) wc_get_product_variation_attributes( (revisit?) $variation_id ); 

Parameters – پارامتر ها (1)

  • 1- $variation_id (revisit?)

Usage – نحوه استفاده

if ( !function_exists( 'wc_get_product_variation_attributes' ) ) { 
    require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
} 
  
// The variation id. 
$variation_id = null; 
  
// NOTICE! Understand what this does before running. 
$result = wc_get_product_variation_attributes($variation_id); 
    

Defined – محل تعریف

/includes/wc-product-functions.php

function wc_get_product_variation_attributes( $variation_id ) { 
    // Build variation data from meta 
    $all_meta = get_post_meta( $variation_id ); 
    $parent_id = wp_get_post_parent_id( $variation_id ); 
    $parent_attributes = array_filter( (array) get_post_meta( $parent_id, '_product_attributes', true ) ); 
    $found_parent_attributes = array(); 
    $variation_attributes = array(); 
 
    // Compare to parent variable product attributes and ensure they match 
    foreach ( $parent_attributes as $attribute_name => $options ) { 
        if ( ! empty( $options['is_variation'] ) ) { 
            $attribute = 'attribute_' . sanitize_title( $attribute_name ); 
            $found_parent_attributes[] = $attribute; 
            if ( ! array_key_exists( $attribute, $variation_attributes ) ) { 
                $variation_attributes[ $attribute ] = ''; // Add it - 'any' will be asumed 
            } 
        } 
    } 
 
    // Get the variation attributes from meta 
    foreach ( $all_meta as $name => $value ) { 
        // Only look at valid attribute meta, and also compare variation level attributes and remove any which do not exist at parent level 
        if ( 0 !== strpos( $name, 'attribute_' ) || ! in_array( $name, $found_parent_attributes ) ) { 
            unset( $variation_attributes[ $name ] ); 
            continue; 
        } 
        /** 
         * Pre 2.4 handling where 'slugs' were saved instead of the full text attribute. 
         * Attempt to get full version of the text attribute from the parent. 
         */ 
        if ( sanitize_title( $value[0] ) === $value[0] && version_compare( get_post_meta( $parent_id, '_product_version', true ), '2.4.0', '<' ) ) { 
            foreach ( $parent_attributes as $attribute ) { 
                if ( 'attribute_' . sanitize_title( $attribute['name'] ) !== $name ) { 
                    continue; 
                } 
                $text_attributes = wc_get_text_attributes( $attribute['value'] ); 
 
                foreach ( $text_attributes as $text_attribute ) { 
                    if ( sanitize_title( $text_attribute ) === $value[0] ) { 
                        $value[0] = $text_attribute; 
                        break; 
                    } 
                } 
            } 
        } 
 
        $variation_attributes[ $name ] = $value[0]; 
    } 
 
    return $variation_attributes; 
} 

versions – نسخه ها

از نسخه : 2.4.0

نسخه فعلی : 3.0.6

دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2

ارسال نظر

جهت استفاده از کد حتما از تگ pre استفاده نمایید .

contact us