首次购买者将商品添加到购物车

2023-12-09

我有一家 WooCommerce 商店,我正在尝试将特定产品添加到 Woocommerce 购物车中,仅供首次购买者使用。我已经有以下 php 代码。

但由于某种原因,它不会起作用。

这是我的代码:

add_action('woocommerce_before_cart','woocommerce_add_to_cart');

function woocommerce_add_to_cart(){
if(! is_admin()){
    global $woocommerce;

    $product_id=912;
    $found=false;
    $first_customer = false;

    if(is_user_logged_in()){
        $user_id=get_current_user_id();
        $customer_orders=get_posts(array(

        'meta_key' => '_customer_user',
        'meta_value' => $user_id,
        'post_type' => 'shop_prder',
        'numberposts' => -1
        ));

        if(count($customer_orders) > 0) {
            $first_customer=true;
            wc_add_notice( sprintf( "first custommer check",error));
            $statuses=array('wc-failed','wc-cancelled','wc-refunded');

            foreach($customer_orders as $tmp_orders){
                $order =wc_get_order($tmp_orders->ID);
                if (! in_array($order->get_status(),$statuses)){
                    wc_add_notice( sprintf( "first custommer tmp check",error));
                    $first_customer=false;
                }
            }
        }

        if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
            wc_add_notice( sprintf( "items in cart check",error));
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $_product->id == $product_id ){
            wc_add_notice( sprintf( "produkt id check",error));
            $found = true;
                }
            }
        }

    }
    if (!$found && $first_customer){
            wc_add_notice( sprintf( "found and custommer check",error));
        WC()->cart->add_to_cart($product_id);
    }

  } 
}

如果有人能帮助我,我将非常感激。

Thanks


您的代码中有 2 个错误以及其他一些错误:

  • 您使用现有的 woocommerce 挂钩来命名您的函数
  • post_type 不是'shop_prder' but 'shop_order'

这是更改后的代码:

// 'woocommerce_add_to_cart' is an existing woocommerce hook so you can't use it to name your custom function here…
add_action('woocommerce_before_cart','first_time_buyers');

function first_time_buyers(){
    if( !is_admin() && is_user_logged_in() ){

        $product_id = 912;
        $found = false;
        $first_customer = true;

        // Getting current customer valid orders (see 'post_status' below)
        $customer_orders=get_posts(array(
            'meta_key' => '_customer_user',
            'meta_value' => get_current_user_id(),
            'post_type' => 'shop_order', // <= NOT 'shop_prder' but 'shop_order'
            // We add the accepted orders status here
            'post_status' => array('wc-on-hold', 'wc-processing', 'wc-completed'),
            'numberposts' => -1
        ));

        if( count($customer_orders) > 0 )
            $first_customer = false;

        if ( !WC()->cart->is_empty() )
            foreach ( WC()->cart->get_cart() as $cart_item )
                if ( $product_id == $cart_item['product_id'] ) {
                    $found = true;
                    break;
                }

        if (!$found && $first_customer){
            WC()->cart->add_to_cart($product_id);
        }
    }
}

代码位于活动子主题(或主题)的任何 php 文件中,或者也位于任何插件 php 文件中。

这是经过测试且功能齐全的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

