Is there a quick fire method of accessing all the data for a product from an external file by using its sku?
I have looked at How can I get prodcut data of a VirtueMart 2 product in an external file?
and tried to replace the Product ID with my desired id but to no avail:
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct(Product_ID);
In short terms, I'm looking for a way to access product data.
Product sku may not be unique. so I have found a solution that works for me. Hope this helps you. This is the full code that you need to call product by sku externally.
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', 'C:\Server\www\joomla' );//you will have diff location for your site
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
function getproductBySKU($sku){
$db = JFactory::getDbo();
$db->setQuery(true);
$db->setQuery("SELECT virtuemart_product_id FROM #__virtuemart_products WHERE product_sku= $sku");
$productids = $db->loadAssocList();
return $productids;
}
function getProduct($id)
{
if (!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'config.php');
VmConfig::loadConfig();
if (!class_exists('VmModel')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct($id);
return $product;
}
$products = getproductBySKU(1);//In this example SKU is 1 having 2 products
var_dump($products);//Gives the product id's in the SKU
foreach($products as $product){
var_dump(getProduct($product));
}
Related
I'm well familiar with coding in PHP and other languages, but I'm totally unfamiliar with Joomla.
What I want to accomplish is to know when a user has logged in on the Joomla site (under /web) and differentiate this script's behaviour accordingly.
I have a Joomla 2.5 site under /web/ and I have a script under /web/test/index.php, where the following code lies (collected from other replies in here and elsewhere):
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
echo dirname(dirname(__FILE__)) . "\n";
if (file_exists(dirname(dirname(__FILE__)) . '/defines.php'))
include_once dirname(dirname(__FILE__)) . '/defines.php';
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(dirname(__FILE__)));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$app = JFactory::getApplication('site');
$app->initialise();
print_r(JFactory::getUser());
What I get back is
/web
JUser Object
(
[isRoot:protected] =>
[id] => 0
[name] =>
...
Thank you,
Use this code it's working:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe = JFactory::getApplication('site');
var_dump(JFactory::getUser());
I want to modify the current template params via database outside joomla framework. I managed to set the 'preset' param, but I can't write it back to the database. Here is my code.
Thanks a lot
// Initialize The Joomla Framework
// -----------------------------------------------------------------------------------
define('_JEXEC', 1);
// this is relative to the current script, so change this according to your environment
define('JPATH_BASE', '/home/kristof/public_html/joomla1');
define('DS', DIRECTORY_SEPARATOR);
// Require Joomla libraries
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_CONFIGURATION . DS . 'configuration.php');
require_once(JPATH_LIBRARIES . DS . 'joomla' . DS . 'database' . DS . 'database.php');
require_once(JPATH_LIBRARIES . DS . 'import.php');
// -----------------------------------------------------------------------------------
$app = JFactory::getApplication('site');
$template = $app->getTemplate(true);
$param=$template->params->set('preset','preset3');
$template = JFactory::getApplication('site')->getTemplate();
$db = JFactory::getDBO();
$sql = "select params from #__template_styles where template = ".$db->quote($template);
$db->setQuery($sql);
$params = json_decode($db->loadResult());
$params->PARAM_NAME= PARAM_VALUE;
$sql = "update #__template_styles set params = ".$db->quote(json_encode($params))." where template = ".$db->quote($template);
$db->setQuery($sql);
return $db->query();
This used to work in 1.5... I could set up the following code:
function startJoomla() {
define('_JEXEC', true);
define( 'DS', DIRECTORY_SEPARATOR );
$dir= dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_LIBRARIES . DS . 'loader.php';
jimport('joomla.base.object');
jimport('joomla.factory');
jimport('joomla.filter.filterinput');
jimport('joomla.error.error');
jimport('joomla.event.dispatcher');
jimport('joomla.event.plugin');
jimport('joomla.plugin.helper');
jimport( 'joomla.utilities.utility' );
jimport('joomla.utilities.arrayhelper');
jimport('joomla.environment.uri');
jimport('joomla.environment.request');
jimport('joomla.user.user');
// JText cannot be loaded with jimport since it's not in a file called text.php but in methods
JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
$mainframe = & JFactory::getApplication('site');
$GLOBALS['mainframe'] = & $mainframe;
return $mainframe;
}
$mainframe = startJoomla();
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
And then, when I wanted to call something up from Joomla, I could do the following:
$custom = new CustomController(); // assuming there's a com_custom somewhere
This worked for 1.5. But in 2.5, the above doesn't work at all. I found a revised version of the load script:
function startJoomla() {
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
$dir = dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE.'/includes/defines.php';
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
return $app->initialise();
}
$mainframe = startJoomla();
This enables things like 'JFactory::getUser();' but will no longer allow me to utilize commands like 'new MycustomcomponentController();' Gives me 'core' Joomla functionality, but skips everything I installed. Is there an appropriate new revised compilation of jimport instructions or perhaps a different function of JComponent that allows me to call up my custom functions along with other components and models for use in external php files? Assume this will be run from a CRON or similar.
I am doing coding in Joomla and now i want to retrieve the User id that were Log in. When the user got logged in then the it works fine but when it clicks on my page then it shows an error. I think their may be caching or session problem. is it?
Here is my Joomla page code:
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
defined('_JEXEC') or die;
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// Initialise variables.
$app = JFactory::getApplication();
$user = JFactory::getUser();
$userId = $user->get('id');
$dispatcher = JDispatcher::getInstance();
$userId = $user->get('id');
echo $userId;
?>
Now in this code it retrieves 00 as a user id. But the user is logged in. So what is the problem?
use this code for detail users logged
$user = JFactory::getUser(); $userid = $user->id; echo $userid;
This line of code will get the current logged in user's ID.
$user_id = JFactory::getUser()->id;
I have created a rule in Jomsocial to award points for member who invites non users.. But now the problem is that...The points are awarded even
when the member enters "invite friends" page
points awarded when member sent email to a user(never checks whether user is a member in network or not)
How can I restrict this?
I need to award points only when the email is sent to a "non-user" or when the non-user clicks the link in the email body.
Currently this is used in components/com_community/libraries/mailq.php inside the function:
public function send( $total = 100 )
{
$mailqModel = CFactory::getModel( 'mailq' );
$userModel = CFactory::getModel( 'user' );
$mails = $mailqModel->get( $total, true );
$jconfig = JFactory::getConfig();
$mailer = JFactory::getMailer();
$config = CFactory::getConfig();
$senderEmail = $jconfig->getValue('mailfrom');
$senderName = $jconfig->getValue('fromname');
The code below is used to award points. I think some more conditions need to be added to make it validated:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_user.add.friend');
}
}
My grandpa used to say: "when you're shouting - I hear you, when you're talking - I'm listening..." ;)
One of the two problems, I believe you can solve by modifying your code as follows:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
$user =& JFactory::getUser();
if($user->id) {
// he's already a user - do nothing
}
else {
CuserPoints::assignPoint('com_user.add.friend');
}
}
}