cart

Change cart item prices in Woocommerce 3

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 19:06:45
I am trying to change product price in cart using the following function: add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price' ); function add_custom_price( $cart_object ) { foreach ( $cart_object->cart_contents as $key => $value ) { $value['data']->price = 400; } } It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+ How can I make it work in WooCommerce Version 3.0+? Thanks. Update (September 2018) With WooCommerce version 3.0+ you need: To use woocommerce_before_calculate_totals hook instead. To use WC_Cart get_cart() method

Need Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another 1 is added then it should remove the previous 1

独自空忆成欢 提交于 2019-11-26 17:43:06
问题 I think this code should work but not exactly sure where to place it. Everywhere I have tried has failed so far... add_action('init', 'woocommerce_clear_cart'); function woocommerce_clear_cart() { global $woocommerce, $post, $wpdb; $url = explode('/', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); $slug=$url[4]; $postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name = '$slug'"); if ($postid){ if ($postid == PRODUCTID1 || $postid ==

Saving a product custom field and displaying it in cart page

筅森魡賤 提交于 2019-11-26 17:08:24
问题 With WooCommerce and I am doing some customization in my functions.php file, to get a custom field value on Cart page. I add a custom field before add to cart: function add_name_on_tshirt_field() { echo '<table class="variations" cellspacing="0"> <tbody> <tr> <td class="label"><label for="color">Name On T-Shirt</label></td> <td class="value"> <input type="text" name="name-on-tshirt" value="" /> </td> </tr> </tbody> </table>'; } add_action( 'woocommerce_before_add_to_cart_button', 'add_name_on

Hide shipping methods for specific shipping class in WooCommerce

这一生的挚爱 提交于 2019-11-26 11:32:30
问题 Essentially I\'m trying to make the flat rate method Id flat_rate:7 disabled when there is cart items that have the shipping class \"Roller\" (ID 92 ). This is the code I tried: add_filter(\'woocommerce_package_rates\', \'wf_hide_shipping_method_based_on_shipping_class\', 10, 2); function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package) { $hide_when_shipping_class_exist = array( 92 => array( \'flat_rate:7\' ) ); $shipping_class_in_cart = array(); foreach

Get in WooCommerce cart the product ID of a cart item

自古美人都是妖i 提交于 2019-11-26 08:34:52
问题 $cart_item = $woocommerce->cart->get_cart(); I have the above code. if I run print_r on cart_item I get a multi dimensional array: Array( [a6292668b36ef412fa3c4102d1311a62] => Array ( [product_id] => 6803 How do I get the product_id only? I tried $test = $cart_item[\'data\']; print_r($test); Didn\'t work. 回答1: To get the product ID of each cart item in the foreach loop (for a simple product): foreach( WC()->cart->get_cart() as $cart_item ){ $product_id = $cart_item['product_id']; } If it's a

Change cart item prices in Woocommerce 3

家住魔仙堡 提交于 2019-11-26 04:28:13
问题 I am trying to change product price in cart using the following function: add_action( \'woocommerce_before_shipping_calculator\', \'add_custom_price\' ); function add_custom_price( $cart_object ) { foreach ( $cart_object->cart_contents as $key => $value ) { $value[\'data\']->price = 400; } } It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+ How can I make it work in WooCommerce Version 3.0+? Thanks. 回答1: Update (September 2018) With WooCommerce