在 WooCommerce 单个产品页面上添加一个 select 字段,允许将其他产品添加到购物车

Add a select field on WooCommerce single product pages that allows an additional product to be added to cart

我希望在我们的一些单一产品页面中添加 select 系列产品。

我添加了一个 select 字段,它抓取特定类别的所有产品并将它们添加为选项。

基于用户 selection,我想将 selected 产品与主要产品一起添加到购物车。仅当附加产品尚未在购物车中时才如此。

这是我的代码尝试:

function comp_bracket_selection( $selection ) {
    echo '<div><br></div>';
    global $product;
    $domain = 'woocommerce';

    $args = array(
        'category'      => 'compressor-brackets', 
        'limit'         => -1,
    );

    $product_array = wc_get_products( $args );
    foreach ($product_array as $comp_product) {
        $comp_id = $comp_product->get_id();
        echo 'Product id: ' . $comp_id . ' | ';
        $options[$comp_id] = $comp_product->get_name();
    }

    array_unshift($options, 'Choose an option');
    woocommerce_form_field('compressor-options', array(
        'id'            => 'compressor-selection',
        'type'          => 'select',
        'label'         => __('Choose Compressor & Bracket', $domain),
        'required'      => false,
        'options'       => $options,
        ),''
    );

    echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'comp_bracket_selection' );

function custom_add_to_cart() {
    if (isset($_POST['compressor-options'])) {

        $product_id = $_POST['compressor-options'];
        $found = false;
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                $found = true;
            }
            if ( ! $found )
            WC()->cart->add_to_cart( $product_id );
        } else {
            WC()->cart->add_to_cart( $product_id );
        }
    }
}
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart' );

但是我在将 selected 产品添加到购物车时遇到了问题。任何建议将不胜感激。

关于您的代码的一些注释 attempt/question:

  • 你犯的错误是使用 array_unshift() 导致所有数字数组键将被修改为从零开始计数
  • 您使用 $_POST['compressor-options']$_POST['compressor-selection'],但它们应该相等
  • 要检查产品是否已在购物车中,您可以使用 WC_Cart::find_product_in_cart()

所以你得到:

function action_woocommerce_before_add_to_cart_button() {
    global $product;

    $domain = 'woocommerce';

    $args = array(
        'category'      => 'compressor-bracket', 
        'limit'         => -1,
    );

    // Retrieving products
    $product_array = wc_get_products( $args );

    // NOT empty
    if ( ! empty( $product_array ) ) {
        foreach ( $product_array as $product ) {
            $product_id = $product->get_id();
            echo 'Product id: ' . $product_id . ' | ';
            $options[$product_id] = $product->get_name();
        }

        $options = array( 0 => __( 'Choose an option', $domain ) ) + $options;

        // Add select field
        woocommerce_form_field( 'compressor-options', array(
            'id'            => 'compressor-selection',
            'type'          => 'select',
            'label'         => __( 'Choose Compressor & Bracket', $domain ),
            'required'      => false,
            'options'       => $options,
        ),'' );

        echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
    }
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );

function filter_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    if ( isset( $_POST['compressor-options'] ) ) {
        // Get product ID
        $the_product_id = sanitize_text_field( $_POST['compressor-options'] );

        // WC Cart
        if ( WC()->cart ) {
            // Get cart
            $cart = WC()->cart;
            
            // If cart is NOT empty
            if ( ! $cart->is_empty() ) {
                // Cart id
                $product_cart_id = $cart->generate_cart_id( $the_product_id );

                // Find product in cart
                $in_cart = $cart->find_product_in_cart( $product_cart_id );

                // NOT in cart
                if ( ! $in_cart ) {
                    $cart->add_to_cart( $the_product_id );
                }
            } else {
                $cart->add_to_cart( $the_product_id );
            }
        }
    }
}
add_action( 'woocommerce_add_to_cart', 'filter_woocommerce_add_to_cart', 10, 6 );

注:如果要从select选项列表中排除当前产品

变化:

$args = array(
    'category'      => 'compressor-bracket', 
    'limit'         => -1,
);

收件人:

$args = array(
    'category'      => 'compressor-bracket', 
    'limit'         => -1,
    'exclude'       => array( $product->get_id() ),
);