woocommerce 中每个类别的额外费用
Posted
技术标签:
【中文标题】woocommerce 中每个类别的额外费用【英文标题】:Extra cost for each category in woocommerce 【发布时间】:2021-12-05 09:05:24 【问题描述】:我想知道是否存在一个 sn-p 或者一个插件,它只是在 woocommerce 类别中添加一个元框,以增加该类别所有产品的价格(而不是购物车)的额外成本。 你能帮帮我吗?
【问题讨论】:
【参考方案1】:从类别元动态添加额外价格
步骤:
-
在添加/编辑产品类别时添加额外的价格输入字段
保存产品类别额外价格输入字段
可选 - 在类别列表(表格)上显示/列出额外价格列
最后 - 将类别额外价格添加到类别产品中(分配产品)
代码:
/**
* Step 1: Create an extra price column on add/edit/list categories page
*
*/
/* =========1(a). Product Create Category page ============== */
function woo_taxonomy_add_extra_price_meta_field()
?>
<div class="form-field">
<label for="extra_price"><?php __('Extra Product Price', 'woocommerece'); ?></label>
<input type="number" name="extra_price" id="extra_price" maxlength="4" value="0" />
<p class="description"><?php _e('Add extra cost to the product price', 'woocommerece'); ?></p>
</div>
<?php
/* =========1(b). Product Edit Category page ============== */
function woo_taxonomy_edit_extra_price_meta_field($term)
$term_id = $term->term_id;
$extra_price = get_term_meta($term_id, 'extra_price', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra_price"><?php __('Extra Product Price', 'woocommerece'); ?></label></th>
<td>
<input type="number" name="extra_price" id="extra_price" value="<?php echo esc_attr($extra_price) ? esc_attr($extra_price) : 0; ?>" maxlength="4" />
<p class="description"><?php _e('Add extra cost to the product price', 'woocommerece'); ?></p>
</td>
</tr>
<?php
add_action('product_cat_add_form_fields', 'woo_taxonomy_add_extra_price_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'woo_taxonomy_edit_extra_price_meta_field', 10, 1);
/* ======== 2. Save extra taxonomy fields callback function. ========= */
function woo_save_taxonomy_extra_price_meta($term_id)
$extra_price = filter_input(INPUT_POST, 'extra_price');
update_term_meta($term_id, 'extra_price', $extra_price);
add_action('edited_product_cat', 'woo_save_taxonomy_extra_price_meta', 10, 1);
add_action('create_product_cat', 'woo_save_taxonomy_extra_price_meta', 10, 1);
/* ========= 3(optional). Displaying Additional Columns on admin screen(category grid) ============== */
add_filter( 'manage_edit-product_cat_columns', 'woo_FieldsListExtraPrice' ); //Register Function
add_action( 'manage_product_cat_custom_column', 'woo_FieldsListExtraPriceDisplay' , 10, 3); //Populating the Columns
/* ========= Extra Price column added to category admin screen. ============== */
function woo_FieldsListExtraPrice( $columns )
$columns['extra_price'] = __( 'Extra Product Price', 'woocommerce' );
return $columns;
/* ========= Extra Price column value added to product category admin screen. ============== */
function woo_FieldsListExtraPriceDisplay( $columns, $column, $id )
if ( 'extra_price' == $column )
$columns = esc_html( get_term_meta($id, 'extra_price', true) );
return $columns;
/**
* Step 4: Add Extra Price in products
*
* @return numaric
*/
add_filter('woocommerce_get_price', 'woocommerce_change_price_by_addition', 10, 2);
function woocommerce_change_price_by_addition($price, $product)
// Product ID
$product_id = isset($product->id) ? $product->id : 0;
$product_categories_id = isset($product->category_ids) ? $product->category_ids : array();
$extra_amount = 0;
if(!empty($product_categories_id))
foreach($product_categories_id as $cat_id)
$category_extra_price = (float)get_term_meta($cat_id, 'extra_price', true);
if ($category_extra_price && is_numeric($category_extra_price))
$extra_amount = $extra_amount + $category_extra_price;
if ($product_id)
//get the product
$product = wc_get_product($product_id);
// change the price by adding the 35
$price = ($price + $extra_amount);
//return the new price
return $price;
【讨论】:
【参考方案2】:在产品价格上添加固定(静态)额外价格
add_filter('woocommerce_get_price', 'woocommerce_change_price_by_addition', 10, 2);
function woocommerce_change_price_by_addition($price, $product)
// Product ID
$product_id = isset($product->id) ? $product->id : 0;
// Extra Amount
$extra_amount = 35;
if ($product_id)
//get the product
$product = wc_get_product($product_id);
// change the price by adding the 35
$price = ($price + $extra_amount);
//return the new price
return $price;
1.之前
2。之后
【讨论】:
谢谢,但这样一来,所有类别的额外内容都是一样的。我的意思是一个元框,以便为每个类别指定正确的额外内容。 好的,我会尽快更新答案以上是关于woocommerce 中每个类别的额外费用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 woo commerce 的结帐表单字段中删除额外的 div html 类