首次购买者将商品添加到购物车 的相关文章

  • Active Record 库的 CodeIgniter 挂钩

    我需要一些帮助来理解 CodeIgniter 的钩子逻辑 以使代码适应我的需要 这一页 https www codeigniter com user guide general hooks html https www codeignite
  • 在cakephp中调用函数

    public function data if old status prev lat lat prev long long if status Village Unknown exec query else if status Town
  • PHP 构建/集成工具:您使用它们吗? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 错误 1366 (HY000):整数值不正确:第 1 行的列“id”的“”[已关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 My code sql INSERT INTO static table name sql join array key
  • 未找到请求的 URL - Laravel 5

    我正在尝试将一个网络应用程序 我使用 Laravel 5 制作 上传到 DigitalOcean Droplet 但我得到一个404错误 在此服务器上找不到请求的 URL public login 这是我的Apache2 conf
  • PHP Foreach数组转表格显示

    我有一个数组 day并希望以表格形式显示该数组 我正在使用 CI Array START EXECUTION gt Array 0 gt 27 OCT 14 1 gt 28 OCT 14 2 gt 29 OCT 14 NUM OF POPU
  • 在 codeigniter 中找不到我的核心类 My_head

    我正在尝试在 codeigniter 中创建核心类 在 application core 中 我创建一个名为 MY head php 的文件 MY head php 的代码是 class MY head extends CI Control
  • PHP 检查 NULL

    这是下面的代码 query mysql query SELECT FROM tablex if result mysql fetch array query if result column NULL print
  • 如何将多个动态行插入数据库

    我有一个使用 php 和 jQuery 创建的多行动态表 这是查看表格的链接 https jsfiddle net soumyar c8w2Lrk8 一切工作正常 除了当我将数据插入数据库时 序列号不按顺序保存 我的插入查询如下 for i
  • Zend DB Select 具有多个表联接

    尝试使用复制以下查询Zend Db Select 有什么指点吗 SELECT compounds id as compounds id reactions id as reactions id reaction compound numbe
  • 通过 WooCommerce 注册时将完整的电子邮件地址设置为用户名

    设置 帐户下的 WooCommerce 有一个名为 根据客户电子邮件自动生成用户名 但它生成的用户名不是完整的电子邮件地址 email protected cdn cgi l email protection becomes myemail
  • 下拉列表在 php 中保留先前选择的值

    我在 php 中创建了一个个人资料页面 用户使用 html 下拉列表选择性别 html代码如下 Gender
  • 将数组项合并到字符串中[重复]

    这个问题在这里已经有答案了 如何将所有数组项合并为一个字符串 Use the implode功能 http php net manual en function implode php 例如 fruits array apples pear
  • 如何对 array_chunk 中的元素进行分组

    我在 joomla 中创建模块时遇到问题 我的 php 技能受到限制 我使用 foreach 显示模板文件中的元素 div show information gt gt 我如何使用 array chunk 将 3 个元素分组到一个 div
  • 我想在我的 Wordpress BootStrap 导航中添加一行

    我有一个简单的网站 我想在导航上方添加一行 其中包含电话号码 也许还有一些社交链接 我的主题名为 Hestia Pro 找不到在哪里添加此额外行 我正在寻找一些关于将其添加到网站主题中的位置的指示 网站是 www gpoint co uk
  • 如何在另一个 php 脚本的后台运行 php 脚本(如更新按钮)

    当我按下 更新 按钮时 我将如何运行一个 php 脚本 然后它将运行脚本 x1 php 没有回显或其他输出 成功或失败 然后更新当前页面 我知道更新部分可以使用 ajax 完成 但我不确定如何以及如何让 x1 php 脚本在后台运行并在完成
  • 如何对 bcmath 数字进行向上、向下和舍入?

    我需要模仿的确切功能ceil http php net manual en function ceil php floor http www php net manual en function floor php and round ht
  • php 字符串与通配符 * 匹配?

    我想提供将字符串与通配符匹配的可能性 Example mystring dir folder1 file pattern dir file stringMatchWithWildcard mystring pattern gt Return
  • 图像上的中心水印

    我需要在图像上添加水印 我已经使用此代码解决了问题 工作良好 但图像位于左 下角 如何设置水印在图片中心居中 img test jpg Load the image where the logo will be embeded into i
  • 将静态站点生成器与 php 集成

    我目前正在使用 php 构建一个不需要定期更新的网站 并且我考虑使用静态站点生成器 因为它将具有类似博客的功能 然而 我的网站包含一个将与数据库链接的表单 我遇到的问题是静态站点生成器无法识别和解析 php 我目前正在考虑使用三个静态站点生

随机推荐