最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce adding product to cart programatically

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have checkbox that when clicked should add a product to the cart ans when unclicked should remove it. Developer before me solved the problem using Fee's which should be changed to product. He used

add_action( 'woocommerce_cart_calculate_fees', 'checkout_radio_choice_product', 20, 1 );

I think I got the code to work I am just struggling to find the right hook. woocommerce_add_to_cart is not the right one. Can anyone point me in the right direction?

function checkout_radio_choice_product()
{  
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $mp = WC()->session->get( 'magic_product' ); //true or false
    
    if( $mp) {
         $product_id = 11446;

        $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 );
        }
    }
    else {
        WC()->cart->remove_cart_item( $product_id);
    }
}
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have checkbox that when clicked should add a product to the cart ans when unclicked should remove it. Developer before me solved the problem using Fee's which should be changed to product. He used

add_action( 'woocommerce_cart_calculate_fees', 'checkout_radio_choice_product', 20, 1 );

I think I got the code to work I am just struggling to find the right hook. woocommerce_add_to_cart is not the right one. Can anyone point me in the right direction?

function checkout_radio_choice_product()
{  
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $mp = WC()->session->get( 'magic_product' ); //true or false
    
    if( $mp) {
         $product_id = 11446;

        $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 );
        }
    }
    else {
        WC()->cart->remove_cart_item( $product_id);
    }
}
Share Improve this question edited Mar 3, 2021 at 11:33 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Mar 3, 2021 at 9:41 user202769user202769 112 bronze badges 1
  • 3rd party plugin/theme dev support is off topic here, you should ask this in the official support routes or in a WooCommerce community – Tom J Nowell Commented Mar 3, 2021 at 11:34
Add a comment  | 

1 Answer 1

Reset to default 1

Got it to work using woocommerce_before_calculate_totals hook

发布评论

评论列表(0)

  1. 暂无评论