• Resolved pas2019

    (@pas2019)


    Hi,

    We have a webshop that is updated with te latest version on Woocommerce, ever since no products with variations will be added to the shopingcart. We got the following notice: Please choose product options (we did choose them)
    This is only on the category page and not on the single productpage

    The code that is used for this is shown below and created by automattic i believe

    What changed after woocommerce v4.4.1, i believe the next version broke this functions

    // Display variations dropdowns on shop page for variable products
     add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );
    
     function woo_display_variation_dropdown_on_shop_page() {
    
     	global $product;
    
    	if( $product->is_type( 'variable' )) {
    
    	$attribute_keys = array_keys( $product->get_attributes() );
    	?>
    
    	<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
    		<?php do_action( 'woocommerce_before_variations_form' ); ?>
    
    		<?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
    			<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
    		<?php else : ?>
    			<table class="variations" cellspacing="0">
    				<tbody>
    					<?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>
    						<tr>
    							<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
    							<td class="value">
    								<?php
    									$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
    									wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
    									echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
    								?>
    							</td>
    						</tr>
    					<?php endforeach;?>
    				</tbody>
    			</table>
    
    			<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
    			<div class="single_variation_wrap">
    				<?php
    					/**
    					 * woocommerce_before_single_variation Hook.
    					 */
    					do_action( 'woocommerce_before_single_variation' );
    
    					/**
    					 * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
    					 * @since 2.4.0
    					 * @hooked woocommerce_single_variation - 10 Empty div for variation data.
    					 * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
    					 */
    					do_action( 'woocommerce_single_variation' );
    
    					/**
    					 * woocommerce_after_single_variation Hook.
    					 */
    					do_action( 'woocommerce_after_single_variation' );
    				?>
    			</div>
    
    			<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    		<?php endif; ?>
    
    		<?php do_action( 'woocommerce_after_variations_form' ); ?>
    	</form>
    
    	<?php } else {
    
    		$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
    		$html .= woocommerce_quantity_input( array(), $product, false );
    		$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
    		$html .= '</form>';
    		return $html;
    
    	}
    
    }
    • This topic was modified 3 years, 4 months ago by pas2019.
    • This topic was modified 3 years, 4 months ago by pas2019.
    • This topic was modified 3 years, 4 months ago by pas2019.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hi @pas2019,

    I’ve performed some cross-testing for us today on several of my sites which have the latest versions of WordPress, WooCommerce and our free Storefront theme as the only enabled software and no code snippets added. When doing so I’ve not been able to replicate the behaviour which you describe with variations not being added to cart.

    As a test, if you remove the custom code you’ve referenced do the variations then function on your site? If the variations do then add you’ll possibly need to refactor the code snippet you’re using.

    Thread Starter pas2019

    (@pas2019)

    When i remove the code like below i see the option: Select options below each product with variables, for the regular single products the same button is: In cart

    When i click on the button select options it will take me to the product page where everything is working fine (it also did before)

    The issue is really in this code

    // Display variations dropdowns on shop page for variable products
     add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );
    
     function woo_display_variation_dropdown_on_shop_page() {
    
     	global $product;
    
    	if( $product->is_type( 'variable' )) {
    
    	$attribute_keys = array_keys( $product->get_attributes() );
    	?>
    
    	<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
    		<?php do_action( 'woocommerce_before_variations_form' ); ?>
    
    		<?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
    			<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
    		<?php else : ?>
    			<table class="variations" cellspacing="0">
    				<tbody>
    					<?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>
    						<tr>
    							<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
    							<td class="value">
    								<?php
    									$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
    									wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
    									echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
    								?>
    							</td>
    						</tr>
    					<?php endforeach;?>
    				</tbody>
    			</table>
    
    			<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
    			<div class="single_variation_wrap">
    				<?php
    					/**
    					 * woocommerce_before_single_variation Hook.
    					 */
    					do_action( 'woocommerce_before_single_variation' );
    
    					/**
    					 * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
    					 * @since 2.4.0
    					 * @hooked woocommerce_single_variation - 10 Empty div for variation data.
    					 * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
    					 */
    					do_action( 'woocommerce_single_variation' );
    
    					/**
    					 * woocommerce_after_single_variation Hook.
    					 */
    					do_action( 'woocommerce_after_single_variation' );
    				?>
    			</div>
    
    			<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    		<?php endif; ?>
    
    		<?php do_action( 'woocommerce_after_variations_form' ); ?>
    	</form>
    
    	<?php } else {
    
    		$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
    		$html .= woocommerce_quantity_input( array(), $product, false );
    		$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
    		$html .= '</form>';
    		return $html;
    
    	}
    
    }
    
    Thread Starter pas2019

    (@pas2019)

    it think it must be in this rule but i cant find out what it is:

    <?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>

    Thread Starter pas2019

    (@pas2019)

    hmm when replacing this with the code beneath it shows multiple variations without selectable options (there is only show options available in the drop down)

    <?php foreach ( $product->get_attributes() as $attribute_name => $options ) : ?>

    The option size is also available in the variables but in here i cant choose any.

    when replacing the code with the original:

    <?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>

    It shows me only 1 (the right) variation (size) with the corresponding selectable options but i cant add them to cart.

    seems to be in here somewhere but i cant figure out where

    Thread Starter pas2019

    (@pas2019)

    or in here maybe…combined with the code above

    $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
    		$html .= woocommerce_quantity_input( array(), $product, false );
    		$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
    		$html .= '</form>';
    		return $html;

    Hi @pas2019,

    Since this has been narrowed down to something in the custom code, it is best to reach out to a developer and have them take a look to see what they can do on resolving it. Developers can be reached at the following areas:

    * WooCommerce Support Forums: https://wordpress.org/support/plugin/woocommerce/
    * WooCommerce Slack Community: https://woocommerce.com/community-slack/
    * Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    as well as https://woocommerce.com/customizations. I hope this helps!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Product variation add to cart not working anymore’ is closed to new replies.