我在一个Woocommerce插件工作,我试图使产品页面上的自定义,但值是不是在页面上得到改变,当我做添加到购物车它需要两个值,即产品的价值已经在那里和定制后的产品价格。由于添加到购物车工程完美,但我只面临的问题,而添加custome价格后,产品定制。
add_action('woocommerce_before_add_to_cart_button', 'add_to_cart_product_pricing',);
function add_to_cart_product_pricing()
{
global $woocommerce, $product, $wpdb;
if (is_a($woocommerce, 'WooCommerce') && is_product()) {
// Targeting "ring" or "rings" product category
if (has_term(array('ring', 'rings'), 'product_cat')) {
// Load the latest gold rate
$gold_rate_table = $wpdb->prefix . 'gold_rate';
$gold_rate = $wpdb->get_var("SELECT final_price FROM $gold_rate_table ORDER BY id DESC LIMIT 1");
//Get Metal Purity
$metal_purity = $product->get_attribute('metal-purity');
$metal_purity_table = $wpdb->prefix . 'diamond_purity';
// Prepare the SQL query with proper escaping
$query = $wpdb->prepare("SELECT percentage FROM $metal_purity_table WHERE purity = %s", $metal_purity);
// Run the prepared query
$metalpurity = $wpdb->get_var($query);
$goldvalue = ($gold_rate * $metalpurity) / 100;
// Diamond Shape
$dquality = $product->get_attribute('diamond-quality');
$sieves = $product->get_attribute('sieves');
$dweight = $product->get_attribute('diamond-weight-ct');
$dpieces = $product->get_attribute('diamond-pieces');
$round_diamond_table = $wpdb->prefix . 'round_diamond';
// Prepare the SQL query with proper escaping
$query = $wpdb->prepare("SELECT $sieves FROM $round_diamond_table WHERE diamond_quality = %s", $dquality);
// Run the prepared query
$dshape = $wpdb->get_var($query);
$dtotal = ($dshape * ($dweight / 5));
$dmtotal = $dtotal * $dpieces;
// Get net weight product attribute
$net_weight = $product->get_attribute('net-weight-g');
// Get product regular price
$regular_price = $product->get_regular_price();
// Calculate product updated price
$updated_price = ($goldvalue * $net_weight) + $regular_price + $dmtotal;
// Get the displayed price
$args = array('price' => floatval($updated_price));
if ('incl' === get_option('woocommerce_tax_display_shop')) {
$displayed_price = wc_get_price_including_tax($product, $args);
} else {
$displayed_price = wc_get_price_excluding_tax($product, $args);
}
// Display product updated price
printf('<p class="productprice">%s</p>', wc_price($displayed_price));
// Display a hidden input field with the "updated_price" as value
printf('<input type="hidden" name="updated_price" value="%s" />', $updated_price);
// Get gross weight product attribute
$gross_weight = $product->get_attribute('gross-weight');
// Display the Gross Weight
printf('<p class="grossweight">' . __('Weight: %s g') . '</p>', $gross_weight);
// Store the updated price in a global variable
global $updated_price;
}
}
}
add_action('woocommerce_single_product_summary', 'customization_of_product', 32);
function customization_of_product()
{
global $product, $wpdb;
if (has_term(array('ring', 'rings'), 'product_cat')) {
echo '<div class="customization-form">
<form id="customization-form" method="POST">
<label for="ring-size">Ring Size:</label>';
// Ring Size
$ring_size_table = $wpdb->prefix . 'ring_size';
$results = $wpdb->get_results("SELECT * FROM $ring_size_table");
echo '<select name="ring-size" id="ring-size">
<option value="">Ring Sizes</option>';
foreach ($results as $result) {
echo '<option value="' . $result->ring_size . '">' . $result->ring_size . '</option>';
}
echo '</select><br> <label for="metal-purity">Metal Purity:</label>';
// Metal Purity
$table_name = $wpdb->prefix . 'diamond_purity';
$results = $wpdb->get_results("SELECT * FROM $table_name WHERE purity='14kt' OR purity='18kt'");
echo '<select name="metal-purity" id="metal-purity">
<option value="">Metal Purity</option>';
foreach ($results as $result) {
echo '<option value="' . $result->purity . '">' . $result->purity . '</option>';
}
echo '</select>';
echo '<input type="submit" name="submit" value="Submit">
</form>
</div>';
if (isset($_POST['submit'])) {
// Get the submitted values
$ring_size = $_POST['ring-size'];
$metal_purity = $_POST['metal-purity'];
// Calculate the updated price
$updated_price = calculate_updated_price($product, $ring_size, $metal_purity);
// Remove any existing items of the same product from the cart
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] === $product->get_id()) {
WC()->cart->remove_cart_item($cart_item_key);
}
}
// Set the updated price in the cart item data
$cart_item_data = array(
'updated_price' => $updated_price,
'unique_key' => md5(microtime() . rand())
);
// Add the cart item
WC()->cart->add_to_cart($product->get_id(), 1, 0, array(), $cart_item_data);
// Get the updated cart value
$updated_cart_value = WC()->cart->get_cart_total();
// Display the updated cart value
echo 'Updated Cart Value: ' . $updated_cart_value;
}
}
}
// Function to calculate the updated price
function calculate_updated_price($product, $ring_size, $metal_purity)
{
global $wpdb;
// Load the latest gold rate
$gold_rate_table = $wpdb->prefix . 'gold_rate';
$gold_rate = $wpdb->get_var("SELECT final_price FROM $gold_rate_table ORDER BY id DESC LIMIT 1");
// Get Metal Purity percentage
$metal_purity_table = $wpdb->prefix . 'diamond_purity';
$query = $wpdb->prepare("SELECT percentage FROM $metal_purity_table WHERE purity = %s", $metal_purity);
$metal_purity_percentage = $wpdb->get_var($query);
// Calculate gold value
$gold_value = ($gold_rate * $metal_purity_percentage) / 100;
// Get net weight product attribute
$net_weight = $product->get_attribute('net-weight-g');
// Get size gram variation
$ring_size_table = $wpdb->prefix . 'ring_size';
$query = $wpdb->prepare("SELECT variation_grams FROM $ring_size_table WHERE ring_size = %s", $ring_size);
$ring_grams_percentage = $wpdb->get_var($query);
// Get product regular price
$regular_price = $product->get_regular_price();
// Diamond Shape
$dquality = $product->get_attribute('diamond-quality');
$sieves = $product->get_attribute('sieves');
$dweight = $product->get_attribute('diamond-weight-ct');
$dpieces = $product->get_attribute('diamond-pieces');
$round_diamond_table = $wpdb->prefix . 'round_diamond';
// Prepare the SQL query with proper escaping
$query = $wpdb->prepare("SELECT $sieves FROM $round_diamond_table WHERE diamond_quality = %s", $dquality);
// Run the prepared query
$dshape = $wpdb->get_var($query);
$dtotal = ($dshape * ($dweight / 5));
$dmtotal = $dtotal * $dpieces;
$default_ring_size = $product->get_attribute('default-ring-size');
if ($default_ring_size < $ring_size) {
$netweight = $net_weight + $ring_grams_percentage;
// Calculate product updated pric
$updated_price = ($gold_value * $netweight) + $regular_price + $dmtotal;
} elseif ($default_ring_size = $ring_size) {
$updated_price = ($gold_value * $net_weight) + $regular_price + $dmtotal;
} else {
$netweight2 = $net_weight - $ring_grams_percentage;
$updated_price = ($gold_value * $netweight2) + $regular_price + $dmtotal;
}
// Get the displayed price
$args = array('price' => floatval($updated_price));
if ('incl' === get_option('woocommerce_tax_display_shop')) {
$displayed_price = wc_get_price_including_tax($product, $args);
} else {
$displayed_price = wc_get_price_excluding_tax($product, $args);
}
// Display product updated price
printf('<p class="productprice">%s</p>', wc_price($displayed_price));
// Display a hidden input field with the "updated_price" as value
printf('<input type="hidden" name="updated_price" value="%s" />', $updated_price);
return $updated_price;
}
add_filter('woocommerce_add_cart_item_data', 'save_custom_cart_item_data', 10, 2);
function save_custom_cart_item_data($cart_item_data, $product_id)
{
if (isset($_POST['updated_price']) && !empty($_POST['updated_price'])) {
// Set the custom data in the cart item
$cart_item_data['updated_price'] = (float) wc_clean($_POST['updated_price']);
// Make each item as a unique separated cart item
$cart_item_data['unique_key'] = md5(microtime() . rand());
}
return $cart_item_data;
}
add_action('woocommerce_cart_item_price', 'filter_cart_displayed_price', 10, 2);
function filter_cart_displayed_price($price, $cart_item)
{
if (isset($cart_item['updated_price'])) {
$args = array('price' => floatval($cart_item['updated_price']));
if ('incl' === get_option('woocommerce_tax_display_cart')) {
$product_price = wc_get_price_including_tax($cart_item['data'], $args);
} else {
$product_price = wc_get_price_excluding_tax($cart_item['data'], $args);
}
return wc_price($product_price);
}
return $price;
}
add_action('woocommerce_before_calculate_totals', 'set_new_cart_item_updated_price');
function set_new_cart_item_updated_price($cart)
{
if ((is_admin() && !defined('DOING_AJAX')))
return;
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
// Loop through cart items and set the updated price
foreach ($cart->get_cart() as $cart_item) {
// Set the new price
if (isset($cart_item['updated_price'])) {
$cart_item['data']->set_price($cart_item['updated_price']);
}
}
}
字符串
我试着做:
// Calculate the updated price
$updated_price = calculate_updated_price($product, $ring_size, $metal_purity);
// Remove any existing items of the same product from the cart
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] === $product->get_id()) {
WC()->cart->remove_cart_item($cart_item_key);
}
}`
型
但它同时接受初始值和定制值。
1条答案
按热度按时间gzszwxb41#
这需要jQuery和 AJAX ,以使其按预期工作。
我已经重新访问了您的所有代码,将所有自定义SQL查询分离到各个函数中,现在可以重用了。
所有产品数据(产品属性、价格和黄金汇率)现在都在一个单独的(可重用的)函数中:
我已经删除了你的提交按钮(和自定义表单),移动了添加到购物车表单中的2个选择器,所以它们显示在添加到购物车按钮之前(在添加到购物车表单中)。在开始时,在每个选择字段上设置默认值(来自产品)(以前不是这种情况),因此不需要验证,因为总是会有选定的值。
现在,当客户选择其他东西时,价格将通过 AJAX 自动计算和更新。在 AJAX 过程中(2秒),表单上的所有内容都被阻塞(就像在checkout Ajax事件中一样)。
我已经用假值欺骗了你的自定义SQL查询,以检查一切正常。同样的事情。产品属性
下面是重新访问的代码:
Jquery( AJAX )外部文件名为“price-calculator.js”
字符串
PHP部分:
型
该代码可用于:
应该能用