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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

I 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!

本文标签: