简体   繁体   English

WooCommerce - 在购物车中获取产品的选定变体

[英]WooCommerce - Get selected variation for a product in cart

Hello, 你好,

Any one please help me find the solution. 任何人请帮我找到解决方案。

My client has a wholesale business, in which he don't need woocommerce checkout functionality. 我的客户有批发业务,他不需要woocommerce结账功能。 He needs woocommerce functionality upto cart, but instead of checkout he wants a "Place Order" button. 他需要购物车的woocommerce功能,但他不想结账,他想要一个“下订单”按钮。

Now, everything is working fine, placing order correctly, order is storing into database and mailing to admin, but the problem is that I want to store variations as well, my question is how to get selected variation (if there is any) of a product inside functions.php for that product, which is already in cart? 现在,一切工作正常,订货正确,顺序存储到数据库,并邮寄给管理员,但问题是,我要存储的变化,以及,我的问题是如何让选择的变化 (如果有的话) 的产品内部的functions.php产品已经在购物车中?

Any hint will be much appreciated. 任何提示都将非常感激。

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'] 购物车商品是关联数组,您可以在$item['product_id']找到商品ID,在$item['variation_id']找到变体ID

Please use following function & pass variation id to get variation detail: 请使用以下函数和传递变体ID来获取变体详细信息:

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. 希望它会有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM