douqi0090 2017-06-26 12:15
浏览 67
已采纳

WooCommerce:将输入添加到购物车,然后将数据添加到购物车项目

I've been trying to add an option on items on a cart so that the customer can rename the product. This is used for them to keep track of different products of the same type to be sent out to different people(making a hamper system)

I have used the woocommerce_cart_item_name filter to add the input field to the cart items. I am using the answer from this link and trigger the wc_update_cart in my javascript when the user will click the "ADD" button next to the input box, but it doesn't seem to be working at all.

Is there any other way that I can add this custom name data to the respective cart item after the product is added in? I know I can do it while add it to the cart( or a work around of removing that product and adding it back in with this new field. But don't want to do that)

Thanks in advance for your help.

  • 写回答

1条回答 默认 最新

  • dqcqcqwq38860 2017-06-26 13:24
    关注

    Found the problem.

    The "Add" button that I had next to the input box, which I was using to trigger the wc_update_cart like jQuery(document).trigger('wc_update_cart'); was not validating the check I had put on the php side for the value of $_POST['update_cart']. When I realised that I changed my woocommerce_cart_item_name filter to only add the input box and use the Update Basket button to update the name. So my code changed to:

    <?php
        add_filter('woocommerce_cart_item_name','custom_naming_front',10,3);
    
        function custom_naming_front($product_title,$cart_item,$cart_item_key){
            if(isset($cart_item['custom_name']) && !empty($cart_item['custom_name'])){
                $product_title=$cart_item['custom_name'];
            }
            $product_title .= '<div class="custom-name-form-wrapper" id="custom-name'.$cart_item_key.'">
                                        <input type="text" name="cart['.$cart_item_key.'][custom_name]" id="custom_name_text'.$cart_item_key.'" value="" placeholder="enter the name you want to give this product" />
                                    </div>
                                </div>';
    
            return $product_title;
    
        }
    ?>
    

    and the function to show update the name looks like:

    <?php
    
        function custom_name_save_to_cart(){
            if ( ( ! empty( $_POST['apply_coupon'] ) || ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
                    $cart_totals  = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
                if ( ! WC()->cart->is_empty()) {
                    foreach ( WC()->cart->cart_contents as $cart_item_key => $values ) {
                        if ( ! isset( $cart_totals[ $cart_item_key ] ) || ! isset( $cart_totals[ $cart_item_key ]['custom_name'] ) ) {
                            continue;
                        }
                        WC()->cart->cart_contents[ $cart_item_key ]['custom_name'] = $cart_totals[ $cart_item_key ]['custom_name'];
    
                    }
                }
    
            }
    
         }
        add_action( 'wp_loaded', 'custom_name_save_to_cart', 25);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R