由于我们可以为订单中的每个产品应用不同的优惠券,
有什么方法可以知道哪些优惠券适用于哪些产品?
我用过$order->get_used_coupons()
函数,但它只返回使用过的优惠券代码。
请帮忙解决。
谢谢。
2016 年 6 月为 Woocommerce 2.5.x 制作的示例(不适用于 Woocommerce 3+)
此示例基于编译的搜索:
function wc_my_order_coupons( $order_id ) {
$order = new WC_Order( $order_id );
// checking if order has some used coupons
if( sizeof($order->get_used_coupons()) > 0 ) {
foreach( $order->get_used_coupons() as $coupon_code ) {
// Retrieving the coupon ID
$post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon');
$coupon_id = $post_obj->ID;
// here is a list of most all data that you can use for a coupon
$cp_discount_type = get_post_meta( $coupon_id, 'discount_type', true );
$cp_amount = get_post_meta( $coupon_id, 'coupon_amount', true );
$cp_indiv_use = get_post_meta( $coupon_id, 'individual_use', true );
$cp_products = get_post_meta( $coupon_id, 'product_ids' );
$cp_excl_products = get_post_meta( $coupon_id, 'exclude_product_ids' );
$cp_usage_limit = get_post_meta( $coupon_id, 'usage_limit', true );
$cp_expiry_date = get_post_meta( $coupon_id, 'expiry_date', true );
$cp_apply_before_tax = get_post_meta( $coupon_id, 'apply_before_tax', true );
$cp_free_shipping = get_post_meta( $coupon_id, 'free_shipping', true );
// Getting the products in this order_id
$items = $order->get_items();
foreach ($items as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
// Example: we use product_ids authorized in the coupon $cp_products
if (in_array($product_id, $cp_products)) {
echo $product_name . 'uses the coupon: ' . $coupon->code . '<br>';
}
}
}
}
}
参考 :
- WC_Coupon 类方法和属性 https://docs.woothemes.com/wc-apidocs/class-WC_Coupon.html
- WC ApiDocs - WC_Coupon 类源 https://docs.woothemes.com/wc-apidocs/source-class-WC_Coupon.html#18
- 以编程方式创建优惠券 https://docs.woothemes.com/document/create-a-coupon-programatically/
- 通过电子邮件发送订单中使用的优惠券 https://docs.woothemes.com/document/send-coupons-used-in-an-order-by-email/
- 显示优惠券说明 woocommerce https://stackoverflow.com/questions/28017767/display-coupon-description-woocommerce
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)