Lia*_*hur 3 php wordpress checkout opayo woocommerce
我正在尝试修复这个不受支持的 SagePay 插件中的一些已弃用的功能。
如何替换 WooCommerce 中以下已弃用的代码片段?
foreach ($order->get_items() as $item) {
if ($item['qty']) {
$product = $order->get_product_from_item($item);
Run Code Online (Sandbox Code Playgroud)
我可以看到这是它的替代品:
@deprecated 在未来版本中添加弃用通知。替换为
$item->get_product()
但仅仅改变它是$product = $item->get_product();行不通的。我也尝试将该行更改为:
$product = wc_get_product($item['id']);
但它会在结帐过程中导致内部服务器错误。
您可以这样使用WC_data get_data()方法WC_Order和对象:WC_Order_Items
// For testing only
$order = wc_get_order(500);
// Iterate though each order item
foreach ($order->get_items() as $item_id => $item) {
// Get the WC_Order_Item_Product object properties in an array
$item_data = $item->get_data();
if ($item['quantity'] > 0) {
// get the WC_Product object
$product = wc_get_product($item['product_id']);
// test output
print_r($product);
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用以下WC_Order_Item_Product get_product_id()方法:
// For testing only
$order = wc_get_order(500);
// Iterate though each order item
foreach ($order->get_items() as $item_id => $item) {
if( $item->get_quantity() > 0 ){
// Get the product id from WC_Order_Item_Product object
$product_id = $item->get_product_id();
// get the WC_Product object
$product = wc_get_product($item['product_id']);
// test output
print_r($product);
}
}
Run Code Online (Sandbox Code Playgroud)
所有这些都在活动主题 php 文件内进行。
在主题 php 文件中测试时,我可以
WC_Product使用以下命令获取对象$item->get_product();
| 归档时间: |
|
| 查看次数: |
6535 次 |
| 最近记录: |