开发者

Magento remove & synchronize google base items by script in magento

开发者 https://www.devze.com 2023-02-13 07:57 出处:网络
I am using magento 1.3.2.4 and my problem is that sync & deleting google base items one by one takes a long time using the control panel.

I am using magento 1.3.2.4 and my problem is that sync & deleting google base items one by one takes a long time using the control panel.

So I want to know how to create a script that I can use in command line that sync & delete all items listing on google base in magento. I need to be abl开发者_如何转开发e to relist items too.


I hope the information below is useful to you:
http://www.magentocommerce.com/boards/viewreply/63666/


This is the same script noted in @anunays answer, but with some modifications I made earlier today so that it would work on 1.4.x as well as include some of Google Merchant Center's required attributes.

I just set condition to "new" and availability to "in stock" as the original script filtered on active, visible products.

#!/usr/bin/env php-5.3
<?php
/**
 * Generate a google base feed for google product search
 *
 * The core magento backend Gbase functionality was not working for me, and it
 * is oft maligned (at least in 1.4x and before) in the Mage community
 */
require_once 'app/Mage.php';
Mage::App('default');
define('GOOGLE_BASE_FEED_TXT', MY_URL . 'var/export/google_base_feed.txt');

class Generate_Google_Base_Feed_Txt
{
    public function __construct() {
        $this->handle = fopen(GOOGLE_BASE_FEED_TXT, 'w');
    }

    public function build() {
        $heading   = array(
            'id',
            'title',
            'description',
            'link',
            'image_link',
            'price',
            'brand',
            'product_type',
            'condition',
            'availability',
        );
        $feed_line = implode("\t", $heading) . "\r\n";
        fwrite($this->handle, $feed_line);

        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('*');
        $products->addAttributeToFilter('status', 1);
        $products->addAttributeToFilter('visibility', 4);
        $products->addAttributeToFilter(array(
            array('attribute'=>'manufacturer', 'notnull' => TRUE),
        ));
        $products->addAttributeToFilter(array(
            array('attribute'=>'name', 'notnull' => TRUE),
        ));
        $products->addAttributeToFilter(array(
            array('attribute'=>'image', 'notnull' => TRUE),
        ));
        $products->addAttributeToFilter(array(
            array('attribute'=>'url_key', 'notnull' => TRUE),
        ));

        foreach ($products as $product) {
            $product_data                 = array();
            $product_data['sku']          = $product->getSku();
            $product_data['title']        = $product->getName();
            $product_data['description']  = $product->getDescription();
            $product_data['link']         = MY_URL . $product->getUrlKey() . '.html';
            $product_data['image_link']   = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
            $product_data['price']        = $product->getPrice();
            $product_data['brand']        = $product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product);
            $product_data['product_type'] = '';
            $product_data['condition']    = 'new';
            /* availability: required ("in stock", "out of stock", "preorder", "available for order") */
            // currently, we only use in-stock but in the future we should include everything and just set out-of-stock as such
            $product_data['availability'] = 'in stock';

            foreach ($product->getCategoryIds() as $_categoryId) {
                $category = Mage::getModel('catalog/category')->load($_categoryId);
                // append categories
                $product_data['product_type'] .= $category->getName() . ', ';
            }
            $product_data['product_type'] = rtrim($product_data['product_type'], ', ');

            // sanitize data
            foreach ($product_data as $k => $val) {
                $bad              = array('"', "\r\n", "\n", "\r", "\t");
                $good             = array("", " ", " ", " ", "");
                $product_data[$k] = '"' . str_replace($bad, $good, $val) . '"';
            }

            $feed_line = implode("\t", $product_data) . "\r\n";
            fwrite($this->handle, $feed_line);
            fflush($this->handle);
        }
        fclose($this->handle);
    }
}

// test it, yo
$base_feed_txt = new Generate_Google_Base_Feed_Txt;
$base_feed_txt->build();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号