在产品页面(可变产品)Woocommerce 上显示运费 class
Show shipping class on product page (variable products) Woocommerce
我很难在 Woocommerce 的产品页面上显示不同的运费 classes。我发现了几个显示简单产品运输 class 的代码。但不是显示可变产品不同变体的 classes 的代码。
代码应该显示简单产品的运费 class(因为我给出了几个简单产品)。但是当产品是可变产品时,它需要显示变体的运费 classes 而不是父运费 class.
我没有能力编写这个,希望有足够熟练并愿意提供帮助的人。
前端要是像这样就好了:
Front end shipping class
一种方法是使用变体库存过滤器 woocommerce_get_stock_html()
,它有 2 个参数:
$html
用于显示内容
$product
产品对象
您需要为每个变体设置运费 class 并将其添加到您的 functions.php
:
/**
* Add shipping class for Product variations.
*
* @param mixed $html Displays stock html content.
* @param WC_Product $product Product Object.
*
* @return mixed $html Updated stock html content.
*/
add_filter( 'woocommerce_get_stock_html', 'amp_woocommerce_get_stock_html', 10, 2 );
function amp_woocommerce_get_stock_html( $html, $product ) {
$shipping_slug = $product->get_shipping_class();
if ( ! empty( $shipping_slug ) ) {
$shipping_name = get_term_by( 'slug', $shipping_slug, 'product_shipping_class' );
$html .= sprintf( '<p class="shipping-class"><strong>%s</strong> %s</p>', __( 'Shipping:', 'text-domain' ), $shipping_name );
}
return $html;
}
我很难在 Woocommerce 的产品页面上显示不同的运费 classes。我发现了几个显示简单产品运输 class 的代码。但不是显示可变产品不同变体的 classes 的代码。
代码应该显示简单产品的运费 class(因为我给出了几个简单产品)。但是当产品是可变产品时,它需要显示变体的运费 classes 而不是父运费 class.
我没有能力编写这个,希望有足够熟练并愿意提供帮助的人。
前端要是像这样就好了: Front end shipping class
一种方法是使用变体库存过滤器 woocommerce_get_stock_html()
,它有 2 个参数:
$html
用于显示内容$product
产品对象
您需要为每个变体设置运费 class 并将其添加到您的 functions.php
:
/**
* Add shipping class for Product variations.
*
* @param mixed $html Displays stock html content.
* @param WC_Product $product Product Object.
*
* @return mixed $html Updated stock html content.
*/
add_filter( 'woocommerce_get_stock_html', 'amp_woocommerce_get_stock_html', 10, 2 );
function amp_woocommerce_get_stock_html( $html, $product ) {
$shipping_slug = $product->get_shipping_class();
if ( ! empty( $shipping_slug ) ) {
$shipping_name = get_term_by( 'slug', $shipping_slug, 'product_shipping_class' );
$html .= sprintf( '<p class="shipping-class"><strong>%s</strong> %s</p>', __( 'Shipping:', 'text-domain' ), $shipping_name );
}
return $html;
}