Vytvorenie kupónu
function vytvor_kupon() {
$coupon_code = 'MOJKUPON'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon');
if( !in_array($coupon['post_title'], coupon_list() ) ) { // iba ak kupon este neexistuje
$new_coupon_id = wp_insert_post( $coupon );
}
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', vypis_podukty_s_tagom ()) ;
update_post_meta( $new_coupon_id, 'exclude_product_ids', '');
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
}
Vráti zoznam všetkých kupónov, konkrétne ich atribúty name a id
function coupon_list() {
$coupon_posts = get_posts( array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
) );
$coupon_codes = []; // Initializing
// Push to array
foreach ( $coupon_posts as $coupon_post ) {
$coupon_codes[$coupon_post->post_name] = $coupon_post->ID; // vytiahnem meno a id kupónu
}
// Return coupon array
return $coupon_codes;
}
Prejde všetky produkty a vytvori zoznam ID všetkých produktov ktoré majú danú značku
function moj_kupon(){
$tag = 'Hendi';
$data_tags = [];
$wc_query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 3,
'product_tag' => $tag,
) );
$i = 0;
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post();
$id = get_the_ID();
// $sku = get_post_meta( $id, '_sku', true );
array_push($data_tags, $id . ', ' );
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<?php endif; ?>
<?php
return implode(" ",$data_tags) ; //zmeni pole na string s IDčkami
}
Hooks ktoré zachytia pouzite kupónu a vyvolajú funkciu
//filter pre validaciu produktov
add_filter("woocommerce_coupon_is_valid","plugin_coupon_validation",10,3);
function plugin_coupon_validation($valid, $coupon, $product) {
if( 'hendiskusobne' === strtolower($coupon->code) ) {
foreach( WC()->cart->get_cart() as $cart_item ){
$id = $cart_item['product_id'];
$term_array = [];
$terms = wp_get_post_terms( $id, 'product_tag' );
foreach ( $terms as $term ) {
array_push( $term_array, $term->name);
}
if( in_array("Hendi", $term_array) ){
// echo 'nie je ';
}
else {
// $valid=false;
return false;
}
}
}
return true;
}
// filter pre validáciu kupónu
//add_filter( 'woocommerce_coupon_is_valid_for_product', 'wphelp_exclude_product_coupons', 9999, 4 );
function wphelp_exclude_product_coupons( $valid, $product, $coupon, $values ) {
$id = $product->id;
//echo get_post_meta( $id, 'product_tag', true );
$terms = wp_get_post_terms( $id, 'product_tag' );
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
if( in_array("Hendi", $term_array) ){
print_r($term_array);
}
}
// hook na aplikovanie kupónu
//add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'hendiskusobne' === $coupon_code ) {
if(current_user_can( 'administrator' )){
echo '<br>hendiskusobne<br>';
$coupon_id = 110169;
update_post_meta( $coupon_id, 'product_ids', ', ' . moj_kupon() ) ;
}
}
}
Zdroje:
https://quadlayers.com/exclude-woocommerce-product-from-coupons/
https://www.phpdevelopment.ca/2017/04/22/creating-a-custom-woocommerce-coupon/
https://themes.artbees.net/blog/how-to-check-if-a-woocommerce-coupon-is-applied/
https://quadlayers.com/edit-woocommerce-coupon-code/
https://storepro.io/learn/how-to-create-a-coupon-programmatically-in-woocommerce/
https://stackoverflow.com/questions/72270940/get-a-list-of-available-woocommerce-coupons-in-a-select-field