Search
 
SCRIPT & CODE EXAMPLE
 

C

add a item to cart woocomerce with quantity

WC()->cart->add_to_cart( 500, 5 );
Comment

add to cart woocommerce product with variation

// 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;

	}

}
Comment

PREVIOUS NEXT
Code Example
C :: c convert char to int 
C :: Convert mpg / mp4 to gif with ffmpeg 
C :: initialize array c 
C :: c substring 
C :: format specifiers in c 
C :: what is the usage of extern in c 
C :: try and catch in rust 
C :: Write a C program to merge two array to third array. 
C :: measure time in c 
C :: c functions example 
C :: bootsrap textbox 
C :: how to compareTo in java 
C :: unable to locate package dos2unix 
C :: bubble sort 
C :: number pattern in c 
C :: how to empty array in c 
C :: c language float user input 
C :: how to merge 2 bytes into an integer 
C :: sizeof file c 
C :: command line arguments c 
C :: threads in c 
C :: round c 
C :: C program to calculate the sum of odd and even numbers 
C :: what is clrsce in C 
C :: columntransformer in randomizedsearchcv 
C :: cocktail sort in c 
C :: anthracnose pronounce 
C :: hgggggggggggggggg 
C :: produit deux matrice 
C :: c programming print pattern pyramid 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =