在 Woocommerce 中显示基于特定运输 类 的产品徽章
Display product badges based for specific shipping classes in Woocommerce
在 Woocommerce 中,我试图显示一个运输标志,它根据定义的运输 classes 进行更新。
我遇到的问题是,当我有一个变量产品时,其变体具有不同的 classes。我需要根据选择更新徽章。
例如,当可变产品有单个和纸箱选项并且单个有 10 class 并且纸箱有散装 1 class 我需要徽章更新当纸箱是从选项中选择。
我不确定如何获得变体运费 class 而不是单一产品运费 class。
这是我的进度:
function rod_shipping_banner() {
if( $product->is_type( 'simple' ) ){
$product = wc_get_product();
$shipping_class = $product->get_shipping_class();
switch ( $shipping_class ) {
case '10':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/10-shipping-small.gif" alt=" Flat Rate"></a></div>';
break;
case 'bulk1':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/Bulky1.gif"alt="Bulky class 1"></a></div>';
break;
case 'bulk2':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="wp-content/uploads/2019/03/Bulky_2.gif"alt="Bulky class 2"></a></div>';
break;
}
} elseif( $product->is_type( 'variable' ) ){
$product = wc_get_product();
$shipping_class = $product->get_shipping_class();
switch ( $shipping_class ) {
case '10':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/10-shipping-small.gif" alt=" Flat Rate"></a></div>';
break;
case 'bulk1':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/Bulky1.gif"alt="Bulky class 1"></a></div>';
break;
case 'bulk2':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="wp-content/uploads/2019/03/Bulky_2.gif"alt="Bulky class 2"></a></div>';
break;
}
}
}
add_action( 'woocommerce_single_product_summary_price',
您的代码中存在一些错误:
- 挂钩
woocommerce_single_product_summary_price
不存在;
- 您需要添加
global $product;
以获得 WC_Product
对象,因为 wc_get_product()
是 null
。
- 简单和可变产品之间没有区别,以获得产品运输class。
- 对于徽章,使用
<span class="onsale"> … </span>
而不是 Woocommerce 通知使用的 <div class="woocommerce-info"> … </div>
…
- 您可以将 2 个函数合并为一个,因为它们使用相同的钩子和优先级
另外你的问题不是很容易理解和清楚。
以下重新访问的代码,将为特定定义的运输 classes 和产品标签以及简单产品而不是可变产品显示有条件的自定义徽章:
add_action( 'woocommerce_before_single_product_summary', 'additional_single_product_badges', 10 );
function additional_single_product_badges() {
global $product;
## ---- SHIPPING CLASSES ---- ##
// Define the related shipping classes data in this multidimensional array
$shipping_class_badges_data = [
'10' => ['2019/03/10-shipping-small.gif' => ' Flat Rate'],
'bulk1' => ['2019/03/Bulky1.gif' => 'Bulky class 1'],
'bulk2' => ['2019/03/Bulky2.gif' => 'Bulky class 2'],
];
if( $shipping_class = $product->get_shipping_class() ) {
foreach( $shipping_class_badges_data as $key => $badge ) {
if ( $shipping_class == $key ) {
foreach( $badge as $img_src => $alt_text ) {
$html = '<a href="'.home_url("/shipping-policy/").'" "target="_blank">';
$html .= '<img src="'.home_url("/wp-content/uploads/".$img_src).'" alt="'.$alt_text.'" />';
$html .= '</a>';
}
echo ' <span class="onsale shipping-badge">'.$html.'</span>';
break;
}
}
}
## ---------- TAGS ---------- ##
// Define the related product tags data in this multidimensional array
$product_tags_badges_data = [
'amarock' => ['2019/03/Asset-4.png' => 'Tag (test)'],
];
// Get the product tag(s) set in the current product
$product_tags_slugs = wp_get_post_terms( $product->get_id(), 'product_tag', ['fields' => 'slugs'] );
if( sizeof($product_tags_slugs) > 0 ) {
foreach( $product_tags_badges_data as $key => $badge ) {
if ( in_array($key, $product_tags_slugs) ) {
foreach( $badge as $img_src => $alt_text ) {
$html = '<img src="'.home_url("/wp-content/uploads/".$img_src).'" alt="'.$alt_text.'" />';;
}
echo ' <span class="onsale tag-badge">'.$html.'</span>';
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
查看此可变产品屏幕截图的实际效果:
与您的评论相关,对于产品变体:
To make it work for the product variations:
You can set a default shipping class in the parent variable product.
If each product variation has a different shipping class, this will not work.
It requires something different and quite complicated with javascript, as product variations are selected/changed on client side (live event), so PHP can't handle alone that.
在 Woocommerce 中,我试图显示一个运输标志,它根据定义的运输 classes 进行更新。
我遇到的问题是,当我有一个变量产品时,其变体具有不同的 classes。我需要根据选择更新徽章。
例如,当可变产品有单个和纸箱选项并且单个有 10 class 并且纸箱有散装 1 class 我需要徽章更新当纸箱是从选项中选择。
我不确定如何获得变体运费 class 而不是单一产品运费 class。
这是我的进度:
function rod_shipping_banner() {
if( $product->is_type( 'simple' ) ){
$product = wc_get_product();
$shipping_class = $product->get_shipping_class();
switch ( $shipping_class ) {
case '10':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/10-shipping-small.gif" alt=" Flat Rate"></a></div>';
break;
case 'bulk1':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/Bulky1.gif"alt="Bulky class 1"></a></div>';
break;
case 'bulk2':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="wp-content/uploads/2019/03/Bulky_2.gif"alt="Bulky class 2"></a></div>';
break;
}
} elseif( $product->is_type( 'variable' ) ){
$product = wc_get_product();
$shipping_class = $product->get_shipping_class();
switch ( $shipping_class ) {
case '10':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/10-shipping-small.gif" alt=" Flat Rate"></a></div>';
break;
case 'bulk1':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="/wp-content/uploads/2019/03/Bulky1.gif"alt="Bulky class 1"></a></div>';
break;
case 'bulk2':
echo '<div class="woocommerce-info"><a href="/shipping-policy/"target="_blank"><img src="wp-content/uploads/2019/03/Bulky_2.gif"alt="Bulky class 2"></a></div>';
break;
}
}
}
add_action( 'woocommerce_single_product_summary_price',
您的代码中存在一些错误:
- 挂钩
woocommerce_single_product_summary_price
不存在; - 您需要添加
global $product;
以获得WC_Product
对象,因为wc_get_product()
是null
。 - 简单和可变产品之间没有区别,以获得产品运输class。
- 对于徽章,使用
<span class="onsale"> … </span>
而不是 Woocommerce 通知使用的<div class="woocommerce-info"> … </div>
… - 您可以将 2 个函数合并为一个,因为它们使用相同的钩子和优先级
另外你的问题不是很容易理解和清楚。
以下重新访问的代码,将为特定定义的运输 classes 和产品标签以及简单产品而不是可变产品显示有条件的自定义徽章:
add_action( 'woocommerce_before_single_product_summary', 'additional_single_product_badges', 10 );
function additional_single_product_badges() {
global $product;
## ---- SHIPPING CLASSES ---- ##
// Define the related shipping classes data in this multidimensional array
$shipping_class_badges_data = [
'10' => ['2019/03/10-shipping-small.gif' => ' Flat Rate'],
'bulk1' => ['2019/03/Bulky1.gif' => 'Bulky class 1'],
'bulk2' => ['2019/03/Bulky2.gif' => 'Bulky class 2'],
];
if( $shipping_class = $product->get_shipping_class() ) {
foreach( $shipping_class_badges_data as $key => $badge ) {
if ( $shipping_class == $key ) {
foreach( $badge as $img_src => $alt_text ) {
$html = '<a href="'.home_url("/shipping-policy/").'" "target="_blank">';
$html .= '<img src="'.home_url("/wp-content/uploads/".$img_src).'" alt="'.$alt_text.'" />';
$html .= '</a>';
}
echo ' <span class="onsale shipping-badge">'.$html.'</span>';
break;
}
}
}
## ---------- TAGS ---------- ##
// Define the related product tags data in this multidimensional array
$product_tags_badges_data = [
'amarock' => ['2019/03/Asset-4.png' => 'Tag (test)'],
];
// Get the product tag(s) set in the current product
$product_tags_slugs = wp_get_post_terms( $product->get_id(), 'product_tag', ['fields' => 'slugs'] );
if( sizeof($product_tags_slugs) > 0 ) {
foreach( $product_tags_badges_data as $key => $badge ) {
if ( in_array($key, $product_tags_slugs) ) {
foreach( $badge as $img_src => $alt_text ) {
$html = '<img src="'.home_url("/wp-content/uploads/".$img_src).'" alt="'.$alt_text.'" />';;
}
echo ' <span class="onsale tag-badge">'.$html.'</span>';
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
查看此可变产品屏幕截图的实际效果:
与您的评论相关,对于产品变体:
To make it work for the product variations:
You can set a default shipping class in the parent variable product.If each product variation has a different shipping class, this will not work.
It requires something different and quite complicated with javascript, as product variations are selected/changed on client side (live event), so PHP can't handle alone that.