php WooCommerce - 获取购物车中产品的选定变体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22809342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WooCommerce - Get selected variation for a product in cart
提问by Habib-ur-Rahman
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 woocommerce functionality upto cart, but instead of checkout he wants a "Place Order" button.
我的客户有一个批发业务,他不需要 woocommerce 结帐功能。他需要 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.
任何提示将不胜感激。
回答by Ratnakar - StoreApps
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']
购物车项目是一个关联数组,您可以在其中找到产品 ID$item['product_id']
和变体 ID$item['variation_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.
希望它会很有用。
回答by Narendra
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
}
}