• Resolved maidot

    (@maidot)


    I’ve searched online to understand if that is possible or not, but couldn’t find anything mentioning that.

    In my code, I have this:

    add_filter('woocommerce_add_cart_item', [$this, 'add_cart_item'], 20, 1);
    public function add_cart_item($cart_item)
        {
            $product_id   = $cart_item['data']->get_id();
            $product_type = wc_get_product($product_id)->get_type();
            $insurance = $cart_item['rental_data']['rental_days_and_costs']['insurance'];
    
            if (isset($cart_item['rental_data']['quote_id']) && !empty($cart_item['rental_data']['quote_id']) && $product_type === 'rental') {
                $cart_item['data']->set_price($cart_item['rental_data']['rental_days_and_costs']['cost']);
            } else {
                if (isset($cart_item['rental_data']['rental_days_and_costs']['cost']) && $product_type === 'rental') {
                    $cart_item['data']->set_price($cart_item['rental_data']['rental_days_and_costs']['cost']);
                }
    
                // revert
                if (isset($cart_item['quantity']) && $product_type === 'rental') {
                    $cart_item['quantity'] = isset($cart_item['rental_data']['quantity']) ? $cart_item['rental_data']['quantity'] : 1;
                }
            }
    
            return $cart_item;
        }

    The “$insurance” variable is the one I want to add to “product data” as a fee. Can I do that with this filter?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @maidot

    I understand that you want to add a fee programmatically, using the woocommerce_add_cart_item filter.

    As this is a development related topic, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out here.

    If you’re not receiving input on this thread, I can recommend reaching out to one of the customization experts listed here: https://woocommerce.com/customizations/.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    This specific forum is more focused on the default WooCommerce core features.

    Thread Starter maidot

    (@maidot)

    I solved it, probably not the best way, but it works.

    So the idea here was to add an “insurance” value that comes from a CPT where a user requests a quote for a car renting.

    So I used this hook:

    add_action('woocommerce_cart_calculate_fees', 'order_item_fee', 20, 3);

    Then, I created this basic function:

    public function order_item_fee()
    {
       global $woocommerce;
       $woocommerce->cart->add_fee( 'Seguro', $GLOBALS['global_insurance'], false, '' );
    }

    And so, inside the function I had for the hook I mentioned above, I added this to send the value inside this new function:

    $GLOBALS['global_insurance'] = $cart_item['rental_data']['rental_days_and_costs']['insurance'];

    And that’s it! Maybe not the best approach, but it works and that’s what matters. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add fee using woocommerce_add_cart_item filter’ is closed to new replies.