php - 在 Woocommerce 上处理产品类型
问题描述
如果我尝试以这种方式获取产品类型,我真的很困惑如何在 Woocommerce 中正确获取产品类型:
$product->get_type();
simple
即使产品是可变的,它也总是返回,然后我尝试了以下方法:
$terms = get_the_terms($this->product->get_id(), 'product_type');
$product_type = (!empty($terms)) ? sanitize_title(current($terms)->name) : 'simple';
从上面的代码可以看出,它获取product_type
产品的分类,如果存在则返回分类名称,否则返回simple
.
但是,simple
即使产品是可变的,它仍然会返回,我现在不知道该怎么做,因为我从 woocommerce 挂钩的参数中获取产品对象我不知道它是什么类型的产品,我需要制作这个验证。
如何在woocommerce中正确获取产品类型?
更新
根据要求,我将添加我正在使用的完整代码,这样您就可以更好地了解我想要实现的目标。
基本上,我想在订单行项目管理编辑屏幕中将元字段显示为一列。
add_action( 'woocommerce_admin_order_item_values', 'ebani_add_cost_price_column_order_value_admin_edit', 10, 3);
function ebani_add_cost_price_column_order_value_admin_edit( $product, $item, $item_id ) {
$order = $item->get_order();
?>
<td class="item_cost_price" width="1%" data-sort-value="<?php echo esc_attr( $order->get_item_subtotal( $item, false, true ) ); ?>">
<div class="view">
<?php
if( ! is_null( $product ) ){
//The right way to get the product type
$terms = get_the_terms( $product->get_id(), 'product_type' );
$product_type = ( !empty( $terms ) ) ? sanitize_title( current( $terms )->name ) : 'simple';
echo $product_type;
if( $product_type == 'variable' ) {
$variation_id = $item->get_variation_id();
echo wc_price( get_post_meta( $variation_id, 'variation_cost', true ), array( 'currency' => $order->get_currency() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}elseif( $product_type == 'simple' ) {
echo wc_price( get_post_meta( $item->get_product_id(), 'cost_price', true ), array( 'currency' => $order->get_currency() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}else{
echo "No aplica";
}
} else {
echo 'No aplica';
}
?>
</div>
</td>
<?php
}
解决方案
我不太明白get_the_terms
. get_type()
应该足够了。
// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="line_mytitle sortable" data-sort="your-sort-option">My Title</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );
//Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
$order = $item->get_order();
?>
<td class="item_cost_price" width="1%" data-sort-value="<?php echo esc_attr( $order->get_item_subtotal( $item, false, true ) ); ?>">
<div class="view">
<?php
if( ! is_null( $product ) ) {
// Get product type
$product_type = $product->get_type();
echo 'product type = ' . $product_type . '<br>';
if( $product_type == 'variation' ) {
$variation_id = $item->get_variation_id();
echo wc_price( get_post_meta( $variation_id, 'variation_cost', true ), array( 'currency' => $order->get_currency() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} elseif( $product_type == 'simple' ) {
echo wc_price( get_post_meta( $item->get_product_id(), 'cost_price', true ), array( 'currency' => $order->get_currency() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
echo "No aplica";
}
} else {
echo 'No aplica';
}
?>
</div>
</td>
<?php
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
推荐阅读
- sql - 连接两个间接相关的表,而不包括中间表中的列
- c - mongoose 网络服务器是否需要外部库来实现 SSL HTTPS?
- amazon-ec2 - DynamoDB batchWrite 调用的 EC2 主机类型
- r - 使用子集在 dplyr 中进行条件左连接
- python - 与 python websockets 包中的生产者协程循环共享数据的最佳方式?
- vba - 在 MAC Word 用户表单中插入图像
- python - 如何在 matplotlib 中为数据框中的多个组添加误差线?
- python - 电子表格 API 超时
- java - stripe.confirmCardPayment 和 java
- stm32 - 挂断 UART 和 DMA