• I am currently developing custom functionalities in WooCommerce to create a custom product type called “Tour.” This tour product has custom fields created with ACF, and the price of the tour adjusts based on specific periods according to values entered in these fields. This functionality works correctly, and the price updates as expected on the frontend.

    However, I encountered an issue when activating your plugin. The percentage-based pricing values entered in the custom fields are not being applied as expected.

    Here is the code I am using:

    <?php

    // Update cart item price based on custom price
    add_action('woocommerce_before_calculate_totals', 'update_cart_item_price_based_on_tour_date', 10, 1);

    function update_cart_item_price_based_on_tour_date($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
    return;
    }

    // Get WooCommerce decimal settings
    $decimal_places = get_option('woocommerce_price_num_decimals', 2);
    $decimal_separator = get_option('woocommerce_price_decimal_sep', '.');
    $thousand_separator = get_option('woocommerce_price_thousand_sep', ',');

    foreach ($cart->get_cart() as $cart_item) {
    if (isset($cart_item['tour_date'])) {
    $product_id = $cart_item['product_id'];
    $tour_date = $cart_item['tour_date'];
    $tour_price = Izdone_Tour_Fields_Handler::get_tour_price_by_date($product_id, $tour_date);

    if ($tour_price !== false) {
    $base_price = get_post_meta($product_id, '_regular_price', true);

    // Convert base price to float
    $base_price = floatval(str_replace([$thousand_separator, $decimal_separator], ['', '.'], $base_price));

    // Calculate new price
    $new_price = $base_price * (1 + ($tour_price / 100));

    // Format new price
    $new_price = number_format($new_price, $decimal_places, $decimal_separator, $thousand_separator);

    // Set new price
    $cart_item['data']->set_price($new_price);
    }
    }
    }
    }

    // Handle AJAX request to get tour price by date
    add_action('wp_ajax_get_tour_price', 'get_tour_price');
    add_action('wp_ajax_nopriv_get_tour_price', 'get_tour_price');

    function get_tour_price() {
    $product_id = intval($_POST['product_id']);
    $tour_date = sanitize_text_field($_POST['tour_date']);
    $tour_price = Izdone_Tour_Fields_Handler::get_tour_price_by_date($product_id, $tour_date);

    if ($tour_price !== false) {
    wp_send_json(array('success' => true, 'price' => $tour_price));
    } else {
    wp_send_json(array('success' => false, 'price' => 0));
    }

    wp_die();
    }

    Could you please assist me in resolving this issue? It appears that the custom pricing functionality is being affected by the activation of your plugin. I would appreciate any guidance or suggestions on how to ensure that the custom pricing works correctly alongside your plugin.

    Thank you for your assistance.

    Best regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Wombat Plugins

    (@maartenbelmans)

    Hi @readonecc

    Our plugin only works with the standard WooCommerce product types “simple” and “variable”. Using them on a custom type is not recommended. Perhaps you can create a simple product that resembles a tour.

    Thread Starter Mohammad Ridwanullah

    (@readonecc)

    Thank you for your response. However, the issue is not related to using custom product types. The problem arises specifically when your plugin is activated; my custom functionality for dynamic pricing stops working as expected.

    Could you please help identify which part of my custom functionality might be conflicting with your plugin? If possible, could you provide guidance on how to troubleshoot or resolve this conflict?

    I appreciate your assistance in resolving this issue.

    Plugin Author Wombat Plugins

    (@maartenbelmans)

    Hi @readonecc

    The issue *is* with the fact that your custom product has fields attached to it but these fields are not meant for that product type. We can not guarantee this will work in the future so I highly recommend not doing this.

    But if you want to go ahead at your own risk:

    Our plugin also hooks into woocommerce_before_calculate_totals so perhaps you should increase the priority on your code snippet from 10 to 20 to make sure it is the last in the sequence to run.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue with Custom Pricing Functionality When Using Your Plugin’ is closed to new replies.