php - 自定义 Woocommerce 电子邮件:获取产品缩略图和价格
问题描述
我最近刚刚为 Woocommerce 创建了自定义电子邮件。除了一个问题,我一切正常。在我带来/获取“产品图片”之前,我能够毫无问题地加载“产品价格”。
但是现在我正在获取产品图片,我无法获取“产品价格”并且订单未处理和完成。
这是我用来收集要在电子邮件中显示的所有信息的 PHP 代码。
<?php
foreach ($order->get_items() as $theitem_id => $theitem ) {
// PRODUCT NAME
$product_name = $theitem->get_name();
// PRODUCT QUANTITY
$quantity = $theitem->get_quantity();
// LINE ITEM SUBTOTAL (Non discounted)
$item_subtotal = $theitem->get_subtotal();
$item_subtotal = number_format( $item_subtotal, 2 );
// LINE ITEM TOTAL (discounted)
$item_total = $theitem->get_total();
$item_total = number_format( $item_total, 2 );
$product_id = $theitem['product_id'];
$product = wc_get_product( $product_id );
// PRODUCT IMAGE
$prodimage = $product->get_image( array( 200, 200 ) )
// PRODUCT PRICE
$product_price = $product->get_price();
?>
我敢肯定,当我注释掉“产品价格”PHP 代码时,我离正确的做法不远了,然后一切正常(显然没有收集到价格)
任何帮助都会很棒,非常感谢
解决方案
从 Woocommerce 3 开始,产品 ID 是错误 的(产品对象也是错误的) ......尝试改为:
<?php
// Loop through order items
foreach ($order->get_items() as $item_id => $item ) {
// PRODUCT NAME
$product_name = $item->get_name();
// PRODUCT QUANTITY
$quantity = $item->get_quantity();
// LINE ITEM SUBTOTAL (Non discounted)
$item_subtotal = $item->get_subtotal();
$item_subtotal = number_format( $item_subtotal, 2 );
// LINE ITEM TOTAL (discounted)
$item_total = $item->get_total();
$item_total = number_format( $item_total, 2 );
// Get an instance of the product Object
$product = $item->get_product(); // <==== HERE
$product_id = $product->get_id(); // <==== and HERE
// PRODUCT IMAGE
$product_image = $product->get_image( array( 200, 200 ) );
// PRODUCT PRICE
$product_price = $product->get_price();
?>
它现在应该更好地工作。
推荐阅读
- jenkins - 如何通过 Jenkins 更新 JIRA 中的问题状态?
- ios - 使用 Firebase 跟踪用户在我的 iOS 应用中所做操作的最佳方式
- python-3.x - 属性错误:
python 中的 .Senton 错误 - Outlook - php - PHP Laravel 5.8 Auth Attempt 在 PHP 72 中总是返回 false
- python - Python 3 os.rename 将重命名文件,但不重命名文件夹
- python - 如何使用相同的代码读取多个文档?
- python - 使用递归传递 lambda
- c# - 如何在使用 c# 和 System.IO.Compression 压缩 zip 文件时取得进展
- swiftui - 如何检测 iOS 13 中的 Light\Dark 模式变化?
- node.js - 节点异步方法理解