S
S
Sergey2020-04-26 17:55:21
WordPress
Sergey, 2020-04-26 17:55:21

How to display Woocommerce product attributes?

Greetings! How to get an array with product attributes on the checkout page? To further compare it and prohibit payment for goods with a certain attribute.

Task: if there are goods in the cart with the attribute color=red, then hide the PayPal payment method.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2020-04-26
@GraF1N

I'm trying to do this:


add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$prod_variable = $prod_simple = $prod_subscription = false;
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if($product->get_attributes('color') == 'red') $prod_simple = true;
}
// Remove Cash on delivery (cod) payment gateway for simple products
if($prod_simple)
unset($available_gateways['cod1']); // unset 'cod'
// Remove Paypal (paypal) payment gateway for variable products
return $available_gateways;
}

Doesn't work, it looks like I'm not reading the product attribute correctly
if($product->get_attributes('color') == 'red') $prod_simple = true;
Tell me how is it right?

D
denkharkov77, 2020-04-26
@denkharkov77

Maybe not quite right, but...
https://webreason.ru/vyvod-atributov-tovara-woocom...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question