复选框字段,用于添加订阅产品并更改其他产品的价格 [英] Checkbox field that add a subscription product and change prices of other products

查看:51
本文介绍了复选框字段,用于添加订阅产品并更改其他产品的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce结帐部分,我试图添加一个复选框,以添加其他订阅产品。一切正常,我从此处

In Woocommerce checkout section, I am trying to add a checkbox that adds an additional subscription product. It is working fine and I took help from here

另一个要求是,如果购物车中还有其他产品,那么我想根据自定义更改每个产品的价格字段和新价格应显示在结帐中。
预先感谢。

One more requirement is that if there are other products in the cart then I want to change each product prices based on custom fields and new prices should display in the checkout. Thanks in advance.

我使用了此代码并且工作正常,但是产品价格在PayPal付款页面中没有变化。

I used this code and working fine but product price is not changing in PayPal payment page.

        // Display a custom checkout field
    add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
    function custom_checkbox_checkout_field() {
        $value = WC()->session->get('add_a_product');

        woocommerce_form_field( 'cb_add_product', array(
            'type'          => 'checkbox',
            'label'         => '  ' . __('Please check here to get VIP Membership'),
            'class'         => array('form-row-wide'),
        ), $value == 'yes' ? true : false );
    }


    // The jQuery Ajax request
    add_action( 'wp_footer', 'checkout_custom_jquery_script' );
    function checkout_custom_jquery_script() {
        // Only checkout page
        if( is_checkout() && ! is_wc_endpoint_url() ):

        // Remove "ship_different" custom WC session on load
        if( WC()->session->get('add_a_product') ){
            WC()->session->__unset('add_a_product');
        }
        if( WC()->session->get('product_added_key') ){
            WC()->session->__unset('product_added_key');
        }
        // jQuery Ajax code
        ?>
        <script type="text/javascript">
        jQuery( function($){
            if (typeof wc_checkout_params === 'undefined')
                return false;

            $('form.checkout').on( 'change', '#cb_add_product', function(){
                var value = $(this).prop('checked') === true ? 'yes' : 'no';

                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'add_a_product',
                        'add_a_product': value,
                    },
                    success: function (result) {
                        $('body').trigger('update_checkout');
                        //console.log(result);
                    }
                });
            });
        });
        </script>
        <?php
        endif;
    }

    // The Wordpress Ajax PHP receiver
    add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
    add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
    function checkout_ajax_add_a_product() {
        if ( isset($_POST['add_a_product']) ){
            WC()->session->set('add_a_product', esc_attr($_POST['add_a_product']));
            echo $_POST['add_a_product'];
        }
        die();
    }

    // Add remove free product
    add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
    function adding_removing_specific_product( $cart ) {
        if (is_admin() && !defined('DOING_AJAX'))
            return;

        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;

        // HERE the specific Product ID
        $product_id = 179;

        if( WC()->session->get('add_a_product') == 'yes' && ! WC()->session->get('product_added_key') )
        {

        foreach ( $cart->get_cart() as $cart_item ) {
            $cart_item_key = $cart->add_to_cart( $product_id );
            WC()->session->set('product_added_key', $cart_item_key);

             // get the product id (or the variation id)
             $product_id = $cart_item['data']->get_id();

             // GET THE NEW PRICE (code to be replace by yours)
             $new_price = get_post_meta( $product_id, 'pro_price_extra_info', true );

             // Updated cart item price
             $cart_item['data']->set_price( floatval( $new_price ) );
        }
        }
        elseif( WC()->session->get('add_a_product') == 'no' && WC()->session->get('product_added_key') )
        {
            $cart_item_key = WC()->session->get('product_added_key');
            $cart->remove_cart_item( $cart_item_key );
            WC()->session->__unset('product_added_key');
        }
    }


推荐答案

I得到了我上面的帖子的解决方案。

