Wocommerce3の単一ドロップダウン変数製品の変動在庫ステータスを表示する[終了]
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;
}