Hi @scotlandtravel
To apply a discount based on the transfer type, you can use the woocommerce_before_calculate_totals
hook to modify the cart total before it’s calculated.
Although writing or providing custom code is not within the scope of our support policy, here is an example for better understanding:
function apply_discount_based_on_transfer_type( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$transfer_type = $product->get_attribute('pa_transfer-type'); // Replace with your actual attribute name
if( $transfer_type == 'return_new_ride' ) {
// Apply WooCommerce coupon code
$coupon_code = 'your_coupon_code'; // Replace with your actual coupon code
if ( ! WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->add_discount( $coupon_code );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'apply_discount_based_on_transfer_type', 10, 1 );
Also if possible I don’t know which line of should I copy from inspect element, if you can just help me find its class on cart page
You can right-click on the element and select “Inspect” or “Inspect Element” from the context menu. This will open the browser’s developer tools and highlight the code for the selected element. The class of the element would be listed within the opening tag of the highlighted code.
f you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:
I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.