Add Form add to cart on Shop, Category

<?php
add_action('flatsome_product_box_after', 'flatsome_product_box_after_add_to_cart_callback');
if ( ! function_exists( 'flatsome_product_box_after_add_to_cart_callback' ) ) {
    function flatsome_product_box_after_add_to_cart_callback() {
        global $product;
        $stock_status = $product->get_stock_status();

        if ($stock_status === 'outofstock') {
        } else {
            $quantity = 1;
            $status_check = false;
            $min_setup = get_field("set_quantity", "option");
            $select_category = [];
            if (get_field("select_category", "option")) {
                $select_category = get_field("select_category", "option");
            }
            if(!$product->managing_stock()){
                $stock = 999; //hard code max stock
            }
            else{
                $stock = $product->get_stock_quantity();
            }
            $terms = get_the_terms($product->get_id(), 'product_cat');
            foreach ($terms as $term) {
                $product_cat_id = $term->term_id;
                $parent = $term->parent;
                if (count($select_category) > 0 && (in_array($product_cat_id, $select_category) || in_array($parent, $select_category))) {
                    $status_check = true;
                }
            }
            if ($status_check ) {
                if ($stock >= $min_setup || !$product->managing_stock()) {
                    $quantity = $min_setup;
                } else {
                    $quantity = $stock;
                }
            }
            ?>
            <div class="box-add-to-cart-product">
                <?php 
                woocommerce_quantity_input(array('min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $stock));
                if ( $product->is_type( 'variable' ) ) {
                    //$get_available_variations = $product->get_available_variations();
                    $get_available_variations = $product->get_children();
                    if($get_available_variations){
                        $variation_id_default = $get_available_variations[0];
                        $single_variation_default = wc_get_product($variation_id_default);
                        //if ($single_variation_default->get_stock_status() == 'outofstock' || !$single_variation_default || !$single_variation_default->exists()) continue;
                        if($single_variation_default && count($single_variation_default->get_variation_attributes()) == 1){
                            
                        ?>
                        <div class="menge-select">
                            <select name="variation_change" id="variation_change">
                                <?php 
                                $i = 1;
                                foreach($get_available_variations as $variation_id ){
                                    $single_variation = wc_get_product($variation_id);
                                    if ($single_variation->get_stock_status() == 'outofstock' || !$single_variation || !$single_variation->exists()) continue;
                                    $price_variable = $single_variation->get_price_html();
                                    $single_variation_name = implode(' - ', $single_variation->get_variation_attributes());
                                ?>
                                    <option data-price="<?php echo htmlentities($price_variable); ?>" <?php if($i == 1) echo "selected"; ?> value="<?php echo $variation_id; ?>"><?php echo $single_variation_name; ?></option>
                                <?php 
                                    $i++;
                                }
                                ?>
                            </select>
                        </div>
                        <?php 
                            $add_to_cart = do_shortcode('[add_to_cart id="' . $variation_id_default . '"  show_price="false" quantity="' . $quantity . '"]'); 
                        }
                        else{
                            $add_to_cart =  do_shortcode('[add_to_cart id="' . $product->get_id() . '"  show_price="false" quantity="' . $quantity . '"]'); 
                        }
                    }
                }
                else{
                    $add_to_cart = do_shortcode('[add_to_cart id="' . $product->get_id() . '"  show_price="false" quantity="' . $quantity . '"]'); 
                }
                echo $add_to_cart;
                ?>
            </div>
            <?php 
        }
    }
}

JS update price, quanty on change

jQuery(document).ready(function ($) {
    jQuery( "body" ).on( "change input", ".quantity .qty", function() {
		var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
		add_to_cart_button.attr( "data-quantity", jQuery( this ).val() );
		add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
	});
    jQuery( "body" ).on( "change", "#variation_change", function() {
        var price = $(this).find(':selected').data('price');
        jQuery( this ).parents( ".product" ).find( ".price-wrapper .price" ).html(price);
		var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
		add_to_cart_button.attr( "data-product_id", jQuery( this ).val() );
		add_to_cart_button.attr( "href", "?add-to-cart=" + jQuery( this ).val() + "&quantity=" + add_to_cart_button.attr( "data-quantity" ) );
	});
});

Leave a Reply

Your email address will not be published. Required fields are marked *