WooCommerce - 获取购物车中产品的选定变体 [英] WooCommerce - Get selected variation for a product in cart

查看:38
本文介绍了WooCommerce - 获取购物车中产品的选定变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

请任何人帮我找到解决方案.

Any one please help me find the solution.

我的客户有一个批发业务,他不需要 woocommerce 结帐功能.他需要 woocommerce 功能到购物车,但他想要一个下订单"按钮,而不是结帐.

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.

现在,一切正常,下订单正确,订单存储到数据库并邮寄给管理员,但问题是我也想存储变体,我的问题是如何被选中(如果有的话) functions.php 中产品的变体,该产品已经在购物车中?

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?

任何提示将不胜感激.

推荐答案

希望我已经正确理解了您的查询.

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.

购物车包含许多物品.您可以遍历 items &可以获取每个项目的变化细节.

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

购物车项目是一个关联数组 &您可以在 $item['product_id'] 中找到产品 ID &$item['variation_id']

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

请使用以下功能&传递变体 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 );
}

希望有用.

这篇关于WooCommerce - 获取购物车中产品的选定变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