I got solution for my above post. It might help others in future.

            // Display a custom checkout field
            add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
            function custom_checkbox_checkout_field() {
                $value = WC()->session->get('add_a_product');

                woocommerce_form_field( 'cb_add_product', array(
                    'type'          => 'checkbox',
                    'label'         => '&nbsp;&nbsp;' . __('Please check here to get VIP Membership'),
                    'class'         => array('form-row-wide'),
                ), $value == 'yes' ? true : false );
            }


            // The jQuery Ajax request
            add_action( 'wp_footer', 'checkout_custom_jquery_script' );
            function checkout_custom_jquery_script() {
                // Only checkout page
                if( is_checkout() && ! is_wc_endpoint_url() ):

                // Remove "ship_different" custom WC session on load
                if( WC()->session->get('add_a_product') ){
                    WC()->session->__unset('add_a_product');
                }
                if( WC()->session->get('product_added_key') ){
                    WC()->session->__unset('product_added_key');
                }
                // jQuery Ajax code
                ?>
                <script type="text/javascript">
                jQuery( function($){
                    if (typeof wc_checkout_params === 'undefined')
                        return false;

                    $('form.checkout').on( 'change', '#cb_add_product', function(){
                        var value = $(this).prop('checked') === true ? 'yes' : 'no';

                        $.ajax({
                            type: 'POST',
                            url: wc_checkout_params.ajax_url,
                            data: {
                                'action': 'add_a_product',
                                'add_a_product': value,
                            },
                            success: function (result) {
                                $('body').trigger('update_checkout');
                                //console.log(result);
                            }
                        });

                        if(value == 'no'){
                            window.location.reload();
                        }
                    });
                });
                </script>
                <?php
                endif;
            }

            // The Wordpress Ajax PHP receiver
            add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
            add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
            function checkout_ajax_add_a_product() {
                if ( isset($_POST['add_a_product']) ){
                    WC()->session->set('add_a_product', esc_attr($_POST['add_a_product']));
                    echo $_POST['add_a_product'];
                }
                die();
            }

            // Add remove free product
            add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
            function adding_removing_specific_product( $cart ) {
                if (is_admin() && !defined('DOING_AJAX'))
                    return;

                if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
                    return;

                $has_sub = wcs_user_has_subscription( '', '179', 'active' );

                $product_id = 179;

                if( WC()->session->get('add_a_product') == 'yes' && ! WC()->session->get('product_added_key') ){
                    // HERE the specific Product ID


                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                         $old_price = $cart_item['data']->get_price();

                         if($new_price == NULL){
                            $cart_item['data']->set_price( floatval( $old_price ) );
                         } else {
                            $cart_item['data']->set_price( floatval( $new_price ) );
                         }

                    }

                    $cart_item_key = $cart->add_to_cart( $product_id );
                    WC()->session->set('product_added_key', $cart_item_key);


                } elseif( WC()->session->get('add_a_product') == 'no' && WC()->session->get('product_added_key') ) {

                    $cart_item_key = WC()->session->get('product_added_key');
                    $cart->remove_cart_item( $cart_item_key );      
                    WC()->session->__unset('product_added_key');        

                }elseif ( $has_sub ) {
                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                            $cart_item['data']->set_price( floatval( $new_price ) );

                    }
                }

            }
            function woo_in_cart($product_id) {
                global $woocommerce;         
                foreach($woocommerce->cart->get_cart() as $key => $val ) {
                    $_product = $val['data'];

                    if($product_id == $_product->id ) {
                        return true;
                    }
                }         
                return false;
            }
            add_action( 'woocommerce_cart_loaded_from_session', 'adding_vip_product' );
            function adding_vip_product( $cart ) {
                $product_id = 179;
                if(woo_in_cart($product_id) ) {

                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                         if($new_price != 0){
                            $cart_item['data']->set_price( floatval( $new_price ) );
                         }

                    }

                }
            }

这篇关于复选框字段,用于添加订阅产品并更改其他产品的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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