In WooCommerce, I have a Variable Product
type product with ID 1010
. This product has an attribute Name: "book terms
" with two defined product types Value(s):
phys (best)
"version (limit)
"I want to write a code in function.php
that when a customer enters the current order page they have purchased at /my-account/view-order/
in their user panel (Order details), the same page where the product is purchased, the product description is displayed on that page (for example, if the order number is 911, the address will be /my-account/view-order/911/
).
First, it should be checked whether the current order that the customer is in the details page of has a product with ID 1010
. If the product with ID 1010
is in this order, it should be checked which attributes of this product ("phys (best)
" and "version (limit)
") have been purchased and the value of these attributes should be displayed along with the current order number. I should also be able to check the values inside these attributes ("phys (best)
" and "version (limit)"
) using a conditional statement.
The customer may have purchased more than one copy of this product with ID 1010
, so these items should be separated from each other (for example, the customer may have purchased 10 copies of this book or product with ID 1010
, of which 2 copies are "version (limit)
", 5 copies are "phys (best)
" and 3 copies are both "phys (best)" and "version (limit)
" together).
The customer may have purchased both attributes of the product with ID 1010
or only one of them, so this should also be separated.
I want all these conditions to be done in the following source code and in <h1>THIS SECTION</h1>
:
function detect_book_type($productId){
$current_user = wp_get_current_user();
$current_user_email = $current_user->user_email;
switch($productId):
<?php case "1": ?>
<label>product number 1 : </label>
<input type="text" value="<?php echo something; ?>" readonly>
<?php break; ?>
<?php case "2": ?>
<label>product number 2 : </label>
<input type="text" value="<?php echo something; ?>" readonly>
<?php break; ?>
<?php case "1010": ?>
<label>Book address is : </label>
<h1>THIS SECTION</h1>
<?php break; ?>
<?php default : ?>
<p>There seems to be a problem...!</p>
<?php endswitch;
}
I wrote the following source code, but it has two problems:
If a customer purchases both attributes of a product with ID 1010
(i.e., "phys (best)
" and "version (limit)
"), only the values related to "phys (best)
" are displayed repeatedly on the view-order
page of their account. Instead of showing both attributes, only one attribute is displayed twice.
In the line $product_variation_id = $item->get_variation_id();
, when I print_r
the value of $product_variation_id
, it always contains only one value (only one of the two values "phys (best)
" and "version (limit)
"). Even though an order may include product 1010 with two attributes, only one value is stored in this variable!
function detect_book_type($productId){
$current_user = wp_get_current_user();
$current_user_email = $current_user->user_email;
switch($productId):
<?php case "1": ?>
<label>product number 1 : </label>
<input type="text" value="<?php echo something; ?>" readonly>
<?php break; ?>
<?php case "2": ?>
<label>product number 2 : </label>
<input type="text" value="<?php echo something; ?>" readonly>
<?php break; ?>
<?php case "1010": ?>
<label>Book address is : </label>
<h1>THIS SECTION</h1>
<?php
$current_order_id = get_query_var('view-order');
$order = wc_get_order( $current_order_id );
$items = $order->get_items();
$product_exists = false;
$product_variation_id = null;
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( $product_id == 1010 ) {
$product_exists = true;
$product_variation_id = $item->get_variation_id();
break;
}
}
if ( $product_exists ) {
$product_attributes = wc_get_product_variation_attributes( $product_variation_id );
$product_attributes = $product_attributes['attribute_book-terms'];
if ( $product_attributes === "phys (best)" ) {
$run_phys_books_best = run_phys_books_func();
?>
<label>sample text phys:</label>
<input type="text" value="<?php echo $run_phys_books_best; ?>" readonly>
<?php
}
elseif ( $product_attributes === "version (limit)" ) {
$run_limited_version = run_limit_func();
?>
<label>sample text limit:</label>
<input type="text" value="<?php echo $run_limited_version; ?>" readonly>
<?php
}
elseif ( ($product_attributes === "phys (best)" ) && ( $product_attributes === "version (limit)") ) {
if ( $product_attributes === "phys (best)" ) {
$run_phys_books_best = run_phys_books_func();
?>
<label>sample text phys:</label>
<input type="text" value="<?php echo $run_phys_books_best; ?>" readonly>
<?php
}
if ( $product_attributes === "version (limit)" ) {
$run_limited_version = run_limit_func();
?>
<label>sample text limit:</label>
<input type="text" value="<?php echo $run_limited_version; ?>" readonly>
<?php
}
}
} else {
echo 'nothing!';
}
?>
<?php break; ?>
<?php default : ?>
<p>There seems to be a problem...!</p>
<?php endswitch;
}
There are some mistakes in your code, try the following revised function code:
function detect_book_type( $product_id ){
$current_user = wp_get_current_user();
$current_user_email = $current_user->user_email;
switch( $product_id ):
case '1':
printf('<label>%s : </label>
<input type="text" value="%s" readonly>',
__('product number 1'), __('something (product 1)') );
break;
case '2':
printf('<label>%s : </label>
<input type="text" value="%s" readonly>',
__('product number 2'), __('something (product 2)') );
break;
case '1010':
printf('<label>%s : </label><h1>%s</h1>',
__('Book address is'), __('THIS SECTION') );
$order_id = absint( get_query_var('view-order') );
$order = wc_get_order( $order_id );
$order_items = is_a($order, 'WC_Order') ? $order->get_items() : array();
foreach ( $order_items as $item ) {
if ( $item->get_product_id() != $product_id )
continue;
$variation = $item->get_product();
switch( $variation->get_attribute('book terms') ):
case 'phys (best)':
printf('<label>%s:</label>
<input type="text" value="%s" readonly>',
__('sample text phys'), run_phys_books_func() );
break;
case 'version (limit)':
printf('<label>%s:</label>
<input type="text" value="%s" readonly>',
__('sample text limit'), run_limit_func() );
break;
endswitch;
break; // <== Added (update)
}
break;
endswitch;
}
It should work.