Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1302349
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 questionI 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 questionI 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
1 Answer
Reset to default 1Got it to work using woocommerce_before_calculate_totals
hook
本文标签: WooCommerce adding product to cart programatically
版权声明:本文标题:WooCommerce adding product to cart programatically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741709746a2393769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论