Posts

Showing posts with the label Woocommerce

Add A Custom Stock Status In WooCommerce

Answer : for anyone interested, here is complete solution, based on Laila's approach. Warning! My solution is intended to work only with WooCommerce "manage stock" option disabled ! I am not working with exact amounts of items in stock. All code goes to functions.php , as usual. Back-end part Removing native stock status dropdown field. Adding CSS class to distinguish my new custom field. Dropdown has now new option "On Request". function add_custom_stock_type() { ?> <script type="text/javascript"> jQuery(function(){ jQuery('._stock_status_field').not('.custom-stock-status').remove(); }); </script> <?php woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array( '...

Add A Discount Programmatically To An Order In Woocommerce 3.2+

Image
Answer : The only available feature to make a discount programmatically for an Order , is tricking the Fee API . For info, this trick is not recommended by woocommerce, but used by many people as there is not a discount feature in Woocommerce outside Coupons. The following function will allow you to make a fixed discount of any amount or a percentage discount. The order need to exist (to be saved before) . The function code (For Woocommerce versions 3.2+) : /** * Add a discount to an Orders programmatically * (Using the FEE API - A negative fee) * * @since 3.2.0 * @param int $order_id The order ID. Required. * @param string $title The label name for the discount. Required. * @param mixed $amount Fixed amount (float) or percentage based on the subtotal. Required. * @param string $tax_class The tax Class. '' by default. Optional. */ function wc_order_add_discount( $order_id, $title, $amount, $tax_class = '' ) { $order = wc_ge...

Change Cart Total Price In WooCommerce

Image
Answer : This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off: function prefix_add_discount_line( $cart ) { $discount = $cart->subtotal * 0.1; $cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount ); } add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' ); Since Woocommerce 3.2+ it does not work anymore with the new Class WC_Cart_Totals ... New answer: Change Cart total using Hooks in Woocommerce 3.2+ First woocommerce_cart_total hook is a filter hook, not an action hook. Also as wc_price argument in woocommerce_cart_total is the formatted price , you will not be able to increase it by 10%. That's why it returns zero. Before Woocommerce v3.2 it works as some WC_Cart properties can be accessed directly You should better use a custom function hooked in woocommerce_calculate_totals action hook this w...

Add A Quantity Field To Ajax Add To Cart Button On WooCommerce Shop Page

Answer : Updated on May 2020 For WooCommerce versions from 3.2 to 4+, Optimized jQuery code and Removed a quantity bug. The following custom function is hooked in woocommerce_loop_add_to_cart_link filter hook and adds a quantity input field to each product on WooCommerce archives pages and other product loops. We use here mostly the original WooCommerce code. A bit of jQuery code is necessary to update the data-quantity attribute on the add to cart button when customer changes the quantity. Some styling might be needed, depending on your client wishes (and on your theme) . An additional section to hide the "View cart" button is located at the end. The code: add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 ); function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) { if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() &...

Ajax Add To Cart Button For Product Variation In WooCommerce 3

Answer : To make it work I use a custom ajax add-to-cart for product variations exclusively. 1). I have first changed a bit your button html: <div class="btnss"> <span class="price"> <span class="woocommerce-Price-amount amount">6,999&nbsp; <span class="woocommerce-Price-currencySymbol">kr</span> </span> </span> <div class="quantity buttons_added"> <input type="button" value="-" class="minus"> <label class="screen-reader-text" for="quantity_5b101f605f067">Quantity</label> <input type="number" id="quantity_5b101f605f067" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Qty" size="4" pattern="[0-9]*" inputm...