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

#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>