Home > database >  Extra cost for each category in woocommerce
Extra cost for each category in woocommerce

Time:10-19

I wonder if exists a snippet or maybe a plugin that just adds a metabox in woocommerce categories to add an extra cost to the price (not at cart) for all the products of that category. Can you help me?

CodePudding user response:

Add Extra Price dynamically from categories meta

Steps:

  1. Add extra price input fields on product category add / edit
  2. Save product category extra price input fields
  3. Optional - Show/List extra price column on the category listing(table)
  4. Finally - Add category extra price into categories products( assign product)

Code:

/**
 * 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;
    }
}

enter image description here

CodePudding user response:

Add Fixed(static) Extra Price on the product price

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. Before enter image description here

2. After

enter image description here

  • Related