Shopify metafield product add to cart - button

I created a custom metafild in shopify to add a suggested product to the product page.
I have added the code to my product template and I am correctly displaying the suggested product.
I want to add the add to cart button for this suggested product so that the user can add the suggested product to the cart without entering the specific page.
<div class = "groups-btn">
{% if current_variant.available%}
<input data-btn-addToCart type = "submit" name = "add" class = "btn" id = "product-add-to-cart" value = "add" data-form-id = "# add-to- cart-form ">
{% endif%}
</div>
With this code, however, I add the main product (the one on the page) to the cart, not the suggested product.
How can I solve my problem?
Thanks

Adding products to the cart requires you to POST the ID of the variant and a quantity. Wire up the add to cart button for your secondary product to be an Ajax call and you're good to go.

Related

How can i add a custom link field under the add to cart button?

I would like to add a custom link under add to cart for each product and I'm not sure how to do this. Each of my products has a PDF file and i would like to have the option available when i edit products so I can add the link to them. I don't want to ad it in the description just under the add to cart button.
You can use the woocommerce_after_add_to_cart_button hook to place content below the add to cart button:
function add_content_below_cart_button( ) {
// do something to add your link
};
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_below_cart_button', 10, 0 );

WooCommerce Bookings: Triggering AJAX Recalculation of Booking Cost on Product Page

I've added a couple new fields to the date-picker.php template in WooCommerce Bookings and need to trigger the AJAX recalculation of the booking cost on the product page when these fields are updated.
Currently this recalculation is triggered when the standard fields in date-picker.php are updated. However, I will be using the additional fields to recalculate the duration and booking cost so I need this AJAX recalculation triggered when updates are made to the new fields as well.
How can I trigger the AJAX recalculation? I have tried simply triggering a change event on one of the standard fields, but that doesn't seem to work.
I had the same issue. If it helps someone in the future, heres my work around :
Go to
/wp-content/plugins/woocommerce-bookings/templates/single-product/add-to-cart/booking.php
This is the template for the single product booking form.
You can then move one of the actions such as <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?> (or create your own action) within the <div id="wc-bookings-booking-form"/>.
Now add a custom field or code to this action (I used wc-fields-factory). When you change a field value within the booking form the AJAX recalculation is triggered.
You can hook into this trigger in your functions.php file (within your theme) to alter the cost based on the custom fields you have added like so :
add_filter( 'booking_form_calculated_booking_cost', 'my_booking_cost', 10, 3 );
function my_booking_cost( $booking_cost, $booking, $posted ) {
/* use fields here to show a new booking cost*/
$my_time = $posted[ <name of custom field> ]; //access field within booking form
$booking_cost = 1; //whatever your calc is
return $booking_cost;
}

Wordpress / WooCommerce - dynamic hyperlink based on current product

I'm running WooCommerce in Wordpress here and on some products, I will be embedding a block of text which includes a hyperlink to a contact form. I would like the Subject on the contact form to populate with the name of the product where the hyperlink was clicked
I have established that I can populate a CF7 field by appending a query string to the URL (e.g. www...co.uk/contact-form/?subject=blah,blah), so if there was some method of dynamically altering the hyperlink in the aforementioned block of text when the product page is loaded so as to append the product name after the query string
Maybe to describe this a little clearer...
The block of text on the product page will look like this:
<a href='...co.uk/contact-form/?subject={NEED-TO-DYNAMICALLY-APPEND-PRODUCT-NAME-HERE}>Contact us...</a>
Any ideas folks of how I might approach this or has anyone tried something similar?
Thanks
So you want the title of product name ?
$product = wc_get_product( id );
$product_name = $product->get_title();
<a href='...co.uk/contact-form/?subject=<?php echo $product_name; ?>'>Contact us...</a>
If you want the title of the page you can give this parameter to your href
$pageTitle = get_the_title();

to add "Add to cart" button at product search result

I m using the woocommerce, and at that i want to insert the add to cart button when the search result are display. Product are displayed bt with the product name ,"Add to cart" button to available with every product at search result.
Add to cart
use above code with your custom product id.For more details refer the link
https://businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/
Use this:
<?php $id = get_the_ID(); ?>
Add to cart

Using Woocommerce Checkout for Quotes Only

How would go about using woocommerce without pricing but keeping the cart functionality? Id like the ability to add products to cart (changing button to to say add to quote) and then when checking out instead of going to payment gateway it would instead submit all the items in the cart for a quote. Once I receive the order I would contact the customer with a quote. You can edit an order in the admin so technically I could change the 0 cost items to the quoted prices and then notify the customer that their order/quote has been updated.
Could I just make all items have 0 cost and hide the prices on the front end ?
Set a price of '0' so the 'add to cart' buttons still show, but in your templates hide the price fields, and change the 'add to cart' labels to whatever is appropriate, so 'add to quote' or 'add to shortlist' or whatever you're using the cart for.
If you still want the checkout function disable all payment options except for 'cash on delivery', and change the title of this payment option to 'No Payment Required For Quotes' or similar. No payment is then required, yet the customer can create orders without paying for them
It takes some digging to find all the right filters in WooCommerce to make this happen but once you find them, it's pretty straightforward. These are the various snippets I'm using to change "Cart" language to "Quote" and streamline the checkout process:
add_filter('woocommerce_product_single_add_to_cart_text', 'rental_single_product_add_to_cart',10,2);
add_filter('woocommerce_product_add_to_cart_text', 'rental_single_product_add_to_cart',10,2);
function rental_single_product_add_to_cart( $title,$product ) {
return 'Add to Quote';
}
add_action('woocommerce_widget_shopping_cart_before_buttons', 'rental_before_mini_cart_checkout',10);
function rental_before_mini_cart_checkout(){
//change buttons in the flyout cart
echo '
<p class="buttons" style="display: block;">
Submit for Quote
</p>
';
}
add_filter('woocommerce_billing_fields','rental_billing_fields',10,1);
function rental_billing_fields($fields){
unset($fields['billing_country']);
unset($fields['billing_address_1']);
unset($fields['billing_address_2']);
unset($fields['billing_city']);
unset($fields['billing_state']);
unset($fields['billing_postcode']);
return $fields;
}
add_filter('woocommerce_checkout_fields','rental_checkout_fields',10,1);
function rental_checkout_fields($fields){
//change comment field labels
$fields['order']['order_comments']['label'] = 'Notes';
return $fields;
}
add_filter('woocommerce_order_button_text','rental_order_button_text',10,1);
function rental_order_button_text($text){
return 'Submit to Request Confirmed Quote';
}
This plus the suggestion from /u/crdunst regarding payment methods should make switching to a Quote submission a breeze!

Resources