samedi 13 mars 2021

WooCommerce additional fee checkbox on archive page

I make a shop page with WooCommerce, where the single product page disabled. On the archive page there's a quantity input field and before that I like to add an additional fee checkbox. This code doesn't work, and I don't know where to modify to add that fee to the cart and checkout. Could you please help me out? Thanks!

Here is my code:

// FRONTEND
function additional_fee() {
    if ( has_term( array( 'test' ), 'product_cat' ) ) {
        echo '<input type="checkbox" name="additional-fee" value="No">Additional Fee';
    } else {
        return;
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'additional_fee' );

// STORE DATA
function store_additional_fee( $cart_item_data, $product_id, $variation_id ) {
    if( isset( $_POST['additional-fee'] ) ) {
        $cart_item_data['additional-fee'] = $_POST['additional-fee'];
    }
        return $cart_item_data;
    }
add_filter( 'woocommerce_add_cart_item_data', 'store_additional_fee', 10, 3 );

// ADD TO CHECKOUT
function add_checkout_additional_fee( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( isset( $cart_item['additional-fee'] ) ) {
        $ndmtransferfee = true;
        $quantity = $cart_item['quantity'];
        break;
        }
    }
    $fee = 5000;
    if ( $ndmtransferfee ) {
        WC()->cart->add_fee( __('Additional Fee', 'textdomain'), $fee * $quantity );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_additional_fee', 10, 1 );



Aucun commentaire:

Enregistrer un commentaire