Php 将变体id更改为产品属性名称
我希望通过使用产品属性的名称而不是id变体来重构代码 根据该代码:Php 将变体id更改为产品属性名称,php,wordpress,woocommerce,hook,hook-woocommerce,Php,Wordpress,Woocommerce,Hook,Hook Woocommerce,我希望通过使用产品属性的名称而不是id变体来重构代码 根据该代码: function action_woocommerce_thankyou( $order_id ) { // Get $order object $order = wc_get_order( $order_id ); // Get items $items = $order->get_items(); // Set variable $found = false;
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Get items
$items = $order->get_items();
// Set variable
$found = false;
// Set variable
$output = '';
// Loop
foreach ( $items as $item ) {
// Add whatever variation id you want below here.
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
$output = 'Thank you for buy VARIABLE A-9647';
$found = true;
break;
}
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
$output = 'Thank you for buy VARIABLE B-9648';
$found = true;
break;
}
}
// Get payment method
$payment_method = $order->get_payment_method();
// Payment method = basc & found = true
if ( $payment_method == 'bacs' && $found ) {
$output .= ' YOUR PAYMENT IS BACS';
}
// Print result
echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
我希望通过使用产品属性的名称而不是id变体来重构代码 如何更改此代码 来自
variation_id
至
product name attribute
更确切地说,这些行:
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 )
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 )
提前谢谢 假设你是这个意思
function action\u woodcommerce\u thankyou($order\u id){
//获取$order对象
$order=wc\u get\u order($order\u id);
//获取项目
$items=$order->get_items();
//设置变量
$found=false;
//设置变量
$output='';
//环路
foreach($items作为$item){
//WC_产品对象
$product=$item->get_product();
//在下面添加您想要的任何属性。
如果(!empty($product->get_属性('pa_kleur')){
$output='谢谢你…1';
$found=true;
打破
}
如果(!empty($product->get_属性('pa_jaar')){
$output='谢谢你…2';
$found=true;
打破
}
}
//获取付款方式
$payment_method=$order->get_payment_method();
//付款方式=basc&found=真
如果($payment_method=='bacs'&&&$found){
$output.='您的付款是BACS';
}
//打印结果
echo$输出;
}
增加行动('woocommerce\u Thankyu','action\u woocommerce\u Thankyu',10,1);