WooCommerce - Get selected variation for a product in cart

后端 未结 2 500
小鲜肉
小鲜肉 2021-02-09 16:38

Hello,

Any one please help me find the solution.

My client has a wholesale business, in which he don\'t need woocommerce checkout functionality. He needs woocomm

相关标签:
2条回答
  • 2021-02-09 17:07

    Hope that I've understood your query correctly.

    You are saying that you want to get variations detail (if available) of the product, which is there in cart.

    Cart contains many items. You can loop over items & can get variation details of each items.

    A cart item is an associative array & you may find product id in $item['product_id'] & variation id in $item['variation_id']

    Please use following function & pass variation id to get variation detail:

    function get_variation_data_from_variation_id( $item_id ) {
        $_product = new WC_Product_Variation( $item_id );
        $variation_data = $_product->get_variation_attributes();
        $variation_detail = woocommerce_get_formatted_variation( $variation_data, true );  // this will give all variation detail in one line
        // $variation_detail = woocommerce_get_formatted_variation( $variation_data, false);  // this will give all variation detail one by one
        return $variation_detail; // $variation_detail will return string containing variation detail which can be used to print on website
        // return $variation_data; // $variation_data will return only the data which can be used to store variation data
    }
    

    Now let's see how to use this function

    $item_id = ( !empty( $cart_item['variation_id'] ) ) ? $cart_item['variation_id'] : '';
    if ( !empty( $item_id ) ) {
       $variations = get_variation_data_from_variation_id( $item_id );
    }
    

    Hope it'll be useful.

    0 讨论(0)
  • 2021-02-09 17:18

    Hope this one will help...

    function woocommerce_variable_add_to_carts() {
            global $product, $post;
            $variations = $product->get_available_variations();
            foreach ($variations as $key => $value) {
                ?>
                <form action="<?php echo esc_url($product->add_to_cart_url()); ?>"method="post" enctype='multipart/form-data'>
                    <input type="hidden" name="variation_id" value="<?php echo $value['variation_id'] ?>" />
                    <input type="hidden" name="product_id" value="<?php echo esc_attr($post->ID); ?>" />
                    <?php
                    if (!empty($value['attributes'])) {
                        foreach ($value['attributes'] as $attr_key => $attr_value) {
                            ?>
                            <input type="hidden" name="<?php echo $attr_key ?>" value="<?php echo $attr_value ?>">
                            <?php
                        }
                    }
                    ?>
    
                    <?php echo implode('/', $value['attributes']); ?>
    
                    <?php echo $value['price_html']; ?>
                    </
                    <button type="submit" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __('Add to cart', 'woocommerce'), $product->product_type); ?></button>
    
                </form>
                <?php
            }
        }
    
    0 讨论(0)
提交回复
热议问题