Posts

Showing posts with the label Wordpress

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( '...

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...

ABSPATH Or __FILE__?

Answer : I would personally prefer dirname() as it is always guaranteed to give me the correct result, while the ABSPATH method relies on a fixed theme path and theme name that can both change. By the way, you can use __DIR__ instead of dirname(__FILE__) . The path to the "wp-content" directory and its subdirectories can be different in a particular WordPress installation. Also, using the WordPress internal constants (such as ABSPATH ) is not recommended. See the Determining Plugin and Content Directories WordPress Codex article. Since PHP 4.0.2, symlinks are being resolved for the __FILE__ and __DIR__ magic constants, so take that into account. Bottom line : To determine the absolute path to a theme directory, I would suggest to use the get_template_directory() function which also applies filters and internally combines get_theme_root() and get_template() . For my own projects I would choose dirname(__FILE__) , also there is a new constant in PHP: __DIR__...

Create Programmatically A WooCommerce Product Variation With New Attribute Values

Image
Answer : Update January 2020: Changed to WC_Product method get_name() instead of get_title() Update September 2018: Handling taxonomy creation (Thanks to Carl F. Corneil) From a defined variable product ID You will find below, a custom function that will add (create) a Product variation. The variable parent product needs to have set for it the needed attributes. You will need to provide some information as: the array of attributes/values the Sku, prices and stock…. This data has to be stored in a formatted multi dimensional array (see an example at the end) . This function will check if the attributes values (term name) already exist and if not: it create it for the product attribute set it in the parent variable product. The custom function code: /** * Create a product variation for a defined variable product ID. * * @since 3.0.0 * @param int $product_id | Post ID of the product parent variable product. * @param array $variation_data | The data to insert in the produ...

Adding Admin Menu Separators In WordPress

Answer : You should hook in admin_menu : add_action('admin_menu','admin_menu_separator'); And use something lower than 220 . The biggest offset I got in my system is 99 . Check this very fine class to deal with Admin Menus. It appeared in this WPSE Question: Add a Separator to the Admin Menu?

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...

Create An Admin User Programmatically In WordPress

Answer : This will create an admin user if placed in a themes functions.php file. Please change the first three variables as needed. /* * Create an admin user silently */ add_action('init', 'xyz1234_my_custom_add_user'); function xyz1234_my_custom_add_user() { $username = 'username123'; $password = 'pasword123'; $email = 'drew@example.com'; if (username_exists($username) == null && email_exists($email) == false) { // Create the new user $user_id = wp_create_user($username, $password, $email); // Get current user object $user = get_user_by('id', $user_id); // Remove role $user->remove_role('subscriber'); // Add role $user->add_role('administrator'); } } Accepted answer has issues and will throw a fatal error when its run twice, because the $user_id will be empty the second time. A work around for the issue: function rndprfx...

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() &...