• Resolved peter09m

    (@peter09m)


    Dear Community and Developers!

    I would like to ask for help in creating a custom action.

    On my website, I sell many products that users often buy in larger quantities. Based on this, I would like to reward them with points. I have researched the possibilities of the GamiPress plugin, but unfortunately, I couldn’t find a feature like this.

    The essence of the functionality is to give the user a reward based on the product quantity after they make a purchase using the GamiPress plugin. For example every quantity gives a reward of 10 points to the user, so Product Quantity x 10 points. I want to encourage users to continue buying larger quantities of the products.

    I have already written a custom function, but I only achieved partial success. Even though I query the user ID in the code and pass it along, all registered users on the site receive the same amount of points, not just the user who purchased the product.

    Could you please assist me with this issue?

    function my_prefix_award_points_per_quantity_on_woocommerce($order_id) {
        $order = wc_get_order($order_id);
        $points_type = 'reward-points'; 
        $points_to_award = 0;
        
        foreach ($order->get_items() as $item) {
            $product_id = $item->get_product_id();
            $quantity = $item->get_quantity();
            $points_per_quantity = 10; 
            $product_categories = get_the_terms($product_id, 'product_cat');
            $category_ids = array(32, 33, 34);
            
            $product_belongs_to_category = false;
            
            foreach ($product_categories as $category) {
                if (in_array($category->term_id, $category_ids)) {
                    $product_belongs_to_category = true;
                    break;
                }
            }
            
            if ($product_belongs_to_category) {
                $points_to_award += absint($quantity * $points_per_quantity);
        }
        
        if ($points_to_award > 0) {
            gamipress_award_points_to_user($order->get_user_id(), $points_to_award, $points_type);
        }
    }

Viewing 1 replies (of 1 total)
  • Plugin Author Dioni Sánchez

    (@dioni00)

    Hi @peter09m

    The WooCommerce integration with Gamipress already includes that feature. If, for example, you use the “Purchase a product” event to award 10 points and your purchase is: Product 1 x qty 5 and Product 2 x qty 2, the customer will get 70 points since they have purchased a total of 7 products.

    Please feel free to use the free add-on and check the functionality.

    https://gamipress.com/add-ons/woocommerce-integration/

    Best regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Award user based on product Quantity’ is closed to new replies.