I’m trying to create a plugin that let the admin to determine the price of gram gold,
and then in each product I add the weight to each product and it’s calc the price
the problem is when I have product with variation I see only the range of the price but not see the option to choose the variation
<?php
/*
Plugin Name: Gold Price Adjuster
Description: Adjusts product prices based on gold price per gram.
Version: 2.0.0
Author: David Amagid
*/
// Create the settings page
function gpa_create_settings_page() {
add_menu_page(
'Gold Price Settings',
'Gold Price',
'manage_options',
'gold-price-settings',
'gpa_render_settings_page',
'dashicons-admin-generic',
100
);
}
add_action('admin_menu', 'gpa_create_settings_page');
function gpa_render_settings_page() {
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('gpa_options_group');
do_settings_sections('gold-price-settings');
submit_button();
?>
</form>
</div>
<?php
}
function gpa_register_settings() {
register_setting('gpa_options_group', 'gpa_gold_price');
add_settings_section('gpa_main_section', 'Gold Price Settings', null, 'gold-price-settings');
add_settings_field('gpa_gold_price_field', 'Gold Price Per Gram', 'gpa_gold_price_field_callback', 'gold-price-settings', 'gpa_main_section');
}
add_action('admin_init', 'gpa_register_settings');
function gpa_gold_price_field_callback() {
$gold_price = get_option('gpa_gold_price', '');
echo '<input type="text" id="gpa_gold_price" name="gpa_gold_price" value="' . esc_attr($gold_price) . '" />';
}
// Adjust the product price for simple products
function gpa_adjust_simple_product_price($price, $product) {
if ($product->is_type('simple')) {
$gold_price = get_option('gpa_gold_price', 0);
$weight = (float) $product->get_weight();
if ($gold_price > 0 && $weight > 0) {
$price = floatval($price) + ($weight * $gold_price);
}
}
return $price;
}
// Adjust the variation prices once per variation
function gpa_adjust_variation_price($price, $variation) {
if ($variation->is_type('variation')) {
static $adjusted_prices = array();
$variation_id = $variation->get_id();
if (isset($adjusted_prices[$variation_id])) {
return $adjusted_prices[$variation_id];
}
$gold_price = get_option('gpa_gold_price', 0);
$variation_weight = (float) $variation->get_weight();
if ($gold_price > 0 && $variation_weight > 0) {
$price = floatval($price) + ($variation_weight * $gold_price);
$adjusted_prices[$variation_id] = $price;
}
}
return $price;
}
// Ensure the price range shown for variable products includes gold adjustment
function gpa_adjust_variable_price_range($price_html, $product) {
if ($product->is_type('variable')) {
$prices = $product->get_variation_prices(true);
$min_price = min($prices['price']);
$max_price = max($prices['price']);
$min_price = gpa_adjust_variation_price($min_price, wc_get_product(array_search($min_price, $prices['price'])));
$max_price = gpa_adjust_variation_price($max_price, wc_get_product(array_search($max_price, $prices['price'])));
$price_html = wc_price($min_price) . ' - ' . wc_price($max_price);
}
return $price_html;
}
// Hook into WooCommerce price retrieval
add_filter('woocommerce_product_get_price', 'gpa_adjust_simple_product_price', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'gpa_adjust_variation_price', 10, 2);
add_filter('woocommerce_product_get_regular_price', 'gpa_adjust_simple_product_price', 10, 2);
add_filter('woocommerce_product_variation_get_regular_price', 'gpa_adjust_variation_price', 10, 2);
// Adjust the variable product price range
add_filter('woocommerce_variable_price_html', 'gpa_adjust_variable_price_range', 10, 2);
?>
when i try to change somthing I get another error