• Resolved oakwoodmedia

    (@oakwoodmedia)


    Hi All,

    I am currently attempting to create a surcharge for samples, my samples are standard products but with a plugin it creates a “Sample” product type when the Request Sample button is used, At the moment I have created a Sample surcharge on the checkout and cart pages when products from a specific category are placed in the cart using the below code:

    function woo_add_cart_fee() {
    
    $category_ID = '1718';
    $category_ID = '6120';
    global $woocommerce;
    
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
        // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
        // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
        foreach ($terms as $term) {
            // 23 is the ID of the category for which we want to remove the payment gateway
            if($term->term_id == $category_ID){
             $excost = 5;
             }
             }
            $woocommerce->cart->add_fee('Samples', $excost, $taxable = false, $tax_class = '');
    }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

    These products I have had to create individually and placed in a specific category but would like to remove these and use my sample button as it is so much simpler.

    Any assistance would be appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Surcharge from product type’ is closed to new replies.