admin管理员组文章数量:1127895
Hope something can help me, since php is unfortunately not my world:
I need to simply extract the product price of a woocommerce product, and divide it by a number taken from an attribute, and then display the number from the attribute x new price. (Reason: show the customer the number of possible partial payments and the price per partial payment.)
I use the Code Snippets Plugin.
I need the solution both for the product archive page/shop page, and for individual products.
Lets call the divider attribute slug: partial_payments.
Example 1:
Product price: 1234
Number from attribute: 1
New price to be displayed: 1234
Example 2:
Product price: 5500
Number from attribute: 2
New price to be displayed: 2x2250
Example 3:
Product price: 6870
Number from attribute: 3
New price to be displayed: 3x2290
Thank you very much in advance.
Hope something can help me, since php is unfortunately not my world:
I need to simply extract the product price of a woocommerce product, and divide it by a number taken from an attribute, and then display the number from the attribute x new price. (Reason: show the customer the number of possible partial payments and the price per partial payment.)
I use the Code Snippets Plugin.
I need the solution both for the product archive page/shop page, and for individual products.
Lets call the divider attribute slug: partial_payments.
Example 1:
Product price: 1234
Number from attribute: 1
New price to be displayed: 1234
Example 2:
Product price: 5500
Number from attribute: 2
New price to be displayed: 2x2250
Example 3:
Product price: 6870
Number from attribute: 3
New price to be displayed: 3x2290
Thank you very much in advance.
Share Improve this question edited Dec 22, 2023 at 19:18 Bjørn Erik Sandbakk asked Dec 22, 2023 at 10:06 Bjørn Erik SandbakkBjørn Erik Sandbakk 31 bronze badge1 Answer
Reset to default 0I think this might help you achieve your goal.
At the end of the functions.php
file, add the following PHP code:
// Add custom price calculation for WooCommerce products
add_filter('woocommerce_get_price_html', 'custom_product_price_html', 10, 2);
function custom_product_price_html($price_html, $product) {
if ($product->is_type('simple')) {
// Get the product price
$product_price = floatval($product->get_price());
// Get the attribute value (partial_payments)
$attribute_value = intval($product->get_attribute('partial_payments'));
if ($attribute_value > 0) {
// Calculate the new price
$new_price = $product_price / $attribute_value;
// Format the new price
$new_price_html = wc_price($new_price);
// Display the new price with the attribute value
$price_html = sprintf('%dx%s', $attribute_value, $new_price_html);
}
}
return $price_html;
}
Now, your custom price calculation code will dynamically calculate and display the new prices based on the "partial_payments" attribute value for your WooCommerce products. Make sure to replace "partial_payments" with the actual slug of your attribute.
Remember to create a backup of your site or the functions.php
file before making any changes to ensure you can revert them if needed!
本文标签:
版权声明:本文标题:Woocommerce, via php snippet: How to get product price and divide it by a number from an attribute, and then display the new pri 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736706805a1948721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论