Ecommerce Shopify WordPress Discussion

Woocommerce plugin discounts on atribute

I am trying to develop a simple WordPress plugin That will, when a percentage is entered and a product attribute is selected, apply that percentage to all products under the chosen attribute, such as 'Brand', taking the regular price, reducing it by 10%, entering that amount under the sale price, and updating the products." When i hit apply it does nothing , price stays the same.Can someone check where i am wrong? Thanks <?php /* Plugin Name: Discount by Attributes Description: Plugin for applying discounts based on product attributes in WooCommerce. Version: 1.0 Author: Matija Raša Aplikacije.hr */ // Dodajemo hook koji će aktivirati funkciju pri inicijalizaciji administracijskog sučelja add_action('admin_menu', 'add_admin_page'); // Funkcija za dodavanje stranice u administracijsko sučelje function add_admin_page() { // Dodajemo stranicu pod "Woocommerce" izbornik add_submenu_page( 'woocommerce', 'Discount by Attributes', // Naziv stranice 'Discount by Attributes', // Naziv stavke izbornika 'manage_options', 'discount-by-attributes', 'render_admin_page' ); } // Funkcija za renderiranje sadržaja stranice function render_admin_page() { if (isset($_POST['discount_percentage']) && isset($_POST['product_attribute'])) { $discount_percentage = floatval($_POST['discount_percentage']); $product_attribute = sanitize_text_field($_POST['product_attribute']); // Spremanje postavki u bazu podataka update_option('discount_percentage', $discount_percentage); update_option('product_attribute', $product_attribute); // Primjenjujemo popust na proizvode s odabranim atributom apply_discount_by_attribute($discount_percentage, $product_attribute); ?> <div class="updated"><p><strong><?php _e('Discount applied to products with selected attribute.'); ?></strong></p></div> <?php } ?> <div class="wrap"> <h2>Discount by Attributes</h2> <form method="post" action=""> <table class="form-table"> <tr> <th scope="row">Discount Percentage:</th> <td><input type="number" name="discount_percentage" min="0" max="100" step="0.01" /></td> </tr> <tr> <th scope="row">Attribute:</th> <td> <select name="product_attribute"> <option value="">Select Attribute</option> <?php // Učitajte sve atribute proizvoda $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $attribute) { echo '<option value="' . $attribute->attribute_name . '">' . $attribute->attribute_label . '</option>'; } ?> </select> </td> </tr> </table> <?php submit_button('Apply Discount'); ?> </form> </div> <?php } // Logika za primjenu popusta na proizvodima s odabranim atributom function apply_discount_by_attribute($discount_percentage, $product_attribute) { if ($discount_percentage && $product_attribute) { // Postavljamo argumente za upit $args = array( 'posts_per_page' => -1, 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'pa_' . $product_attribute, 'field' => 'slug', 'terms' => $product_attribute, ), ), ); // Dohvaćamo proizvode s odabranim atributom $products = new WP_Query($args); // Iteriramo kroz pronađene proizvode i primjenjujemo popust if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); global $product; // Dohvaćamo Regular price proizvoda $regular_price = $product->get_regular_price(); // Računamo novu cijenu primjenom popusta $new_price = $regular_price * (1 - ($discount_percentage / 100)); // Postavljanje nove cijene u Sale price $product->set_sale_price($new_price); $product->save(); } } wp_reset_postdata(); } }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.