Repository Forums Support WooCommerce Extended Coupon Features PRO Remove free item from cart

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1588
    Jonathan Moore
    Participant

    Hi,

    I found that a free product can’t be removed from the basket
    – the “x” is missing to delete the free item
    – the coupon can’t be removed either

    I retested this on a plain installation with woocommerce sample products on:
    WP4.8
    WooCommerce 3.1.0-rc.1
    WooCommerce Extended Coupon Features PRO

    The test case is:
    Coupon for Free Product by Minimum Spend 50, Auto-Coupon selected

    Tested these different options:

    Coupon set to 1 Free Product, “Select one” not selected
    – Result: product is added to basket but cannot be removed
    – Expected: should be able to remove the product in case the customer doesn’t want that product

    Coupon set to 1 Free Product, “Select one” is selected
    – Result: free gift option with a selection of one gift.
    If you click the radio button it can’t be unclicked again, although it is possible to refresh the page and it is then possible to ignore prompts for the unwanted gift and complete the checkout.

    Actually mandatory addition/chaining of products is a cool feature.. and would be great to have it as a separate option from the Free Product.

    So….

    MaybeBug: should be permitted to remove free product from cart
    Request: would be great to keep the mandatory add item facility as a separate feature.. (with a linked discount applied which could be 100% [free] or percentage reduction)

    #1590
    Soft79
    Keymaster

    Hi,

    This is the way the plugin is designed. If you’re comfortable with template overrides, you can override coupon-select-free-product.php from the template/ directory of this plugin by placing it in:

    wp-content/YOUR_THEME/woocommerce-auto-added-coupons/coupon-select-free-product.php

    and then adding this snippet before the tag:

    <input type="radio" id="<?php echo $field_id; ?>" name="<?php echo $name_prefix; ?>[selected_product]" value="" />
    <label for="<?php echo $field_id; ?>">No product</label><br>

    That adds an option to select ‘no product’.

    #1594
    Jonathan Moore
    Participant

    Yes thanks that would apply to the second case,
    although variables such as $field_id are only set within the loop of available products, really the none option needs to be set outside the loop.

    Something like this may do it:

    
            endforeach;
            if ($input_type='radio') {
                $field_id = esc_attr( $id_prefix . '_' . 0 );
                echo '<input type="radio"    id="' . $field_id . '" name="' . $name_prefix . '[selected_product]" value="" ' . ( ($free_item_selected ) ? '' : ' checked="checked"' ) . ' />';
                
        ?>
                <label for="<?php echo $field_id; ?>"><?php _e('None', 'your-text-domain') ?></label><br>
            <?php } ?>
    

    note a $free_item_selected is added and tested within the loop to be set to true if a free item has been chosen, otherwise None should be selected.

    #1595
    Soft79
    Keymaster

    Yes, I didn’t test it, sorry about that.

    This is from a production site:

    
    <?php
    /**
     * Single coupon ( for the "Select Free Product" on Cart or Checkout page )
     * 
     * This template can be overridden by copying it to yourtheme/woocommerce-auto-added-coupons/coupon-select-free-product.php
     * 
     * @version     2.5.1
     */
    
    defined('ABSPATH') or die();
    
    $tooltip = sprintf(
        _n( 
            'You can select one free product.', //singular
            'You can select up to %d free products.', //plural
            $max_quantity,
            'woocommerce-jos-autocoupon'
        ), 
        $max_quantity
    );
    
    $input_type = $allow_multiple_products ? 'number' : 'radio';
    
    //This DOM object will manage the total quantity of the selected products
    $totalizer_id = $id_prefix . '_total_qty';
    
    ?>
    <div class="wjecf-select-free-products coupon-<?php echo esc_attr( sanitize_title( $coupon_code ) ); ?>">
        <h3><?php echo WJECF_API()->get_select_free_product_message( $coupon ); ?></h3>
        <input type="hidden" name="<?php echo $name_prefix; ?>[coupon]" value="<?php echo esc_attr( $coupon_code ); ?>" />
        <input type="hidden" id="<?php echo $totalizer_id; ?>" data-wjecf-qty-max="<?php echo $max_quantity; ?>" />
        <ul class="wjecf-cols cols-4">
        <?php 
            $product_rendered = false; 
            $product_selected = false;
    
            foreach ( $form_items as $key => $form_item ):
                $product = $form_item->getProduct(); 
                if ( ! $product instanceof WC_Product || ! $product->is_in_stock() ) {
                    //Only display items that are in stock
                    continue;
                }
    
                $value = $form_item->getQuantity();
                $product_id = $form_item->getProductId(); 
                $field_id = esc_attr( $id_prefix . '_' . $key ); // e.g. wjecf_sel_0_0
                $field_name_prefix = esc_attr( "{$name_prefix}[products][{$key}]" ); // e.g. wjecf_sel[0][product][0]
    
                $product_rendered = true; 
                $product_selected = $product_selected || ! empty( $value );
        ?>
                <li>
                    <input type="hidden" name="<?php echo $field_name_prefix; ?>[product_id]" value="<?php echo $product_id; ?>" />
                    <?php
                        switch( $input_type ) {
                            case 'radio':
                                echo '<input type="radio"    id="' . $field_id . '" name="' . $name_prefix . '[selected_product]" value="' . $product_id . '" ' . ( empty( $value ) ? '' : ' checked="checked"' ) . ' />';
                                break;
                            case 'checkbox':
                                echo '<input type="checkbox" id="' . $field_id . '" name="' . $field_name_prefix . '[quantity]"   value="1" ' . ( empty( $value ) ? '' : ' checked="checked"' ) . ' title="' . esc_attr( $tooltip ) . '" data-wjecf-qty-totalizer="' . $id_prefix . '_total_qty" />';
                                break;
                            case 'number':
                                echo '<input type="number"   id="' . $field_id . '" name="' . $field_name_prefix . '[quantity]"   value="' . intval( $value ) . '" min="0" max="' . $max_quantity . '" title="' . esc_attr( $tooltip ) . '" data-wjecf-qty-totalizer="' . $id_prefix . '_total_qty" />';
                                break;
                        }
                        echo '<label for="' . $field_id . '">' . esc_html( $product->get_title(), 'woocommerce' ) . '</label><br>';
                        echo $product->get_image();
    
                        //Variable product attributes
                        if ( $product->is_type( 'variable' ) ) {
                            $template->render_attribute_selectors( $product, $form_item->getAttributes(), $field_id, $field_name_prefix . '[attributes]' );
                        }
                    ?>
                </li>
        <?php
            endforeach;
            if ( $product_rendered && $input_type == 'radio' ) {
                echo '<li>';
                echo '<input type="radio" id="' .$id_prefix . '_none" name="' . $name_prefix . '[selected_product]"' . ( $product_selected ? '' : 'checked="checked"' ) . ' />';
                echo '<label for="' .$id_prefix . '_none" />' . __( "No product" ) . '</label>';
                echo '</li>';
            }
        ?>
        </ul>
        <p>
    </div>
    
    #2225
    Jonathan Moore
    Participant

    I realised I had another variant on this, when removing the last item from cart, a free item was getting left behind and in fact there was no way for the user to remove it. There is a simple workaround which is to add a minimum spend on the coupon for the free item, at which point the free item is then removed automatically.

    This is on woo3.1.2 (3.2.x is still getting too many patches and breaks too many other things) and WooCommerce Extended Coupon Features PRO v2.5.3 and 2.5.5 (tested both)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.