Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在magento中,如果客户至少有一个订单,则更新客户实体_Magento_Updates - Fatal编程技术网

在magento中,如果客户至少有一个订单,则更新客户实体

在magento中,如果客户至少有一个订单,则更新客户实体,magento,updates,Magento,Updates,我已经为用户创建了一个自定义属性is_buyer,它将在订单发布事件后设置为1 现在我想为至少有一个订单的老用户将“is_buyer”设置为1 我可以使用foreach循环来实现这一点,但我希望通过单个查询来实现这一点 任何人都知道如何使用magento为上述任务编写查询 我已经使用循环编写了一次脚本 <?php set_time_limit(0); $customers = Mage::getModel('customer/customer')

我已经为用户创建了一个自定义属性is_buyer,它将在订单发布事件后设置为1

现在我想为至少有一个订单的老用户将“is_buyer”设置为1

我可以使用foreach循环来实现这一点,但我希望通过单个查询来实现这一点

任何人都知道如何使用magento为上述任务编写查询

我已经使用循环编写了一次脚本

    <?php
    set_time_limit(0);
    $customers = Mage::getModel('customer/customer')
            ->getCollection()
            ->addAttributeToSelect('entity_id')
            ->addAttributeToFilter(array(array('attribute' => 'is_buyer', 'null' => true)), '', 'left');

    $orders = Mage::getSingleton('sales/order')->getCollection()
        ->addAttributeToSelect('customer_id');

    foreach ($customers as $customer) {   
        $orders->addFieldToFilter('customer_id', $customer->getId());
        if ($orders->count() > 0) {
            $customer->setData('is_buyer', 1);        
        } else {
             $customer->setData('is_buyer', 0);     
        }
        $customer->save();
}

您可以使用这种缓慢的方法一次性设置旧用户。并使用观察者
为新订单设置临时客户信息。因此,您只需要运行上述脚本一次。实际上,您可以进行一些性能调整…在foreach中保存和加载是不好的做法,但由于您只需要运行一个脚本,这可能无关紧要

$customers = Mage::getModel('customer/customer)->getCollection();

foreach ($customers as $_customer){

        $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$_customer->getId());                        
        if ($_orders->count()){
            $customer->setIsBuyer();
            $customer->save();
        }
}
活动内容如下:

<sales_order_place_after>    
    <observers>
       <your_observer>
         <type>singleton</type>
         <class>YOUR_CLASS</class>
         <method>YOUR_METHOD</method>
       </your_observer>    
     </observers> 
 </sales_order_place_after>

下面是安装程序代码,它可以在一个查询而不是循环中完成这项工作

/** @var Mage_Customer_Model_Entity_Setup $setup */
$setup = $this;
$setup->startSetup();

$setup->addAttribute('customer', 'is_buyer', array(
    'type' => 'int',
    'input' => 'boolean',
    'label' => 'Is Buyer',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 0,
    'default' => '0',
    'visible_on_front' => 0,
    'position' => 200
));
$attributeId = $this->getAttributeId('customer', 'is_buyer');
$setup->run("
     INSERT INTO customer_entity_int (`entity_type_id`, `attribute_id`, `entity_id`, `value`)
    SELECT 1, {$attributeId}, c.entity_id, 1
    FROM customer_entity c
    WHERE c.entity_id IN (
       SELECT s.customer_id FROM sales_flat_order s
        WHERE s.customer_id IS NOT NULL
        GROUP BY s.customer_id
   )
   ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
");

请看我的问题,刚刚更新,我需要一个查询来做这件事,循环是另一个选项将需要时间时,模块将首次运行。所以,你为什么不告诉这个…你现在已经编辑了你的问题。运行脚本一次(执行时间可能无关紧要),然后继续使用observer解决方案来设置临时信息-以避免再次运行初始脚本…使用模块版本管理此脚本,但我需要使用out-loop来执行此操作。我的开发经理希望在没有外循环的情况下看到这一点:),所以我已将票证分配给他:)
/** @var Mage_Customer_Model_Entity_Setup $setup */
$setup = $this;
$setup->startSetup();

$setup->addAttribute('customer', 'is_buyer', array(
    'type' => 'int',
    'input' => 'boolean',
    'label' => 'Is Buyer',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 0,
    'default' => '0',
    'visible_on_front' => 0,
    'position' => 200
));
$attributeId = $this->getAttributeId('customer', 'is_buyer');
$setup->run("
     INSERT INTO customer_entity_int (`entity_type_id`, `attribute_id`, `entity_id`, `value`)
    SELECT 1, {$attributeId}, c.entity_id, 1
    FROM customer_entity c
    WHERE c.entity_id IN (
       SELECT s.customer_id FROM sales_flat_order s
        WHERE s.customer_id IS NOT NULL
        GROUP BY s.customer_id
   )
   ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);
");