وردپرس

Issue with Custom Pricing Functionality When Using Your Plugin


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:

// 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

این خبر را در ایران وب سازان مرجع وب و فناوری دنبال کنید

مشاهده پاسخ های این مطلب
———————————————
این مطلب از سایت انجمن وردپرس گردآوری شده است و کلیه حقوق مطلق به انجمن وردپرس می باشد در صورت مغایرت و یا بروز مشکل اطلاع دهید تا حذف گردد

منبع: انجمن وردپرس

دکمه بازگشت به بالا