【发布时间】:2022-01-09 20:37:58
【问题描述】:
感谢@Bhautik 帮助更正了一些功能,以便在应用 URL 时自动生成 WooCommerce 优惠券。帖子在这里 - How to auto generate a coupon in WooCommerce and apply it to the cart?
我现在正尝试向同一功能添加免费产品。我有点让它工作但有错误。如果有人可以提供一些建议或帮助,将不胜感激。请看下面的截图。提前致谢。
https://i.imgur.com/gJVyW5O.gif
https://i.imgur.com/tzxyM83.gif
https://i.imgur.com/OIBbHlS.gif
这是我目前的代码。
function coupon_exists( $coupon_code ) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code );
$coupon_codes = $wpdb->get_results($sql);
if ( count( $coupon_codes ) > 0 ) {
return true;
} else {
return false;
}
}
// Generate coupon
function generate_coupon($coupon_generated) {
// Set some coupon data by default
$date_expires = date('Y-m-d H:i:s', strtotime('+24 hours'));
$discount_type = 'percent';
$coupon_amount = '100';
$coupon = new WC_Coupon();
$coupon->set_code($coupon_generated);
$coupon->set_discount_type($discount_type);
$coupon->set_amount($coupon_amount);
$coupon->set_date_expires($date_expires);
$coupon->set_individual_use(true);
$coupon->set_product_ids (array(74));
$coupon->set_usage_limit(1);
$coupon->set_limit_usage_to_x_items(1);
$coupon->set_usage_limit_per_user(1);
$coupon->set_exclude_sale_items(true);
$coupon->set_minimum_amount('100');
$coupon->set_maximum_amount('150');
$coupon->save();
return $coupon_generated;
}
// Add coupon and product to cart
function generate_coupon_add_product_to_cart ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$coupon_code = ( isset( $_GET['coupon-code'] ) && $_GET['coupon-code'] != '' ) ? $_GET['coupon-code'] : '' ;
if( $coupon_code == '' ){
return;
}
$applied_coupons = $cart->get_applied_coupons();
if( empty( $applied_coupons ) || ! in_array( $coupon_code, $applied_coupons ) ){
if ( !coupon_exists( $coupon_code ) ) {
generate_coupon( $coupon_code );
}
}
// I'm having trouble with the code below this point
$free_product_id = 74;
$free_product_in_cart = false;
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_count = $cart->get_cart_contents_count();
$free_product = $cart_item['data'];
if ( $free_product->get_id() == $free_product_id ) {
$free_product_key = $cart_item_key;
$regular_price = $cart_item['data']->get_regular_price();
$cart_item['data']->set_price(0);
$free_product_in_cart = true;
}
}
if ( !in_array($coupon_code, $applied_coupons) && !$free_product_in_cart ){
$cart->add_to_cart( $free_product_id, 1 );
$cart->add_discount( $coupon_code );
wc_add_notice( sprintf(__('<strong>FREE</strong> product valued at %s has been added to your cart.', 'woocommerce'),'<strong>' . get_woocommerce_currency_symbol() . $regular_price . '</strong>'), 'notice');
}
// Removing coupon
elseif( !in_array($coupon_code, $applied_coupons) && $free_product_in_cart ){
$cart->remove_coupon( $coupon_code );
wc_add_notice( __("Coupon removed"), 'notice' );
}
// Removing free product if it is the only one left in cart
elseif ( $free_product_in_cart && $cart_count <= 1 ){
$cart->remove_cart_item( $free_product_key );
wc_add_notice( __("Product removed"), 'notice' );
}
}
add_action('woocommerce_before_calculate_totals', 'generate_coupon_add_product_to_cart');```
【问题讨论】:
标签: php woocommerce cart hook-woocommerce