在 WooCommerce 商店页面中显示变体数量
Show number of variations in WooCommerce shop page
我想在 WooCommerce 的商店页面中显示每个产品的变体数量,在产品标题下,我尝试了以下操作:
add_action( 'woocommerce_after_shop_loop_item', 'custom_echo_stock_variations_loop' );
function custom_echo_stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ( $product->get_available_variations() as $key ) {
echo count($key['attributes']) . ' more variations';
}
}
}
他们应该return“还有 6 个变体”和“另外 3 个变体”,但我得到的是 111111 和 111。
如何从 foreach 中显示数组长度?
试试这个
add_action( 'woocommerce_after_shop_loop_item', 'custom_echo_stock_variations_loop' );
function custom_echo_stock_variations_loop() {
global $product;
if ( $product->get_type() == 'variable' ) {
echo '<br/>'.count( $product->get_available_variations() ) . ' more variations';
}
}
此代码在生成器(元素)中有效,但无法再保存页面(致命错误;500)
子主题function.php中的代码
add_shortcode( 'anzahl_varianten', function() {
global $product;
$variations = count($product->get_available_variations());
return '+ '.$variations. ' weitere Varianten';
});
我想在 WooCommerce 的商店页面中显示每个产品的变体数量,在产品标题下,我尝试了以下操作:
add_action( 'woocommerce_after_shop_loop_item', 'custom_echo_stock_variations_loop' );
function custom_echo_stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ( $product->get_available_variations() as $key ) {
echo count($key['attributes']) . ' more variations';
}
}
}
他们应该return“还有 6 个变体”和“另外 3 个变体”,但我得到的是 111111 和 111。
如何从 foreach 中显示数组长度?
试试这个
add_action( 'woocommerce_after_shop_loop_item', 'custom_echo_stock_variations_loop' );
function custom_echo_stock_variations_loop() {
global $product;
if ( $product->get_type() == 'variable' ) {
echo '<br/>'.count( $product->get_available_variations() ) . ' more variations';
}
}
此代码在生成器(元素)中有效,但无法再保存页面(致命错误;500)
子主题function.php中的代码
add_shortcode( 'anzahl_varianten', function() {
global $product;
$variations = count($product->get_available_variations());
return '+ '.$variations. ' weitere Varianten';
});