URL to product page with quantity inputed - WooCommerce - wordpress

I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.

you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.

Related

In-store only products WooCommerce (WordPress)

I'm using WordPress with WooCommerce to sell physical products to customers online.
I would like to have either specific products or rather a specific category of products as "available in-store only" thus not displaying the "add to cart" button for those products, forcing the customers to get in touch with me or to physically come into my store to buy those products.
Some of these products are using variations (colors, options, etc) with different prices so I need to keep those products as "variable products".
Would love to have ideas / solutions to this as I can't find any. Thanks!
You can check for category slug or id, and filter woocommerce_is_purchasable like so:
function BN_restrict_store_only( $purchasable, $product ){
//The category by slug
$pickup_prod_cat_slug = 'available-in-store-only'; // slug
//The category by id
$pickup_prod_cat_ids ='681'; // the id of the product cat
// For variations (
if ( $product->is_type('variation') ) {
$parent = wc_get_product( $product->get_parent_id() );
$product_id = $parent->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
// For simple and other product types
else {
$product_id = $product->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'BN_restrict_store_only', 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'BN_restrict_store_only', 10, 2 );
Works both with simple products and variations.
You have to make a category with the slug "available-in-store-only" or get the id/slug of whatever product cat you want to use.
File goes in the functions.php of your child theme, or CodeSnippets.

woocommerce change product permalink to products

I have at the moment url in product view: https://mywebsite.com/product/product-name
Shop url looks like this: https://mywebsite.com/shop
I need to change product view url like https://mywebsite.com/products/product-name
I didnt find any settings for that.
I tried this, It is working fine for me
let me know your thoughts..
function change_post_types_slug( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['rewrite']['slug'] = 'products';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
Permalink structure like the following screenshot will give the intended result.
Link to image: https://snipboard.io/xGJWK6.jpg

Replacing variable product pricing in WooCommerce

I want to remove the price range that is shown for variable products in the WooCommerce site I'm working on. On non-product pages I want to replace it with "from: [lowest price]" and on the product page I'd like to replace it simply with the price of the selected variation.
Any ideas how I can get it working that way?
Thanks,
Darren
The best possible way to do what you want is that you change it on non-product page and remove it from product page. On product page when you select a variaion, its price and remaining stock is shown below the dropdown. So there is no need for you to show it above. If you still want to do it you will need a js solution.
Try this, this is tested solution. I have commented the cases for better guidance and changing if you want.
function sr_change_variable_price($price, $product)
{
if ( $product->is_type( 'variable' ) && !is_product() )
{
return 'From: '.$product->get_variation_price( 'min' ); // if variable product and non-product page
} else if ( $product->is_type( 'variable' ) && is_product() )
{
return ''; // if variable product and product page
} else
{
return $price; // other cases
}
}
add_filter( 'woocommerce_get_price_html', 'sr_change_variable_price', 10, 2 );

Woocommerce add to cart with custom price

I've seen many examples of adding an item to the WC cart with a customer price, but none dynamically. I am trying to do in a shortcode function that receives a POST variables....
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$cart_object = $woocommerce->cart;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$cart_object->add_to_cart($target_product_id, "1");
foreach ( $cart_object->cart_contents as $key => $value ) {
if ( $value['product_id'] == $target_product_id ) {
$value['data']->price = $custom_price;
}
}
}
This adds the item to the cart of course, but the price is zero and I realize I need to somehow save this array back to the WC cart data. Is this method even possible or can it only be done via a filter or action hook? If so, how can I save the changed array back to the cart contents or make it work to add the one item with its posted price? Any guidance greatly appreciated.
Thanks for that answer doublesharp, I was not able to get it to work as described because the form was posting to the page with my shortcode, which has my form, instead of posting directly to the cart. The $_POST was not being seen and the product ended up zero. I did find another approach, but having a problem using wp_redirect. I changed the above shortcode to this:
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$_SESSION['donation_amount'] = $custom_price;
$woocommerce->cart->add_to_cart($target_product_id, "1");
wp_redirect( site_url() . '/gifts/swag-bag/');
}
Then I added the following filters to functions.php:
add_filter('woocommerce_get_price','donation_price', 10, 2);
add_filter('woocommerce_get_regular_price','donation_price', 10, 2);
add_filter('woocommerce_get_sale_price','donation_price', 10, 2);
function donation_price($price, $productd){
if($productd->id == '65986'){
$price = $_SESSION['donation_amount'];
}
return $price;
}
This does not work except when wp_redirect is commented out, hence, not redirecting. The above redirects to the cart, but its empty. If I comment out the wp_redirect line and then manually go to the cart, the product is there with my custom price. Actually, I would like to apply a custom price and redirect directly to the checkout page instead of the cart, if possible?
You can use the woocommerce_before_calculate_totals action hook to modify the contents of the cart, including the product prices.
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( $item['id'] == 65986 && isset( $_POST['donation_amount'] ) ){
// custom price from POST
$custom_price = $_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0;
// save to the cart data
$item['data']->price = $custom_price;
// new versions of WooCommerce may require (instead of line above)...
// $item['data']->set_price($custom_price);
}
}
}

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources