Wocommerce 3의 단일 드롭 다운 가변 제품에 대한 변형 재고 상태 표시 [종료 됨]

Nov 25 2020

WooCommerce 가변 제품 응답 코드의 각 속성 값 옆에 재고 상태 표시를 사용하여 가변 제품 페이지의 단일 제품 속성 드롭 다운에 변형 재고 상태를 표시하고 있습니다.

이것은 잘 작동하지만 제품을로드하는 데 너무 많은 시간이 걸립니다.

로드 속도를 높이기 위해 코드를 어떻게 최적화 할 수 있습니까?

답변

1 LoicTheAztec Nov 25 2020 at 15:37

대신 다음을 사용하면 약간 더 가벼워집니다 (따라서 가변 제품은 빠르게로드됩니다) .

add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name' );
function customizing_variations_terms_name( $term_name ){ global $product;

    if( is_admin() ) return $term_name; // Only on frontend single products // Iterating through each visible product variation Ids foreach( $product->get_visible_children() as $variation_id ){ $variation = new WC_Product_Variation( $variation_id ); $stock_status = $variation->get_stock_status(); $stock_qty    = $variation->get_stock_quantity(); // The attributes taxonomy key and slug value for this variation $attributes = $variation->get_attributes(); // Caution: Works only for 1 attribute set in the product if(count($attributes) == 1 ) {
            $attributes_keys = array_keys($attributes);
            $attr_taxonomy = str_replace('attribute_', '', reset($attributes_keys) );
            if( $variation->get_attribute( $attr_taxonomy ) === $term_name ) { break; // stop the loop } } $term_name .= ' - ' . $stock_status; $term_name  = $stock_qty > 0 ? $term_name . ' ('.$stock_qty.')' : $term_name;
    }
    return $term_name;
}