检查变体项目中产品名称和WooCommerce中允许的国家/地区中的特定字符串

问题描述

如何添加允许的国家/地区列表和标题中仅包含特定单词的目标产品,而不是版本ID。例如,“裱框A3海报”产品标题中的“裱框”一词。

这意味着我将避免定位每个单独的版本ID,因为我实际上有+280个产品。

下面的代码是我正在使用的代码

add_action( 'woocommerce_check_cart_items','check_cart_items_for_shipping' );
function check_cart_items_for_shipping() {
    $allowed_variation_id = '13021'; // Here defined the allowed Variation ID
    $allowed_country      = 'IE'; // Here define the allowed shipping country for all variations

    $shipping_country = WC()->customer->get_shipping_country();
    $countries        = WC()->countries->get_countries();

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( $shipping_country !== $allowed_country && $cart_item['variation_id'] !== $allowed_variation_id ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( __('The product "%s" can not be shipped to %s.'),$cart_item['data']->get_name(),$countries[$shipping_country]
            ),'error' );
            break; // stop the loop
        }
    }
}

解决方法

使用protected override void WndProc(ref Message msg) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (msg.Msg == WM_SYSCOMMAND && ((int)msg.WParam == SC_CLOSE)) { // click the 'X' button to close the application Application.Exit(); return; } base.WndProc(ref msg); } PHP函数将允许在购物车商品名称中找到一个字符串,如下所示(代码句柄也允许多个国家/地区代码)

strpos()

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...