• Resolved szakku

    (@szakku)


    Hi!
    It’s possible to add specific amount for product by ID?
    Like, if customer have 3 different products in checkout:

    No. 1 = 4$
    No. 2 = 5$
    No. 3 = 6$

    Regards!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter szakku

    (@szakku)

    my friend helped me to make something like that:

    add_filter('pre_option_tgpc_gift_wrapper_cost', function($cost) { if (is_product()) { $product_fees = [ 5 => [111], 10 => [112, 113], 15 => [114, 115], ]; $product_id = get_the_ID(); foreach ($product_fees as $fee => $ids) { if (in_array($product_id, $ids)) { return $fee; } } } return $cost; });

    But, it’s not working, and he said I need to contact with you.

    • This reply was modified 4 months, 2 weeks ago by szakku.
    Plugin Author Pexle Chris

    (@pexlechris)

    Hello,

    You need something like the code below:

    add_filter('pre_option_tgpc_gift_wrapper_cost', function($cost) {

    // if (is_product()) { // This if is wrong

    $product_fees = [
    5 => [111],
    10 => [112, 113],
    15 => [114, 115],
    ];

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product_id = get_the_ID(); // this is WRONG
    $product_id = $cart_item['product_id'];

    foreach ($product_fees as $fee => $ids) {
    if (in_array($product_id, $ids)) {
    return $fee;
    }
    }

    }

    return $cost;
    });

    But the code is not tested, so you need to hire a developer to fix the code according to your needs or to ask chatgpt if is not worked.

    But have in mind that, if the cart contains 112 and 115 products (with these ids) for example we don’t know exactly the result of this hook

    Plugin Author Theo Gkitsos

    (@theogk)

    Hi all, I’m going to mark this topic as resolved.

    One way to achieve what you want is using @pexlechris code that corrected the mistakes of your original code.

    Another way is to add (with custom code) a custom field in the product edit page, and insert the cost for each specific product. Then you can loop the products in the cart, using the code above foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){.....} and calculate the total cost based on each product’s custom field.

    So, there are ways to do this, but you have to hire a PHP developer to write some lines of code for you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.