优惠券控制:Wordpress插件问题



我的WordPress插件有一些问题。我是一个PHP初学者,所以我合并了一些论坛的技巧和代码块。该插件的目的是让你购买选定的产品(通过WooCommerce)在网站上只有当你有优惠券代码。

function available_coupon_codes()
{
global $wpdb;
// Get an array of all existing coupon codes
$coupon_codes = $wpdb->get_col(
"SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon' 
AND post_status = 'publish' ORDER BY post_name ASC");
// Return array of available coupon codes
return $coupon_codes; // always use return in a shortcode
}
function mandatory_coupon_code()
{
$targeted_ids = array(236116); // The targeted product ids (in this array)
$coupon_codes = available_coupon_codes();
$coupons_found = array_intersect(array_filter(array_map('sanitize_title', 
$coupon_codes)),
WC()->cart->get_applied_coupons());
// Loop through cart items
foreach (WC()->cart->get_cart() as $item) {
// Check cart item for defined product Ids and applied coupon
if (in_array($item['product_id'], $targeted_ids) && empty($coupons_found)) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice(sprintf(
'Pro zakoupení "%s" je potřeba zadat kód květinového předplatného.
', $item['data']->get_name()), 'error');
break; // stop the loop
}
}
}
add_action('woocommerce_check_cart_items', 'mandatory_coupon_code', 10, 0);

插件激活后,我的WordPress冻结,直到插件停用(通过FTP重命名插件文件夹)。如果可以,请告诉我问题出在哪里?

这是由于代码冻结,你的wordpress变成了只读状态所以,请在您的插件中找到代码冻结,然后上传…

最新更新