woocommerce 中每个类别的额外费用
Extra cost for each category in woocommerce
我想知道是否存在一个片段或一个插件,它只是在 woocommerce 类别中添加一个 metabox,以便为该类别的所有产品的价格(不是购物车)增加额外费用。
你能帮帮我吗?
在产品价格上添加固定(静态)额外价格
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。
之后
从类别元动态添加额外价格
步骤:
- 在产品类别添加/编辑中添加额外的价格输入字段
- 保存产品类别额外的价格输入字段
- 可选 - Show/List 类别列表中的额外价格栏(table)
- 最后 - 将类别额外价格添加到类别产品中(分配产品)
代码:
/**
* 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;
}
}
我想知道是否存在一个片段或一个插件,它只是在 woocommerce 类别中添加一个 metabox,以便为该类别的所有产品的价格(不是购物车)增加额外费用。 你能帮帮我吗?
在产品价格上添加固定(静态)额外价格
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。
之后从类别元动态添加额外价格
步骤:
- 在产品类别添加/编辑中添加额外的价格输入字段
- 保存产品类别额外的价格输入字段
- 可选 - Show/List 类别列表中的额外价格栏(table)
- 最后 - 将类别额外价格添加到类别产品中(分配产品)
代码:
/**
* 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;
}
}