如何更改添加到购物车表单



有没有办法通过functions.php更改WooCommerce添加到购物车的形式?

目标是为附加产品添加一个复选框。选中复选框后,单击"添加到购物车"按钮,此产品也将添加到购物篮中。

我正在寻找一个不依赖javascript的解决方案。

更好的标题是"WooCommerce以复选框形式追加销售"

大量的研究和解决这个问题的几种策略让我找到了一个解决方案,我一开始甚至认为这是不可能的。

现在的解决方案正是我想要的。一个非JavaScript,没有模板覆盖,但对functions.php进行了简单而纯粹的添加。它适用于简单和可变的产品(可能也适用于分组和外部产品)。

它仍然缺少一些不错的功能。如果追加销售是一种可变的产品,它还不会起作用。数量选择和限制每件商品或订单的销售也将是很好的补充。根据下面的代码,添加这些功能应该不再是什么大事了。

// create the checkbox form fields and add them before the cart button
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_form', 10, 0 );
function action_woocommerce_before_add_to_cart_form(){
    global $woocommerce, $product;
    // get the product up-sells
    $upsells = $product->get_upsells();
    // store the number of up-sells and pass it on to the add-to-cart hook
    ?>
    <input type="hidden" name="upsells_size" value="<?php echo(sizeof($upsells)); ?>">
    <div id="wb-upsell-div">
    <?php
    // iterate through all upsells and add an input field for each
    $i = 1;
    foreach( $upsells as $value ){
        $product_id  = $value;
        ?>
        <input id="wb-upsell-checkboxes" type="checkbox" name="upsell_<?php echo($i) ?>" value="<?php echo($product_id); ?>"><?php echo( '<a href="' . esc_url( get_permalink( $product_id )) . '" target="_blank">' . get_the_title( $product_id ) . "</a>". "&nbsp;&nbsp;&nbsp;&nbsp;($" . get_post_meta( $product_id, '_regular_price', true) . ")"); ?><br>
        <?php
        $i++;
    }
    ?>  
    </div>
    <?php
}

// function to add all up-sells, where the checkbox have been checked, to the cart 
add_action('woocommerce_add_to_cart', 'custom_add_to_cart', 10, 3);
function custom_add_to_cart() {
    global $woocommerce;
    // get the number of up-sells to iterate through
    $upsell_size = $_POST['upsells_size'];
    // iterate through up-sell fields
    for ($i=1; $i<=$upsell_size; $i++){
        // get the product id of the up-sell
        $product_id = $_POST['upsell_' . $i];
        $found = false;
        //check if product already in cart
        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 product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

这是用于格式化<div>和复选框的CSS。它进入style.css文件:

#wb-upsell-div {
    margin-top: 10px;
    margin-bottom: 20px;
}
#wb-upsell-checkboxes{
}

因此,这个问题有一个实际的答案,您可以使用钩子在添加到购物车<form>中添加您想要的任何内容。例如:

add_action( 'woocommerce_before_add_to_cart_button', 'so_34115452_add_input' );
function so_34115452_add_input(){
    echo '<input type="checkbox" name="something"/>' . __( 'Some Checkbox', 'text-domain' );
}

最新更新