Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #16324
    era_uma_vez
    Participant

    Hello,

    I am getting the ‘select one’-checkbox and I follow your instructions:

    > Create a subdirectory woocommerce-auto-added-coupons-pro on my children template;
    > Copy the files templates/cart/select-free-product.php and templates/checkout/select-free-product.php in here.

    Then after reading some post, here, i tried this code: https://www.soft79.nl/forums/topic/question-about-free-product-and-cache-on-cart/ From 2018, but didn’t work.

    Did you update it? Could you please share a current version

    Thanks

    #16327
    Soft79
    Keymaster

    The subdir must be woocommerce-auto-added-coupons (without pro).

    #16328
    era_uma_vez
    Participant

    I got that subdir from your documentation: https://www.soft79.nl/documentation/wjecf/usage/
    Could you please reply to my second question, because i already tried both php codes and don’t work – i am still with selection boxes

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

    This was the last code:

    <?php
    /**
     * Select Free Product on Cart page
     *
     * This template can be overridden by copying it to yourtheme/woocommerce-auto-added-coupons/cart/select-free-product.php
     *
     * @version     2.6.0
     */
    
    defined( 'ABSPATH' ) or die();
    
    /**************************************************************************
    
    Available variables:
    	$free_gift_coupons     : (deprecated) An array of WC_Coupon objects applied to the cart that grant free product selections
    	$template              : The template helper object (WJECF_Pro_Free_Products_Template)
    	$coupons_form_data     : An array with the following info:
    	[
    		$coupon_code =>
    			[
    				'coupon'                  => The WC_Coupon object
    				'coupon_code'             => The coupon code
    				'allow_multiple_products' => True if multiplication is enabled for this coupon
    				'form_items'              => WJECF_Free_Product_Item objects. Contains all info about the free products
    				'selected_quantity'       => Amount of items selected by the customer
    				'max_quantity'            => The max amount of free products for this coupon
    				'name_prefix'             => The name prefix for all form input elements (checkbox / radiobutton / input type="number") for this coupon (e.g. 'wjecf_free_sel[0]')
    				'id_prefix'               => The unique prefix to use for all DOM elements for this coupon ( e.g. 'wjecf_free_sel_0')
    				'totalizer_id'            => The id of the <input> that is used to count the total amount of selected items (e.g. 'wjecf_free_sel_0_total_qty')
    				'template'                => The template helper object (WJECF_Pro_Free_Products_Template)
    			],
    	]
    
    **************************************************************************/
    
    //Don't display if no free product selections...
    if ( empty( $coupons_form_data ) ) {
    	return;
    }
    
    ?>
    <tr class="wjecf-fragment-cart-select-free-product wjecf-auto-submit">
    	<td colspan="6" data-title="<?php _e( 'Free products', 'woocommerce-jos-autocoupon' ); ?>">
    		<?php
    
    		foreach ( $coupons_form_data as $coupon_code => $coupon_form_data ) :
    			$template->render_template( 'coupon-select-free-product.php', $coupon_form_data );
    		endforeach;
    
    		?>
    	</td>
    </tr>
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.