WooCommerce可变产品通知发行-请选择产品选项 [英] WooCommerce Variable Product notice Issue - please choose product options

查看:108
本文介绍了WooCommerce可变产品通知发行-请选择产品选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个电子商务网站.我在使用WooCommerce 可变产品时遇到了麻烦.

I'm building an e-commerce site. I'm having some trouble with WooCommerce Variable Product.

添加到购物车"按钮适用于简单产品,但不适用于可变产品.它会发出"Please choose product options…"通知.

The "Add to Cart" button works fine with simple products, but does not work with variable products. It gives a "Please choose product options…" notice.

我四处张望,并在网上尝试了一些建议,但都无济于事.因此,我查看了WooCommerce源文件: class-wc-form-handler.php .

I looked everywhere and tried several suggestions online, none of them work. So I looked into WooCommerce source file: class-wc-form-handler.php.

在功能 add_to_cart_handler_variable 中:

In the function add_to_cart_handler_variable:

function add_to_cart_handler_variable( $product_id ) {
    $adding_to_cart     = wc_get_product( $product_id );
    $variation_id       = empty( $_REQUEST['variation_id'] ) ? '' : absint( $_REQUEST['variation_id'] );
    $quantity           = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
    $missing_attributes = array();
    $variations         = array();
    $attributes         = $adding_to_cart->get_attributes();
    $variation          = wc_get_product( $variation_id );
...
if ( $missing_attributes ) {
        wc_add_notice( sprintf( _n( '%s is a required field', '%s are required fields', sizeof( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ), 'error' );
    } elseif ( empty( $variation_id ) ) {
        wc_add_notice( __( 'Please choose product options…', 'woocommerce' ), 'error' );
    } else {
        // Add to cart validation
        $passed_validation  = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );

        if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) !== false ) {
            wc_add_to_cart_message( $product_id );
            return true;
        }
    }
    return false;
}

错误在elseif子句中捕获.
因此,我尝试回显$variation_id$variations$variation.它们中都没有任何东西,因为当我回显$variation_id时:它不输出任何东西.

The error is caught in the elseif clause.
So I tried to echo out $variation_id, $variations, and $variation. None of them has anything in it because when I echo $variation_id: it doesn't output anything.

有什么建议吗?

推荐答案

在商店页面上,您不能将购物车"按钮用于可变产品,因为您需要首先进入单个产品页面以选择用于将该可变产品添加到购物车之前.

On shop page you can't use add-to-cart button for variable products, because you need first to go on single product page to chose the options for this variable product before adding it to cart.

在可变产品页面上,通常,在使用添加到购物车"按钮之前,您会显示一些要为可变产品选择的选项.如果您不这样做,则会收到错误消息……
因此,在这一点上:

On variable product pages, normally you have some displayed options to chose for a variable product, before using "add to cart" button. If you don't do it, you get the error message…
So at this point:

  1. 产品页面中未显示选项 (后端产品页面中的设置错误,主题错误或某些其他插件):
    • 检查您的产品后端设置
    • 尝试切换到默认的wordpress主题(以查看是否仍然存在此问题)
    • 尝试禁用所有插件中的大多数.
  1. The options are not displayed in the product page (Bad settings in your backend product page, a bug with your theme or some additional plugin):
    • Check your product backend settings
    • Try to switch to default wordpress theme (to see if this issue is still there)
    • Try to disable most of all plugins.

如果此问题与您的主题有关,请联系您主题的作者并打开支持线程或故障单...

If this issue is related to your theme, contact the author of your theme and open a support thread or ticket…

为产品ID输入产品变化:

要以编程方式获取可变产品ID的产品变体,

To get product variations programmatically for a variable product ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

echo var_dump($product_variations); // Displaying the array

然后获取第一个版本ID:

Then to get the first variation ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

$variation_product_id = $product_variations [0]['variation_id'];
echo $variation_product_id; // Displaying the variation ID

或者获取该产品ID的所有版本ID的数组:

Or to get an array of all variations ID of this product ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

$arr_variations_id = array();
foreach ($product_variations as $variation) {
    $product_variation_id = $variation['variation_id'];
    array_push( $arr_variations_id, $product_variation_id );
}

echo var_dump($arr_variations_id); // Displaying the array of variations ID

参考:

这篇关于WooCommerce可变产品通知发行-请选择产品选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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